简体   繁体   English

Android在Canvas中使用OnTouchListener

[英]Android Using OnTouchListener in Canvas

So, I'm developing an app where user will be able to interact with image, I need to use OnTouchListener, but I don't know how to do it properly. 因此,我正在开发一个用户可以与图像进行交互的应用程序,我需要使用OnTouchListener,但是我不知道如何正确地进行操作。 Whenever I click on image - nothing happens. 每当我单击图像时,什么都不会发生。 In the end I'm trying to get circle that will appear when user touches the image and that'll move with the finger. 最后,我试图获得一个圆圈,该圆圈将在用户触摸图像时出现,并且将随手指移动。 Here's my MainActivity: 这是我的MainActivity:

public class MainActivity extends Activity{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        File file = new File(Environment.getExternalStorageDirectory() + "/Pictures/boxes.jpg");

        String fileString = file.getPath();


        takenPhoto = (ImageView) findViewById(R.id.imageView1);

        bmp = BitmapFactory.decodeFile(fileString);
        mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
        takenPhoto.setImageBitmap(mutableBitmap);

    }

    private static class DrawView extends View {



        public DrawView(Context context) {
            super(context);
        }


         public boolean onTouch(View view, MotionEvent event) {


                int action = event.getAction(); 

                drawPos.x = event.getX();
                drawPos.y = event.getY();

                switch (action) { 
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    drawing = true;
                    this.invalidate();
                    break; 
                case MotionEvent.ACTION_UP:   
                case MotionEvent.ACTION_CANCEL:
                    drawing = false;
                    this.invalidate();
                    break; 

                default: 
                    break; 
                }



             return true;
         }

         @Override
         protected void onDraw(Canvas canvas) {

             super.onDraw(canvas);

             if (drawing) {
                 canvas.drawCircle(zoomPos.x, zoomPos.y, 100, mPaint);
             }
         }





    }


}

A simpler solution would be to use a custom ImageView that will handle both the image and the draw: 一个更简单的解决方案是使用一个自定义ImageView来处理图像和绘图:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class DrawingImageView extends ImageView {

    private PointF point;
    private Paint paint = new Paint();

    public DrawingImageView(Context context) {
        super(context);
    }

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

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

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                point = new PointF(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                point.set(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                point = null;
                invalidate();
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        if (point != null) {
            canvas.drawCircle(point.x, point.y, 100, paint);
        }
    }
}

Then in your xml file: 然后在您的xml文件中:

    <your.package.DrawingImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

And in your Activity: 在您的活动中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //...
    view = (DrawingImageView) findViewById(R.id.image);
    //...
    view.setImageBitmap(mutableBitmap);
}

That's all 就这样

You need to remove the last onTouch method from your Activity and remove setOnTouchListener . 您需要从Activity中删除最后一个onTouch方法,并删除setOnTouchListener Since the activity captures touch event, the view will never get access to it. 由于活动捕获触摸事件,因此视图将永远无法访问它。

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File file = new File(Environment.getExternalStorageDirectory() + "/Pictures/boxes.jpg");
        String fileString = file.getPath();
        takenPhoto = (ImageView) findViewById(R.id.imageView1);

        bmp = BitmapFactory.decodeFile(fileString);
        mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
        takenPhoto.setImageBitmap(mutableBitmap);
    }

    private static class DrawView extends View {

        public DrawView(Context context) {
            super(context);
        }

         public boolean onTouch(View view, MotionEvent event) {
                int action = event.getAction(); 
                drawPos.x = event.getX();
                drawPos.y = event.getY();

                switch (action) { 
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    drawing = true;
                    this.invalidate();
                    break; 
                case MotionEvent.ACTION_UP:   
                case MotionEvent.ACTION_CANCEL:
                    drawing = false;
                    this.invalidate();
                    break; 

                default: 
                    break; 
                }
             return true;
         }

         @Override
         protected void onDraw(Canvas canvas) {
             super.onDraw(canvas);
             if (drawing) {
                 canvas.drawCircle(zoomPos.x, zoomPos.y, 100, mPaint);
             }
         }
    }

}

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

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