滚动/平移或拖动自定义布局 [英] Scroll/Panning or drag in custom layout

查看:122
本文介绍了滚动/平移或拖动自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我是新来的Andr​​oid,现在我想将平移/拖动应做的ImageView其中,也就是说,势必限制我做了定制的相对布局与大量的研究放大。

我不使用矩阵,但 ScaleGestureDetector.SimpleOnScaleGestureListener 。现在我想将平移/拖在相同的。

我如何实现拖/平移在我的code?这是我用正确的注释和理解定制的相对布局。

  / *
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 *
 * @description:自定义布局放大
 *
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
* /

包com.layoutzooming;

进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Rect;
进口android.util.AttributeSet;
进口android.util.Log;
进口android.view.GestureDetector;
进口android.view.GestureDetector.OnDoubleTapListener;
进口android.view.GestureDetector.OnGestureListener;
进口android.view.MotionEvent;
进口android.view.ScaleGestureDetector;
进口android.view.View;
进口android.view.ViewParent;
进口android.widget.RelativeLayout;

公共类ZoomLayout扩展RelativeLayout的实现OnDoubleTapListener,OnGestureListener {

    // ScalingFactor即放大的量
    静浮mScaleFactor = 1.0F;

    //最大和最小放大
    私有静态浮动MIN_ZOOM = 1.0F;
    私有静态浮动MAX_ZOOM = 3.0F;

    //要使用不同的操作
    私人最终诠释NONE_OPERATION = 0;
    私人最终诠释ZOOM_OPERATION = 2;
    私人浮动mWidth = 1280;
    私人浮动mHeight = 800;

    //模式选择操作
    私人诠释方式;

    //轨迹的X和Y坐标的手指时,第一触摸屏
    私人浮动mInitialX = 0F;
    私人浮动mInitialY = 0F;

    //变焦计算偏移后跟踪界的形象
    静态矩形mClipBound;

    // mDetector检测scaleGesture为夹送缩放
    私人ScaleGestureDetector mDetector;

    // mDoubleTapDetector检测双击
    私人GestureDetector mDoubleTapDetector;

    //枢轴点缩放
    静态浮动GX = 0,曲线gy = 0;


    / *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @description:通过XML使用时调用构造函数
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /

    公共ZoomLayout(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        setWillNotDraw(假);
        mClipBound =新的矩形();
        // Intialize ScaleGestureDetector
        mDetector =新ScaleGestureDetector(的getContext(),新ZoomListener());
        mDoubleTapDetector =新GestureDetector(背景下,这一点);
        mDoubleTapDetector.setOnDoubleTapListener(本);
    }

    / *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @description:通过code使用时调用构造函数
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /
    公共ZoomLayout(上下文的背景下){
        超(上下文);
        setWillNotDraw(假);
        mClipBound =新的矩形();
        // Intialize ScaleGestureDetector
        mDetector =新ScaleGestureDetector(的getContext(),新ZoomListener());
        mDoubleTapDetector =新GestureDetector(背景下,这一点);
        mDoubleTapDetector.setOnDoubleTapListener(本);
    }

    / *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @description:的onTouchEvent布局负责处理所有类型的运动活动可能
    * @返回值: - 我们正在处理的TouchEvent。
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /
    @覆盖
    公共布尔的onTouchEvent(MotionEvent事件){

        //处理所有类型的运动活动可能
        开关(event.getAction()及MotionEvent.ACTION_MASK){

            案例MotionEvent.ACTION_DOWN:
                //发生事件时,第一手指pssed在屏幕上$ P $

                Log.d(打印,事件:ACTION_DOWN);
                mInitialX = event.getX();
                mInitialY = event.getY();

                打破;

            案例MotionEvent.ACTION_POINTER_DOWN:
                //发生事件时,第二个手指pssed下跌$ P $

                Log.d(打印,事件:Action_Pointer_Down);
                //如果第二个手指pssed在屏幕上与第一组的模式来进行变焦操作$ P $
                模式= ZOOM_OPERATION;

                打破;

            案例MotionEvent.ACTION_UP:
                //当所有的手指都采取了屏幕的发生事件
                Log.d(打印,事件:Action_UP);
                //如果所有的手指都溶于不会有操作
                模式= NONE_OPERATION;


                打破;
        }

        //给这个事件到mDetector得到缩放因子
        mDetector.onTouchEvent(事件);

        //给该事件的mDoubleTapDetector为doubleTap
        mDoubleTapDetector.onTouchEvent(事件);

        返回true;
    }

    / *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @description:通过覆盖onInterceptTouchEventŸ覆盖onInterceptTouchEvent,
    *这可以让你观看的事件,因为他们被分派到你的孩子,
    *获取当前手势的所有权在任何时候。允许RelativeLayout的的的onTouchEvent处理
    *所有motioin事件。
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /
    @覆盖
    公共布尔onInterceptTouchEvent(MotionEvent EV){
        的onTouchEvent(EV);
        返回super.onInterceptTouchEvent(EV);

    }

    / *
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @descriptiont:invalidateChildInParent
    *
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /
    @覆盖
    公共ViewParent invalidateChildInParent(INT []的位置,矩形脏){
        返回super.invalidateChildInParent(位置,脏);
    }

    / *
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @descriptiont:正确设置x,相对于彼此为不同的比例系数的孩子的y位置。
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /
    @覆盖
    保护无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在
    {
        诠释计数= getChildCount();
        的for(int i = 0; I<计数;我++){
            查看孩子= getChildAt(我);
            如果(child.getVisibility()!= GONE){
                RelativeLayout.LayoutParams PARAMS =(RelativeLayout.LayoutParams)child.getLayoutParams();
                child.layout(
                    (中间体)(params.leftMargin),
                    (中间体)(params.topMargin),
                    (中间体)((params.leftMargin + child.getMeasuredWidth())),
                    (中间体)((params.topMargin + child.getMeasuredHeight()))
                    );
            }
        }
    }

    / *
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @description:通过抽签调用,以绘制ChildViews。我们需要得到控制孩子们之前
    *拉伸从而以应用缩放因子
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /
    @覆盖
    保护无效dispatchDraw(帆布油画){

        //保存画布设置从探测器返回的比例因子
        canvas.save(Canvas.MATRIX_SAVE_FLAG);

        canvas.scale(mScaleFactor,mScaleFactor,GX,GY);

        super.dispatchDraw(画布);

        mClipBound = canvas.getClipBounds();

          canvas.restore();
    }

    / *
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    * @name:ZoomListener
    * @description:类定义的侦听ScaleGestureDetector
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    * /
    私有类ZoomListener扩展ScaleGestureDetector.SimpleOnScaleGestureListener {
        @覆盖
        公共布尔onScale(ScaleGestureDetector检测器){
            //从检测器获得的比例因子
            mScaleFactor * = detector.getScaleFactor(); //给出从previous缩放比例因子到当前
            // Log.d(打印,探测器定标因子+ mScaleFactor);

            GX = detector.getFocusX();
            GY = detector.getFocusY();
            //限制其规模因素,在MIN和MAX结合
            mScaleFactor = Math.max(Math.min(mScaleFactor,MAX_ZOOM),MIN_ZOOM);
            // Log.d(打印,有限的比例因子+ mScaleFactor);

            / * //强制画布重绘自身只有当一个事件是发生(例如缩放只),否则,不坏在这里多操作
               正如我们所去的滚动或平移将不会反映在这里。因此,我们将在OnDraw的方法添加此
            无效(); * /
             //这里我们只变焦使无效也要做
            无效();
            requestLayout();

            //我们处理onScale
            返回true;
        }
    }

    @覆盖
    公共布尔onDoubleTap(MotionEvent E){
        //使mScaleFactor其正常价值
        mScaleFactor = 1.0F;
        //强制画布再次重绘本身的变化已经发生。
        无效();
        requestLayout();
            返回false;
    }

    @覆盖
    公共布尔onDoubleTapEvent(MotionEvent E){
        // Log.d(打印,OnDoubleTapEvent);
            返回false;
    }

    @覆盖
    公共布尔onSingleTapConfirmed(MotionEvent E){
        // Log.d(打印,OnSingleTap);
            返回false;
    }

    @覆盖
    公共布尔onDown(MotionEvent E){
        // TODO自动生成方法存根
        返回false;
    }

    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,
                           浮velocityX,浮动velocityY){
        返回false;
    }


    @覆盖
    公共无效onLong preSS(MotionEvent E){
    }

    @覆盖
    公共布尔onScroll(MotionEvent E1,E2 MotionEvent,
                           浮distanceX,浮distanceY){
        返回false;
    }

    @覆盖
    公共无效OnShow中preSS(MotionEvent E){

    }

    @覆盖
    公共布尔onSingleTapUp(MotionEvent E){
        返回false;
    }
}
 

解决方案

我在做同样的事情。我把在的onTouchEvent ACTION_UP事件以下。

 如果(mScaleFactor→1)
            {
                GX = GX  - (event.getX() -  mInitialX);
                GY = GY  - (event.getY() -  mInitialY);
                无效();
                requestLayout();
            }
 

在哪里mScaleFactor,mInitialX,mInitialY距离ScaleGestureDetector onScale功能。缩放运作良好。一旦扩大规模,虽然我不能选择布局的看法。有时工作,有时没有。

I have made custom Relative Layout for Zooming with lot of research since I am new to Android and now I want to incorporate the panning/dragging which should behave like Imageview, that is, bound limits.

I am not using a matrix but the ScaleGestureDetector.SimpleOnScaleGestureListener. Now I want to incorporate the panning/dragging in the same.

How do I achieve drag/panning in my code? Here is my custom relative layout with proper commenting and understanding.

/*
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 *
 *  @description : Custom Layout Zoom
 *
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

package com.layoutzooming;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewParent;
import android.widget.RelativeLayout;

public class ZoomLayout extends RelativeLayout implements OnDoubleTapListener, OnGestureListener{

    //ScalingFactor i.e. Amount of Zoom
    static float mScaleFactor = 1.0f;

    // Maximum and Minimum Zoom
    private static float MIN_ZOOM = 1.0f;
    private static float MAX_ZOOM = 3.0f;

    //Different Operation to be used
    private final int NONE_OPERATION=0;
    private final int ZOOM_OPERATION=2;
    private float mWidth= 1280;
    private float mHeight=800;

    // Mode to select the operation
    private int mode;

    //Track X and Y coordinate of the finger when it first touches the screen
    private float mInitialX = 0f;
    private float mInitialY = 0f;

    // Track the Bound of the Image after zoom to calculate the offset
    static Rect mClipBound;

    // mDetector to detect the scaleGesture for the pinch Zoom
    private ScaleGestureDetector mDetector;

    // mDoubleTapDetector to detect the double tap
    private GestureDetector mDoubleTapDetector;

    //Pivot point for Scaling
    static float gx=0,gy=0;


    /*
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *  @description : Constructor is called when used via XML
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */

    public ZoomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        mClipBound = new Rect();
        // Intialize ScaleGestureDetector
        mDetector = new ScaleGestureDetector(getContext(), new ZoomListener());
        mDoubleTapDetector = new GestureDetector(context,this);
        mDoubleTapDetector.setOnDoubleTapListener(this);
    }

    /*
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *  @description : Constructor is called when used via code
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */
    public ZoomLayout(Context context) {
        super(context);
        setWillNotDraw(false);
        mClipBound = new Rect();
        // Intialize ScaleGestureDetector
        mDetector = new ScaleGestureDetector(getContext(), new ZoomListener());
        mDoubleTapDetector = new GestureDetector(context,this);
        mDoubleTapDetector.setOnDoubleTapListener(this);
    }

    /*
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *  @description : OnTouchEvent of the layout which handles all type of motion-events possible
    *  @ Returns : true  - we are handling the touchEvent.
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // Handles all type of motion-events possible
        switch(event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_DOWN:
                // Event occurs when the first finger is pressed on the Screen

                Log.d("Print", "Event: Action_Down " );
                mInitialX = event.getX();
                mInitialY = event.getY();

                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                //Event occurs when the second finger is pressed down

                Log.d("Print", "Event: Action_Pointer_Down " );
                // If second finger is pressed on the screen with the first set the Mode to Zoom operation
                mode=ZOOM_OPERATION;

                break;

            case MotionEvent.ACTION_UP:
                //Event occurs when all the finger are taken of the screen
                Log.d("Print", "Event: Action_UP " );
                //If all the fingers are taken up there will be no operation
                mode = NONE_OPERATION;


                break;
        }

        // Give the event to the mDetector to get the scaling Factor
        mDetector.onTouchEvent(event);

        // Give the event to the mDoubleTapDetector for the doubleTap
        mDoubleTapDetector.onTouchEvent(event);

        return true;
    }

    /*
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *   @description : By overriding the onInterceptTouchEvent y overriding the onInterceptTouchEvent,
    *   This allows you to watch events as they are dispatched to your children, and
    *   take ownership of the current gesture at any point.  Allowing onTouchEvent of RelativeLayout to handle the
    *   all the motioin events .
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        onTouchEvent(ev);
        return super.onInterceptTouchEvent(ev);

    }

    /*
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *   @descriptiont : invalidateChildInParent
    *
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */
    @Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
        return super.invalidateChildInParent(location, dirty);
    }

    /*
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *   @descriptiont : Correctly sets the x,y position of the children relative to each other for different scale factors.
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        int count = getChildCount();
        for(int i=0;i<count;i++){
            View child = getChildAt(i);
            if(child.getVisibility()!=GONE){
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)child.getLayoutParams();
                child.layout(
                    (int)(params.leftMargin ),
                    (int)(params.topMargin ),
                    (int)((params.leftMargin + child.getMeasuredWidth()) ),
                    (int)((params.topMargin + child.getMeasuredHeight()))
                    );
            }
        }
    }

    /*
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *   @description : Called by draw to draw the ChildViews. We need to gained control before the children are
    *   drawn so that to apply the scaling Factors
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */
    @Override
    protected void dispatchDraw(Canvas canvas) {

        //Save the canvas to set the scaling factor returned from detector
        canvas.save(Canvas.MATRIX_SAVE_FLAG);

        canvas.scale(mScaleFactor, mScaleFactor,gx,gy);

        super.dispatchDraw(canvas);

        mClipBound = canvas.getClipBounds();

          canvas.restore();
    }

    /*
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    *
    *   @name : ZoomListener
    *   @description :  Class which defines the listener for ScaleGestureDetector
    *
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    */
    private class ZoomListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // getting the scaleFactor from the detector
            mScaleFactor *= detector.getScaleFactor(); // Gives the scaling factor from the previous scaling to the current
            //    Log.d("Print", "detector scaling Factor" + mScaleFactor);

            gx = detector.getFocusX();
            gy = detector.getFocusY();
            // Limit the scale factor in the MIN and MAX bound
            mScaleFactor= Math.max(Math.min(mScaleFactor, MAX_ZOOM),MIN_ZOOM);
            //    Log.d("Print", "Bounded scaling Factor" + mScaleFactor);

            /*//Force canvas to redraw itself only if the one event is to happen (say Zooming only ) else do not invalidate here for multi operations
               As what we de for scrolling or panning will not reflect here. So we will add this in onDraw method
            invalidate();*/
             // Here we are only zooming so invalidate has to be done
            invalidate();
            requestLayout();

            // we have handle the onScale
            return true;
        }
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // Make the mScaleFactor to its normal value
        mScaleFactor=1.0f;
        // Force the canvas to redraw itself again as the changes has been occured.
        invalidate();
        requestLayout();
            return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        //    Log.d("Print", "OnDoubleTapEvent");
            return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        //    Log.d("Print", "OnSingleTap");
            return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                           float velocityX, float velocityY) {
        return false;
    }


    @Override
    public void onLongPress(MotionEvent e) {
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                           float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
}

解决方案

I am doing the same thing. I put the following in the onTouchEvent ACTION_UP event.

         if (mScaleFactor > 1)
            {
                gx = gx - (event.getX() - mInitialX);
                gy = gy - (event.getY() - mInitialY);
                invalidate();
                requestLayout();
            }

Where mScaleFactor, mInitialX, mInitialY are from the ScaleGestureDetector onScale function. The scaling is working well. Once scaled up though I cannot select views on the layout. Sometimes it works and sometimes it doesn't.

这篇关于滚动/平移或拖动自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆