Android-使用展开/捏放大/缩小RelativeLayout

我有一个包含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>


我没有实。我必须在扩展类中包括哪些其他方法来实际缩放布局或重置布局?

江户川乱折腾
浏览 1091回答 3
3回答

慕侠2389804

因此,我创建了RelativeLayout上述主题中所述的的子类。看起来像这样:public class ZoomableRelativeLayout extends RelativeLayout {float mScaleFactor = 1;float mPivotX;float mPivotY;public ZoomableRelativeLayout(Context context) {&nbsp; &nbsp; super(context);&nbsp; &nbsp; // TODO Auto-generated constructor stub}public ZoomableRelativeLayout(Context context, AttributeSet attrs) {&nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; // TODO Auto-generated constructor stub}public ZoomableRelativeLayout(Context context, AttributeSet attrs,&nbsp; &nbsp; &nbsp; &nbsp; int defStyle) {&nbsp; &nbsp; super(context, attrs, defStyle);&nbsp; &nbsp; // TODO Auto-generated constructor stub}protected void dispatchDraw(Canvas canvas) {&nbsp; &nbsp; canvas.save(Canvas.MATRIX_SAVE_FLAG);&nbsp; &nbsp; canvas.scale(mScaleFactor, mScaleFactor, mPivotX, mPivotY);&nbsp; &nbsp; super.dispatchDraw(canvas);&nbsp; &nbsp; canvas.restore();}public void scale(float scaleFactor, float pivotX, float pivotY) {&nbsp; &nbsp; mScaleFactor = scaleFactor;&nbsp; &nbsp; mPivotX = pivotX;&nbsp; &nbsp; mPivotY = pivotY;&nbsp; &nbsp; this.invalidate();}public void restore() {&nbsp; &nbsp; mScaleFactor = 1;&nbsp; &nbsp; this.invalidate();}}我对SimpleOnScaleGestureListener外观的实现如下所示:private class OnPinchListener extends SimpleOnScaleGestureListener {&nbsp; &nbsp; float startingSpan;&nbsp;&nbsp; &nbsp; float endSpan;&nbsp; &nbsp; float startFocusX;&nbsp; &nbsp; float startFocusY;&nbsp; &nbsp; public boolean onScaleBegin(ScaleGestureDetector detector) {&nbsp; &nbsp; &nbsp; &nbsp; startingSpan = detector.getCurrentSpan();&nbsp; &nbsp; &nbsp; &nbsp; startFocusX = detector.getFocusX();&nbsp; &nbsp; &nbsp; &nbsp; startFocusY = detector.getFocusY();&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean onScale(ScaleGestureDetector detector) {&nbsp; &nbsp; &nbsp; &nbsp; mZoomableRelativeLayout.scale(detector.getCurrentSpan()/startingSpan, startFocusX, startFocusY);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public void onScaleEnd(ScaleGestureDetector detector) {&nbsp; &nbsp; &nbsp; &nbsp; mZoomableRelativeLayout.restore();&nbsp; &nbsp; }}希望这可以帮助!更新:您可以整合OnPinchListener你的ZoomableRelativelayout使用ScaleGestureDetector:ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(this, new OnPinchListener());并且您需要将Zoomable布局的触摸侦听器与ScaleGestureDetector的触摸侦听器绑定:mZoomableLayout.setOnTouchListener(new OnTouchListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onTouch(View v, MotionEvent event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scaleGestureDetector.onTouchEvent(event);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });

波斯汪

我认为我设法提高了Schnodahipfe的答案。我向ZoomableRelativeLayout类添加了两个方法。public void relativeScale(float scaleFactor, float pivotX, float pivotY){&nbsp; &nbsp; mScaleFactor *= scaleFactor;&nbsp; &nbsp; if(scaleFactor >= 1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; mPivotX = mPivotX + (pivotX - mPivotX) * (1 - 1 / scaleFactor);&nbsp; &nbsp; &nbsp; &nbsp; mPivotY = mPivotY + (pivotY - mPivotY) * (1 - 1 / scaleFactor);&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; pivotX = getWidth()/2;&nbsp; &nbsp; &nbsp; &nbsp; pivotY = getHeight()/2;&nbsp; &nbsp; &nbsp; &nbsp; mPivotX = mPivotX + (pivotX - mPivotX) * (1 - scaleFactor);&nbsp; &nbsp; &nbsp; &nbsp; mPivotY = mPivotY + (pivotY - mPivotY) * (1 - scaleFactor);&nbsp; &nbsp; }&nbsp; &nbsp; this.invalidate();}public void release(){&nbsp; &nbsp; if(mScaleFactor < MIN_SCALE)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; final float startScaleFactor = mScaleFactor;&nbsp; &nbsp; &nbsp; &nbsp; Animation a = new Animation()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void applyTransformation(float interpolatedTime, Transformation t)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scale(startScaleFactor + (MIN_SCALE - startScaleFactor)*interpolatedTime,mPivotX,mPivotY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; a.setDuration(300);&nbsp; &nbsp; &nbsp; &nbsp; startAnimation(a);&nbsp; &nbsp; }&nbsp; &nbsp; else if(mScaleFactor > MAX_SCALE)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; final float startScaleFactor = mScaleFactor;&nbsp; &nbsp; &nbsp; &nbsp; Animation a = new Animation()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void applyTransformation(float interpolatedTime, Transformation t)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scale(startScaleFactor + (MAX_SCALE - startScaleFactor)*interpolatedTime,mPivotX,mPivotY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; a.setDuration(300);&nbsp; &nbsp; &nbsp; &nbsp; startAnimation(a);&nbsp; &nbsp; }}并像这样重写OnPinchListener类private class OnPinchListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{&nbsp; &nbsp; float currentSpan;&nbsp; &nbsp; float startFocusX;&nbsp; &nbsp; float startFocusY;&nbsp; &nbsp; public boolean onScaleBegin(ScaleGestureDetector detector)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; currentSpan = detector.getCurrentSpan();&nbsp; &nbsp; &nbsp; &nbsp; startFocusX = detector.getFocusX();&nbsp; &nbsp; &nbsp; &nbsp; startFocusY = detector.getFocusY();&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean onScale(ScaleGestureDetector detector)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ZoomableRelativeLayout zoomableRelativeLayout= (ZoomableRelativeLayout) ImageFullScreenActivity.this.findViewById(R.id.imageWrapper);&nbsp; &nbsp; &nbsp; &nbsp; zoomableRelativeLayout.relativeScale(detector.getCurrentSpan() / currentSpan, startFocusX, startFocusY);&nbsp; &nbsp; &nbsp; &nbsp; currentSpan = detector.getCurrentSpan();&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public void onScaleEnd(ScaleGestureDetector detector)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ZoomableRelativeLayout zoomableRelativeLayout= (ZoomableRelativeLayout) ImageFullScreenActivity.this.findViewById(R.id.imageWrapper);&nbsp; &nbsp; &nbsp; &nbsp; zoomableRelativeLayout.release();&nbsp; &nbsp; }}每次触摸事件结束时,原始答案都会重置比例,但是像这样,您可以放大和缩小多次。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android