亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android-使用展開/捏放大/縮小RelativeLayout

Android-使用展開/捏放大/縮小RelativeLayout

江戶川亂折騰 2019-10-29 11:05:41
我有一個包含RelativeLayout和的私有類的活動,該活動擴展了SimpleOnScaleGestureListener。在onScale聽眾的方法中,我想通過用戶展開/捏手指來放大/縮小整個布局(用戶看到的一切)。我想改變的布局不被永久的,即當病毒/捏合手勢是在我想的布局回去這是什么在首位(任何復位可能在做onScaleEnd的方法SimpleOnScaleGestureListener例如)。我試圖通過調用setScaleX和setScaleY在上RelativeLayout以及還使用來實現它ScaleAnimation。兩者均不會導致平滑縮放(或任何可能被稱為縮放的東西)。甚至可以放大/縮小RelativeLayout?我剩下的唯一想法是從緩存中讀取屏幕截圖,并將其作為ImageView整個布局的頂部,并通過放大/縮小此圖像setImageMatrix。但是,我不知道如何實現這一點。May布局還包含一個片段的容器,在應該進行縮放時該片段為空。通過該onScaleEnd手勢,將片段放入其容器中(已經實現并且可以正常工作)。這是我的布局:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout_pinch"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffff" >   <!-- Layout containing the thumbnail ImageViews --><LinearLayout    android:id="@+id/thumbnail_group_pui"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_centerVertical="true"    android:orientation="horizontal" >    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/tn_c1"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/tn_c2"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/tn_c3"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/tn_c4"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/tn_c5"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/tn_c6"/></LinearLayout>我沒有實。我必須在擴展類中包括哪些其他方法來實際縮放布局或重置布局?
查看完整描述

3 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

因此,我創建了RelativeLayout上述主題中所述的的子類。看起來像這樣:


public class ZoomableRelativeLayout extends RelativeLayout {

float mScaleFactor = 1;

float mPivotX;

float mPivotY;


public ZoomableRelativeLayout(Context context) {

    super(context);

    // TODO Auto-generated constructor stub

}


public ZoomableRelativeLayout(Context context, AttributeSet attrs) {

    super(context, attrs);

    // TODO Auto-generated constructor stub

}


public ZoomableRelativeLayout(Context context, AttributeSet attrs,

        int defStyle) {

    super(context, attrs, defStyle);

    // TODO Auto-generated constructor stub

}


protected void dispatchDraw(Canvas canvas) {

    canvas.save(Canvas.MATRIX_SAVE_FLAG);

    canvas.scale(mScaleFactor, mScaleFactor, mPivotX, mPivotY);

    super.dispatchDraw(canvas);

    canvas.restore();

}


public void scale(float scaleFactor, float pivotX, float pivotY) {

    mScaleFactor = scaleFactor;

    mPivotX = pivotX;

    mPivotY = pivotY;

    this.invalidate();

}


public void restore() {

    mScaleFactor = 1;

    this.invalidate();

}


}

我對SimpleOnScaleGestureListener外觀的實現如下所示:


private class OnPinchListener extends SimpleOnScaleGestureListener {


    float startingSpan; 

    float endSpan;

    float startFocusX;

    float startFocusY;



    public boolean onScaleBegin(ScaleGestureDetector detector) {

        startingSpan = detector.getCurrentSpan();

        startFocusX = detector.getFocusX();

        startFocusY = detector.getFocusY();

        return true;

    }



    public boolean onScale(ScaleGestureDetector detector) {

        mZoomableRelativeLayout.scale(detector.getCurrentSpan()/startingSpan, startFocusX, startFocusY);

        return true;

    }


    public void onScaleEnd(ScaleGestureDetector detector) {

        mZoomableRelativeLayout.restore();

    }

}

希望這可以幫助!


更新:

您可以整合OnPinchListener你的ZoomableRelativelayout使用ScaleGestureDetector:


ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(this, new OnPinchListener());

并且您需要將Zoomable布局的觸摸偵聽器與ScaleGestureDetector的觸摸偵聽器綁定:


mZoomableLayout.setOnTouchListener(new OnTouchListener() {


                @Override

                public boolean onTouch(View v, MotionEvent event) {

                    // TODO Auto-generated method stub

                    scaleGestureDetector.onTouchEvent(event);

                    return true;

                }

            });


查看完整回答
反對 回復 2019-10-29
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

我認為我設法提高了Schnodahipfe的答案。我向ZoomableRelativeLayout類添加了兩個方法。


public void relativeScale(float scaleFactor, float pivotX, float pivotY)

{

    mScaleFactor *= scaleFactor;


    if(scaleFactor >= 1)

    {

        mPivotX = mPivotX + (pivotX - mPivotX) * (1 - 1 / scaleFactor);

        mPivotY = mPivotY + (pivotY - mPivotY) * (1 - 1 / scaleFactor);

    }

    else

    {

        pivotX = getWidth()/2;

        pivotY = getHeight()/2;


        mPivotX = mPivotX + (pivotX - mPivotX) * (1 - scaleFactor);

        mPivotY = mPivotY + (pivotY - mPivotY) * (1 - scaleFactor);

    }


    this.invalidate();

}


public void release()

{

    if(mScaleFactor < MIN_SCALE)

    {

        final float startScaleFactor = mScaleFactor;


        Animation a = new Animation()

        {

            @Override

            protected void applyTransformation(float interpolatedTime, Transformation t)

            {

                scale(startScaleFactor + (MIN_SCALE - startScaleFactor)*interpolatedTime,mPivotX,mPivotY);

            }

        };


        a.setDuration(300);

        startAnimation(a);

    }

    else if(mScaleFactor > MAX_SCALE)

    {

        final float startScaleFactor = mScaleFactor;


        Animation a = new Animation()

        {

            @Override

            protected void applyTransformation(float interpolatedTime, Transformation t)

            {

                scale(startScaleFactor + (MAX_SCALE - startScaleFactor)*interpolatedTime,mPivotX,mPivotY);

            }

        };


        a.setDuration(300);

        startAnimation(a);

    }

}

并像這樣重寫OnPinchListener類


private class OnPinchListener extends ScaleGestureDetector.SimpleOnScaleGestureListener

{

    float currentSpan;

    float startFocusX;

    float startFocusY;


    public boolean onScaleBegin(ScaleGestureDetector detector)

    {

        currentSpan = detector.getCurrentSpan();

        startFocusX = detector.getFocusX();

        startFocusY = detector.getFocusY();

        return true;

    }


    public boolean onScale(ScaleGestureDetector detector)

    {

        ZoomableRelativeLayout zoomableRelativeLayout= (ZoomableRelativeLayout) ImageFullScreenActivity.this.findViewById(R.id.imageWrapper);


        zoomableRelativeLayout.relativeScale(detector.getCurrentSpan() / currentSpan, startFocusX, startFocusY);


        currentSpan = detector.getCurrentSpan();


        return true;

    }


    public void onScaleEnd(ScaleGestureDetector detector)

    {

        ZoomableRelativeLayout zoomableRelativeLayout= (ZoomableRelativeLayout) ImageFullScreenActivity.this.findViewById(R.id.imageWrapper);


        zoomableRelativeLayout.release();

    }

}

每次觸摸事件結束時,原始答案都會重置比例,但是像這樣,您可以放大和縮小多次。


查看完整回答
反對 回復 2019-10-29
  • 3 回答
  • 0 關注
  • 1115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號