无法在可变图像上绘制android

时间:2015-02-25 19:51:15

标签: android

我正在尝试使用下面提到的代码在我从内部存储器加载的图像上绘制线条,但它无法正常工作

 imgView = (ImageView) findViewById(R.id.imageView);
 bitmap = BitmapFactory.decodeFile(img1.getAbsolutePath()).copy(Bitmap.Config.ARGB_8888,true);
 canvas = new Canvas(bitmap);
 paint = new Paint(Paint.ANTI_ALIAS_FLAG);
 paint.setColor(Color.RED);
 imgView.setImageBitmap(bitmap);
 imgView.setOnTouchListener(this);

但是当我将代码更改为:

    imgView = (ImageView) findViewById(R.id.imageView);
    bitmap = Bitmap.createBitmap((int) dw, (int) dh, Bitmap.Config.ARGB_8888);;
     canvas = new Canvas(bitmap);
     paint = new Paint(Paint.ANTI_ALIAS_FLAG);
     paint.setColor(Color.RED);
     imgView.setImageBitmap(bitmap);
     imgView.setOnTouchListener(this);

我可以用手指画线

有什么建议吗?

OnTouchCodeListner函数

 public boolean onTouch(View v, MotionEvent event){
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downx = event.getX();
                downy = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                upx = event.getX();
                upy = event.getY();
                canvas.drawLine(downx, downy, upx, upy, paint);
                imgView.invalidate();
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;
        }
        return true;
    }

1 个答案:

答案 0 :(得分:0)

这可能是因为它无法使其变得可变。尝试使用

Bitmap.createBitmap(BitmapFactory.decodeFile(img1.getAbsolutePath()),dw,dh);