Zoom in/out the whole page layout

时间:2017-05-16 09:34:23

标签: java android

I am trying to write code for zoom in/out the whole page/screen of the app. I was given this link

Android - zoom in/out RelativeLayout with spread/pinch

but it's really difficult for a beginner to understand all the procedures to follow.

If someone can help and provide clearer explanation on this topic, I and other beginners will surely appreciate it.

So far I have set MainActivity, AnswerActivity and Fragments.

1 个答案:

答案 0 :(得分:2)

首先,让我们开始吧。缩放比较容易。 (此代码未在其他示例中使用):

    TextView rootView;
    rootView.setScaleX(sx);
    rootView.setScaleY(sx);

sxsy是比例[X / Y]

这是扩展的基础。现在我们来看看困难的部分:捏缩放。这需要用户以触摸事件的形式输入。

如果您不能将onTouchEvent用于根视图,请先设置onTouchListener。 (我不会展示这部分)

在开始之前,声明一个名为scaleFactor的浮点数:

[ANY-ACCESS-MODIFIER] long scaleFactor = 1f;

首先,我们需要一个ScaleGestureListener。如果需要,这可以是嵌套类:

class Scaler extends ScaleGestureDetector {
    public Scaler(Context context, OnScaleGestureListener listener) {
        super(context, listener);
    }

    @Override
    public float getScaleFactor() {//Leave this method empty.
        return super.getScaleFactor();
    }
}

其次我们需要OnScaleGestureListener:

class ScaleListener implements ScaleGestureDetector.OnScaleGestureListener{

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        scaleFactor *= detector.getScaleFactor();

        if(scaleFactor > 2) scaleFactor = 2;//Limit to your liking
        else if(scaleFactor < 0.3f) scaleFactor = 0.3f;//Limit to your liking
        scaleFactor = (scaleFactor * 100) / 100;//jitter-protection
        //scaleMatrix.setScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());//This is for usage with a Matrix: Good for canvas and other areas where this is usable. This is from my own scaling code, so I keep the matrix around in this example in case it is needed
        tv.setScaleX(scaleFactor);
        tv.setScaleY(scaleFactor);
        tv.setPivotX(detector.getFocusX());
        tv.setPivotY(detector.getFocusY());
        return true;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {return true;}

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {}
}

现在,这是分成两部分的地方。如果可能,请使用onTouchEvent(MotionEvent ev)。如果您不能使用此方法(当您在其上方添加@Override时显示错误),则必须使用onTouchListener。在TextView上设置它(tv.setOnTouchListener(this);。确保该类实现OnTouchListener)

现在,无论您选择哪种方法,都要确保退货true

此代码应该适用于这两种方法,并且不限于特定方法:

(ev是MotionEvent)

    int pointers = ev.getPointerCount();

    if(pointers == 2) {

        zoom = true;
        s.onTouchEvent(ev);//pass original motionevent(unscaled) to zoom

    }

现在,基本代码已到位。现在我们需要为s

创建实例

全球声明:

private Scaler s;
private ScaleListener listener;

以及在哪里夸大布局:

listener = new ScaleListener();
s = new Scaler(c, listener);//c is a context. 

现在,假设所有组件都已到位,您就拥有了一个有效的放大/缩小系统。请注意,这不包括在缩放视图上滚动。您必须创建一个offsetX / Y变量,并在有一个指针时接收输入并检查您想要移动的距离。

使用TextView和触摸事件,您可以使用#setScrollX或#setScrollY以及偏移来设置新的滚动位置。

创建自己的自定义文本视图可能会更容易。您可以通过创建一个新类并使其扩展TextView来完成此操作。然后你可以根据需要进行修改。这将允许您将缩放等添加到自定义TextView中。如果您在单个班级中有多个文本视图,或者您有多个具有可缩放和可滚动文本视图的活动,则这是首选方法。

编辑:自定义文字视图

可悲的是,并没有很多集成工具能够发挥作用。 textview上的android:scrollbars不适用于实例。所以首先TextView必须有一个ScrolLView:

<ScrollView android:id="@+id/textScroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.package.ZoomableTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some hfndusijhgn hgnm hnixjkgbhn fvb uynfv bunfg vbuygn buy hgnyu gnui h  uh nuioiogfej uhud\nfhbnikjhgnuieskhg nmuimjhbnguijhgne \nfuyh ghfuisdghbuisjhgnuie dgjh\nifgb dsauingfbehja kbfiuej ksghbisdjkg nbhni\ngfdfjgdfh hdfh sdfhg sh "/>

</ScrollView>

和ZoomableTextView:

首先需要这种依赖:

compile 'com.android.support:appcompat-v7:25.3.1'

这是为了获得AppCompat库,因此TextView可以使用新功能,同时保持对早期版本的支持。现在为班级:

public class ZoomableTextView extends AppCompatTextView/*This is why the AppCompat dependency is needed*/ {
    private float textSize,
            textScale;
    private Scaler s;
    private ScaleListener listener;
    public ZoomableTextView(Context context) {
        super(context);
        init();
    }

    public ZoomableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ZoomableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init(){
        listener = new ScaleListener();
        s = new Scaler(getContext(), listener);
        textSize = getTextSize();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        super.onTouchEvent(ev);
        if(ev.getPointerCount() == 2){
            s.onTouchEvent(ev);
        }
        return true;
    }

    class ScaleListener implements ScaleGestureDetector.OnScaleGestureListener{

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            textScale *= detector.getScaleFactor();

            if(textScale > 2) textScale = 2;//Limit to your liking
            else if(textScale < 0.3f) textScale = 0.3f;//Limit to your liking
            textScale = (textScale * 100) / 100;//jitter-protection
            if(textScale < 0.3f) textScale = 0.3f;
            if(textScale > 2) textScale = 2;

            setTextSize(textSize * textScale);
            setPivotX(detector.getFocusX());
            setPivotY(detector.getFocusY());
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {return true;}

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {}
    }

    class Scaler extends ScaleGestureDetector {
        public Scaler(Context context, OnScaleGestureListener listener) {
            super(context, listener);
        }

        @Override
        public float getScaleFactor() {//Leave this method empty.
            return super.getScaleFactor();
        }
    }
}