我使用畫布在位圖上繪製並設置在ImageView上覆蓋的位圖。Android畫布不以另一種方法在位圖上繪製
我在drawGraph()方法中繪製了一個基本繪圖,當用戶在畫布上觸摸時,我必須在那裏繪製一個圓,爲此我在canvas內部使用了onTouchEvent()方法,它不繪製任何東西,代碼如下,問題是什麼,如何解決這個問題。
我也試圖用直線創建另一個位圖,在drawGraph()和checkClicked()的末尾將新位圖設置爲imageView。它顯示了新的位圖(繪製了一條直線)在開始時正確設置爲imageView,但是當單擊imageView時,空白位圖(未繪製直線)被設置爲imageView。所以我相信這是畫布繪製在checkClicked()中不起作用。
感謝您提前幫助我!
ImageView imageView;
Paint p = new Paint();
Bitmap myBitmap;
Bitmap workingBitmap;
Bitmap mutableBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_drawpanel);
workingBitmap = Bitmap.createBitmap(myBitmap);
mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
imageView = (ImageView) findViewById(R.id.image1);
imageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int touchX = (int) (event.getX() + imageView.getX());
int touchY = (int) (event.getY() + imageView.getY());
checkClicked(touchX, touchY);
return true;
}
});
drawGraph();
}
public void drawGraph(){
p.setAntiAlias(true);
p.setColor(Color.BLACK);
p.setStyle(Style.FILL_AND_STROKE);
p.setStrokeWidth(5);
Canvas canvas = new Canvas(mutableBitmap);
//basic drawing is successfully drawn here
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
}
public void checkClicked(int x, int y){
p.setAntiAlias(true);
p.setColor(Color.RED);
p.setStyle(Style.FILL_AND_STROKE);
Canvas canvas = new Canvas(mutableBitmap);
//canvas doesn't draw a circle here
canvas.drawCircle(x, y, 10, p);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
我確實轉換了座標: int a =(int)(drawPanel.getX()+ x); int b =(int)(drawPanel.getY()+ myBitmap.getHeight() - y); 它工作正常,在功能drawGraph(),同樣的規則適用於功能checkClicked()畫什麼(或其他地方) 待辦事項drawPanel.getX()和drawPanel.getY()函數之間的變化? 請幫我解決這個問題!謝謝! – philwu
當點擊imageView時,我調用函數drawGraph()。 發生了奇怪的事情,drawGraph()重繪了imgaeView範圍內的基本繪圖,因爲我只能看到繪圖的上半部分... – philwu