0
我有一個活動,使用畢加索圖書館從URL加載圖像。我的位圖變成黑色canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.MULTIPLY)Android
該圖像比屏幕大,這就是爲什麼在「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(){
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
}