简体   繁体   English

onTouchListener 在 ImageView 中无法正常工作

[英]onTouchListener is not working properly in ImageView

I am developing an android application, in my application I dynamically create some ImageViews and set that ImageViews in to a RelativeLayout.我正在开发一个 android 应用程序,在我的应用程序中我动态创建一些 ImageViews 并将该 ImageViews 设置为 RelativeLayout。 So I want to drag each image in the layout independently.Now its working but not properly, when I drag image then the image start vibrating and moving slowly.所以我想独立拖动布局中的每个图像。现在它工作但不正常,当我拖动图像时,图像开始振动并缓慢移动。 And I wanted to implement Pinch Zoom in this image.Is it possible?我想在这张图片中实现捏缩放。这可能吗? And how how can I remove the vibration on moving the image?以及如何消除移动图像时的振动? If any one know it please help me..如果有人知道请帮助我..

Thanks谢谢

This is my onTouchListener()这是我的onTouchListener()

    canvasImage[i].setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {


                x1 = motionEvent.getX();
                y1 = motionEvent.getY();

                //Log.v("vvvvvv", "" + x1 + " " + y1);
                final int action = motionEvent.getAction();

                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

                    case MotionEvent.ACTION_DOWN:

                        String w = imglayout.getTop() + " " + imglayout.getHeight();
                        Log.v("wwww", "" + w);

                        x2 = motionEvent.getX();
                        y2 = motionEvent.getY();

                        dx = x2 - canvasImage[finalI].getX();
                        dy = y2 - canvasImage[finalI].getY();

                        //Log.v("cccccc", "" + x2 + " " + y2);
                        moving = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        moving = false;
                        break;
                    case MotionEvent.ACTION_MOVE:


                        if (moving) {

                            canvasImage[finalI].setX(motionEvent.getX() - dx);
                            canvasImage[finalI].setY(motionEvent.getY() - dy);

                            //Log.v("qqqqqq", "" + canvasImage[finalI].getX());
                        }
                        break;
                }
                //mainLayout.invalidate();
                return true;
            }

        });

This is my setCanvasImage() method used to set images into RelativeLayout这是我的setCanvasImage()方法,用于将图像设置为 RelativeLayout

public void setCanvasImage() {


    int screenSize = getResources().getConfiguration().screenLayout &
            Configuration.SCREENLAYOUT_SIZE_MASK;


    final int imgCount = dbobj.getFromTemp();
    canvasImage = new ImageView[imgCount];
    imglayout = (RelativeLayout) findViewById(R.id.canvas);

    final String[] strImage = dbobj.getdImage();

    imglayout.removeAllViews();


    for (int i = 0; i < imgCount; i++) {

        canvasImage[i] = new ImageView(this);
        canvasImage[i].setTag("img_" + i);
        boolean tabletSize = getResources().getBoolean(R.bool.isTablet);

        if (tabletSize) {

            int imgWidth = 130;
            int imgHeight = 130;

            RelativeLayout.LayoutParams paramss = new RelativeLayout.LayoutParams(imgWidth, imgHeight);
            paramss.setMargins(posLeft, posTop, 0, 0);
            canvasImage[i].setLayoutParams(paramss);

            canvasImage[i].setScaleType(ScaleType.FIT_XY);
            canvasImage[i].setTag(strImage[i]);
            setImage(strImage[i], canvasImage[i]);
            imglayout.addView(canvasImage[i]);


        }
        if( screenSize == Configuration.SCREENLAYOUT_SIZE_NORMAL){


            int imgWidth = 100;
            int imgHeight = 100;
            RelativeLayout.LayoutParams paramss = new RelativeLayout.LayoutParams(imgWidth, imgHeight);
            paramss.setMargins(posLeft, posTop, 0, 0);
            canvasImage[i].setLayoutParams(paramss);
            canvasImage[i].setTag(strImage[i]);
            setImage(strImage[i], canvasImage[i]);
            imglayout.addView(canvasImage[i]);
        }

        posLeft = posLeft + 15;
    }
}

RelativeLayout xml file相对布局 xml文件

<RelativeLayout
    android:layout_below="@+id/btnhorscrolvw"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:id="@+id/canvas"
    android:background="#ffff"
    android:orientation="horizontal"
  >
</RelativeLayout>

I think this will work.我认为这会奏效。

float dX, dY;

@Override
public boolean onTouch(View view, MotionEvent event) {

switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:

        dX = view.getX() - event.getRawX();
        dY = view.getY() - event.getRawY();
        break;

    case MotionEvent.ACTION_MOVE:

        view.animate()
                .x(event.getRawX() + dX)
                .y(event.getRawY() + dY)
                .setDuration(0)
                .start();
        break;
    default:
        return false;
}
return true;
}

You need to use scalx and scaley factor of imageview for implementing pinch zooming您需要使用 imageview 的 scalx 和比例因子来实现捏缩放

imageview.setScaleX(scalefactor);
imageview.setScaleY(scalefacto);

Android you can adjust the margin left and margin top to for draging the imageview Android 您可以调整左边距和上边距以拖动 imageview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM