在画布上绘制和删除位图图标

时间:2015-04-10 19:24:38

标签: android canvas bitmap

我正在开发一个应用程序,用户可以在汽车上标记损坏图标。这里的汽车基本上是一个图像,损坏图标也是一个图像。我可以使用以下代码绘制用户触摸汽车的图像:

public class TestActivity extends Activity {
    private Button btn_save, btn_resume;
    private ImageView iv_canvas;
    private Bitmap baseBitmap;
    private Canvas canvas;
    private Paint paint;
    public Bitmap bt;
    public Bitmap ct;
    boolean ismove = false;
    private static final int SWIPE_MIN_DISTANCE = 50;
    int i = 0;
    private float x = 0;

    public HashMap<String, String> testholdmap = new HashMap<String, String>();

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

        // The initialization of a brush, brush width is 5, color is red
        paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.RED);

        iv_canvas = (ImageView) findViewById(R.id.iv_canvas);

        iv_canvas.setOnTouchListener(touch);

        bt = BitmapFactory.decodeResource(getResources(),
                R.drawable.damage_mark_r_ic);
    }

    private View.OnTouchListener touch = new OnTouchListener() {
        // Coordinate definition finger touch
        float startX;
        float startY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            // Press the user action
            case MotionEvent.ACTION_DOWN:
                // The first drawing initializes the memory image, specify the
                // background is white
                if (baseBitmap == null) {
                    baseBitmap = BitmapFactory.decodeResource(getResources(),
                            R.drawable.b_ferrari_f152_000_0000).copy(
                            Bitmap.Config.ARGB_8888, true);
                    canvas = new Canvas(baseBitmap);

                }
                // Recording began to touch point coordinate
                startX = event.getX();
                startY = event.getY();
                break;
            // The user's finger on the screen of mobile action
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_UP:

                float stopX = event.getX();
                float stopY = event.getY();
                if (startX == stopX) {

                    canvas.drawBitmap(bt, startX, startY, paint);

                    // The pictures to the ImageView
                    iv_canvas.setImageBitmap(baseBitmap);
                } else if (startX - stopX > 50 || stopX - startX > 50) {

                    baseBitmap = null;
                }
                break;
            default:
                break;
            }
            return true;
        }
    };

}

我的问题是弄清楚如果已经绘制了该图标的删除方法。我希望它以这种方式运行:我在x,y位置绘制一个伤害图标,如果用户再次点击x,y位置,则会删除此损坏图标。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

使用此:

canvas.drawColor(0, Mode.CLEAR);

overlayBitmap.eraseColor(Color.TRANSPARENT);

这会将现有的Bitmap设置为全透明。

答案 1 :(得分:0)

我的解决方案如下: 每次用户点击图像时 - 在列表中存储该死区边界(x,y,w,h) 并在画布上绘制 如果用户再次点击相同的损坏图像 - 将其从列表中删除并重新绘制(清除画布 - Canvas.drawColor(Color.WHITE))所有内容:汽车和列表中的剩余图像