Android 台灣中文網

標題: 如何在imageview上畫圖 [打印本頁]

作者: 30cm    時間: 2013-4-11 19:36
標題: 如何在imageview上畫圖
如題
在程式中載入圖片並用ImageView顯示
但後面想讓使用者能在上面畫畫加一些東西
那如何在上面畫畫
當然我知道要用Canvas和Paint來實現
然後重作onDraw的方法


作者: iamjason008    時間: 2013-4-15 15:58
參考一下這裡
http://www.eoeandroid.com/thread-264349-1-1.html

1.Create a new image bitmap and attach a brand new canvas to it so that the bitmap and the canvas use the same coordinate system.
2.Draw the image bitmap into the canvas.
3.Draw everything else you want into the canvas (the rectangles in my case).
4.Attach the canvas to the ImageView.

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;

ImageView myImageView = ...
Bitmap myBitmap = ...
Paint myRectPaint = ...
int x1 = ...
int y1 = ...
int x2 = ...
int y2 = ...

//Create a new image bitmap and attach a brand new canvas to it
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);

//Draw the image bitmap into the cavas
tempCanvas.drawBitmap(myBitmap, 0, 0, null);

//Draw everything else you want into the canvas, in this example a rectangle with rounded edges
tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, myPaint);

//Attach the canvas to the ImageView
myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));






歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1