onDraw不适用于ImageView Android

时间:2017-07-28 11:44:04

标签: java android canvas

我需要在ImageView Android上画一点。

我使用View类的自定义子类并覆盖onDraw方法,如:

public class DrawPoints extends View {

    private float x;
    private float y;
    private Paint mPaint;

    public DrawPoints(Context context, float x, float y) {
        super(context);
        this.x = x;
        this.y = y;
    }

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

        Log.i("TAG", "onDraw: " + x + "" + y);
        mPaint = new Paint();
        mPaint.setColor(Color.YELLOW);
        canvas.drawCircle(x,y,200,mPaint);
     }
}

在我的MainActivity中我使用onTouch事件来绘制,也不要忘记使invalidate(),但它没有画出一点。

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;
    private Canvas mCanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCanvas = new Canvas();
        setContentView(R.layout.activity_main);
        mImageView = (ImageView) findViewById(R.id.image_main);
        mImageView.setOnTouchListener(new View.OnTouchListener() {

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

                float y = motionEvent.getY();
                float x = motionEvent.getX();
                new DrawPoints(MainActivity.this,x,y).draw(mCanvas);
                mImageView.invalidate();
                return false;
            }
        });
    }
}

它可能是什么?我已经在stackoverflow上阅读了很多关于onDraw的例子但没有帮助。

1 个答案:

答案 0 :(得分:2)

您可以创建一个在触摸事件上绘制圆圈的图像视图。 例如:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import java.util.ArrayList;

public class DrawPoints extends android.support.v7.widget.AppCompatImageView {

    private static final String TAG = DrawPoints.class.getSimpleName();

    private ArrayList<Point> mSavedPoints = new ArrayList<>();
    private Point mLastTouchPoint;
    private Paint mPaint;

    public DrawPoints(Context context) {
        super(context);
        init();
    }

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

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

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.YELLOW);
    }

    @Override public boolean dispatchTouchEvent(MotionEvent event) {
        mLastTouchPoint = new Point((int) event.getX(), (int) event.getY());
        postInvalidate();
        return super.dispatchTouchEvent(event);
    }

    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mLastTouchPoint != null) {
            if (mLastTouchPoint.x > 0 || mLastTouchPoint.y > 0) {
                mSavedPoints.add(mLastTouchPoint);
                Log.i(TAG, mLastTouchPoint.toString());
            }
        }

        for (Point point : mSavedPoints) {
            canvas.drawCircle(point.x, point.y, 200, mPaint);
        }

        Log.i(TAG, mSavedPoints.toString());
    }
}