我的位图变成黑色,带有canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.MULTIPLY)Android

时间:2016-04-22 15:57:14

标签: android bitmap android-imageview android-canvas bitmapimage

我有一个Activity,它使用Picasso Library从URL加载图像。

此图像比屏幕大,这就是“onTouch”方法实现代码通过模拟地图滚动图像的原因。

我遇到的问题是我在该图像上画圆圈,每次点击屏幕我都应该画一个圆圈然后擦掉之前的。

使用此行canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.MULTIPLY);将我的整个位图变黑。

当我点击屏幕并删除上一页

时,我需要在图像上画一个圆圈

以下是我的活动代码的片段

将图像加载到imageView并创建位图以在复制位图上绘制画布

Picasso.with(getApplicationContext()).load(url).into(imageView, new Callback() {
  @Override
  public void onSuccess() {
  imageView.setVisibility(View.VISIBLE); 
  lineABmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
  lineAMutBmp = lineABmp.copy(Bitmap.Config.RGB_565, true);
  copy = Bitmap.createBitmap(lineAMutBmp);
  canvas = new Canvas(copy);
  imageView.setImageBitmap(copy);
}
});

绘制圆圈

    private void drawBeacon() {

    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.MULTIPLY);
    canvas.drawCircle(startX, startY, circle_radius, circle);

    canvas.drawRect(startX - 50,//left
            startY - 30, // top
            startX + 50, //right
            startY - 10, rect); //bottom

    canvas.drawText(" Minor: " + beacon.getMinor(),
            startX - 45,
            startY - 15, text);

    imageView.invalidate();


}

init Paint

    init(){
    circle = new Paint();
    circle.setColor(Color.rgb(46, 204, 113));
    circle.setStyle(Paint.Style.FILL);
    circle.setStrokeWidth(10);

    circle_radius = 10;

    text = new Paint();
    text.setColor(Color.BLACK);
    text.setTextSize(16);
    text.setFakeBoldText(true);

    rect = new Paint();
    rect.setColor(ContextCompat.getColor(getApplicationContext(), R.color.white_80));
    rect.setStyle(Paint.Style.FILL_AND_STROKE);   }

OnTouch,在这个方法中,我进行缩放和拖动图像,并调用方法来绘制圆圈

public boolean onTouch(View v, MotionEvent event) {
    ImageView view = (ImageView) v;
    view.setScaleType(ImageView.ScaleType.MATRIX);
    float[] m = new float[9];
    matrix.getValues(m);
    float transX = m[Matrix.MTRANS_X] * -1;
    float transY = m[Matrix.MTRANS_Y] * -1;

    // Handle touch events here...
    switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN: //first finger down only
            savedMatrix.set(matrix);

            if (draw) {
                startX = Math.abs((event.getX() + transX) / scale);
                startY = Math.abs((event.getY() + transY) / scale);
                drawBeacon();
            } else {
                ...
            }

            break;
        case MotionEvent.ACTION_UP: //first finger lifted
        case MotionEvent.ACTION_POINTER_UP: //second finger lifted
            ...
             break;
        case MotionEvent.ACTION_POINTER_DOWN: //second finger down
            ....
            break;
        case MotionEvent.ACTION_MOVE:
            ....
                break;
            }
    } //perform the transformation.

    view.setImageMatrix(matrix);
    return true; // indicate event was handled

}

1 个答案:

答案 0 :(得分:0)

最后自己做。我在我的方法

中创建了一个位图副本
 private void drawBeacon() {
    Bitmap tmpBitmap = Bitmap.createBitmap(copyOriginal);
    Canvas canvas = new Canvas(tmpBitmap);

    ....

    bitmapLayers.add(1, tmpBitmap);
    bt_delete.setVisibility(View.VISIBLE);
    imageView.setImageBitmap(tmpBitmap);
    draw = false;

}