如何在imageview上绘制一个圆圈

时间:2018-03-10 09:13:05

标签: android

如何在用户触摸图像的位置在图像上绘制圆圈。

使用imageview设置图像。

我在网上找到了一个解决方案,它在新画布上创建了图像位图并在其上绘制了一个圆圈(在http://joerg-richter.fuyosoft.com/?p=120处找到了解决方案) 像

这样的东西
ImageView imageView = (ImageView) findViewById (R.id.image1);  
myBitmap = BitmapFactory.decodeResource(getResources(), drawid);                   
Paint myCircPaint = new Paint();

tempBitmap = Bitmap.createBitmap(bitmapXht, bitmapYht, Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(tempBitmap);

 tempCanvas.drawBitmap(myBitmap, 0, 0, null);

 myCircPaint.setStrokeWidth(5);
 tempCanvas.drawCircle(evX, evY, 15, myCircPaint);

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

这是有效的,但由于我每次触摸都会绘制圆圈,因此每次都会绘制所有内容。如何避免每次都绘制主图像。是否可以在多个触摸事件中保留画布。

我还是Android的新手,所以请原谅这是非常简单的事情

1 个答案:

答案 0 :(得分:0)

您需要自定义视图。

创建TouchableImageView Java类:

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

    public static final String BUNDLE_SUPER_STATE = "bundle_super_state";
    public static final String BUNDLE_POINT_LIST = "bundle_point_list";

    private float mRadius;
    private Paint myCirclePaint = null;
    private List<PointF> mPoints;
    private GestureDetector mDetector;

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

    public TouchableImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TouchableImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mRadius = 15;
        myCirclePaint = new Paint();
        mPoints = new ArrayList<>();
        mDetector = new GestureDetector(context, new MyListener());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPoints != null) {
            for (int i = 0; i < mPoints.size(); i++) {
                canvas.drawCircle(mPoints.get(i).x, mPoints.get(i).y, mRadius, myCirclePaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }

    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(BUNDLE_SUPER_STATE, super.onSaveInstanceState());
        bundle.putParcelableArrayList(BUNDLE_POINT_LIST, (ArrayList<? extends Parcelable>) mPoints);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            mPoints = bundle.getParcelableArrayList(BUNDLE_POINT_LIST);
            super.onRestoreInstanceState(bundle.getParcelable(BUNDLE_SUPER_STATE));
        } else {
            super.onRestoreInstanceState(state);
        }
    }

    class MyListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDown(MotionEvent event) {
            if (mPoints != null) {
                mPoints.add(new PointF(event.getX(), event.getY()));
                invalidate();
            }
            return true;
        }
    }
}

并将ImageView替换为TouchableImageView

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <com.example.touchable.TouchableImageView
           android:id="@+id/image_view"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:src="@mipmap/ic_launcher" />

</android.support.constraint.ConstraintLayout>
相关问题