在画布图像位图上绘制圆形onTouch

时间:2014-05-09 14:38:57

标签: android touch android-canvas draw android-bitmap

我正在使用CanvasBitmap显示图片。现在我想画一个圆圈,我触摸ImageView。如何实现这一目标?

而不是tempCanvas.drawCircle(50,100, 60, myPaint);,我想要一些东西 tempCanvas.drawCircle(event.getX(),event.getY(), 60, myPaint);

这是我的代码:

public class New extends Activity {

public final int RESULT_LOAD_IMAGE = 1;
public static int key = 0;

ImageView imageView ;
private Bitmap myBitmap;

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


    imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setAdjustViewBounds(true);
    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

                            //Get Image from Gallery
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            Toast.makeText(getApplicationContext(), "Select Image to Load", Toast.LENGTH_SHORT).show();
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        myBitmap = BitmapFactory.decodeFile(picturePath);

        Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
        Canvas tempCanvas = new Canvas(tempBitmap);
        tempCanvas.drawBitmap(myBitmap, 0, 0, null);

        Paint myPaint = new Paint();
        myPaint.setColor(Color.WHITE);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(15);
        myPaint.setAlpha(60);

        imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));

        tempCanvas.drawCircle(50,100, 60, myPaint);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:3)

您必须扩展ImageView并覆盖onTouchEvent()和onDraw()方法。

    public class MyImageView extends ImageView {

    private float lastX;
    private float lastY;
    ...

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

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

    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        lastX = event.getX();
        lastY = event.getY();

        invalidate();
        return super.onTouchEvent(event);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(lastX, lastY, 60, myPaint);
    }
}

并在您的xml文件中而不是:

<ImageButton
    android:id="@+id/imgView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

使用:

<my.package.MyImageView
    android:id="@+id/imgView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

答案 1 :(得分:0)

图片视图范围广,在图片视图中需要覆盖onDraw方法 并尝试绘制圆圈。 我希望它会奏效。