2011-02-07 121 views
23

我是新來的android編程,我試圖找出的是這個;Android:繪製畫布到ImageView

在我的佈局中,我有一個TextView,ImageView和Button,都是在一個垂直方向的LinearLayout上。

我希望能夠在ImageView中動態繪製圓,而不會影響我的佈局(textview/button)的其餘部分。我正在嘗試創建一個畫布,並在畫布中使用drawcircle函數來設置該圓的位置。然後以某種方式將該畫布繪製到我的圖像視圖中。我無法得到這個工作,這有什麼竅門嗎?或者我的方法根本錯誤?我將如何去繪圖圈到ImageView而不重新創建我的整個佈局?

謝謝!

回答

0

如果你有一個xml佈局,所有的元素都是垂直排列的。

您可以通過在擴展Class View並重寫其onDraw方法的軟件包中創建一個類來實現所需的功能。在你想要的地方畫圓圈。

然後,而不是在佈局中添加的ImageView在XML中添加這個您自己的看法layout.like以下

<的LinearLayout>

< TextView> < /TextView> 

    < Button> < /Button> 

    <com.prac.MyView> </ com.prac.MyView> 

< /的LinearLayout>

檢查以下鏈接2D圖形。它是一個偉大的教程閱讀。
http://organicandroid.blogspot.com/2010/08/starting-to-play-with-graphics.html
希望這有助於:)

+0

請更新的鏈接。博客被刪除 – 2013-08-20 08:56:09

0

有幾個方法可以做到你想要什麼,但使用的ImageView您所描述的方法是不是其中之一。一種可能是ImageView顯示動畫Drawable。然後,您可以專注於讓Drawable實現繪製圓圈。另一種方法是在每次要更改圖像並將ImageView設置爲顯示新的位圖時創建一個新的位圖。但是,通常的做法是創建一個自定義的View子類。示例項目API Demos有一個自定義視圖的示例,您可以在Google中找到大量教程。

26

我有同樣的挑戰,並得出結論認爲覆寫onDraw將至少在一般情況下不起作用。 My blog解釋原因。對我而言非常有效的是:

  1. 創建一個新的圖像位圖並附加一個全新的畫布。
  2. 將圖像位圖繪製到畫布中。
  3. 在畫布中繪製所有你想要的東西。
  4. 將畫布附加到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)); 
+0

謝謝,工作正常 – Lilo 2013-05-24 16:40:49

+5

只是一個建議,因爲我遇到了同樣的問題,而不是創建一個新的BitmapDrawable,我們也可以利用myImageView.setImageBitmap(tempBitmap)函數! – balachandarkm 2014-12-05 12:24:07

20
 ImageView imageView=(ImageView) findViewById(R.id.image); 
     Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  
     Canvas canvas = new Canvas(bitmap); 
     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     paint.setColor(Color.BLACK); 
     canvas.drawCircle(50, 50, 10, paint); 
     imageView.setImageBitmap(bitmap); 
2

我認爲,更好的方法是創建一個自定義的ImageView和重寫的onDraw方法。喜歡的東西:

public class CustomView extends ImageView { 

public CustomView(Context context) { 
    super(context); 
} 

public CustomView(Context context, AttributeSet attrst) { 
    super(context, attrst); 
} 

public CustomView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

MyBitmapFactory bitMapFac = null; 
public void setBitmapFactory(MyBitmapFactory bitMapFac) 
{ 
    this.bitMapFac = bitMapFac; 
} 

@Override 
public void onDraw(Canvas canvas) { 

    canvas.drawColor(Color.TRANSPARENT); 
    /*instantiate a bitmap and draw stuff here, it could well be another 
    class which you systematically update via a different thread so that you can get a fresh updated 
    bitmap from, that you desire to be updated onto the custom ImageView. 
    That will happen everytime onDraw has received a call i.e. something like:*/ 
    Bitmap myBitmap = bitMapFac.update(); //where update returns the most up to date Bitmap 
    //here you set the rectangles in which you want to draw the bitmap and pass the bitmap   
    canvas.drawBitmap(myBitMap, new Rect(0,0,400,400), new Rect(0,0,240,135) , null); 
    super.onDraw(canvas); 
    //you need to call postInvalidate so that the system knows that it should redraw your custom ImageView 
    this.postInvalidate(); 
} 
} 

這將是一個好主意,實施,檢查是否有新的位圖通過update()方法來獲取一些邏輯,讓裏面的onDraw代碼將不會執行每次和將系統開銷。

然後使用您的自定義視圖,無論你需要它。最簡單的方法是直接在activity_layout.xml內聲明它是這樣:

<com.mycustomviews.CustomView 
     android:id="@+id/customView" 
     android:layout_centerInParent="true" 
     android:layout_height="135dp" 
     android:layout_width="240dp" 
     android:background="@android:color/transparent"/> 

,然後訪問是在你的代碼像任何其他視圖通過使用:

customView = (CustomView) findViewById(R.id.customView);