2013-06-18 139 views
0

這是我在指定位置的圖像上繪製矩形的代碼。矩形繪製,但我的問題是,現在我的圖像不顯示。位圖圖像問題android

Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
imageView.setImageBitmap(bitmap); 

Paint paint = new Paint(); 
paint.setColor(Color.RED); 
paint.setStyle(Paint.Style.FILL_AND_STROKE); 
paint.setStrokeWidth(50); 
float left = 20; 
float topy = 20; 
float right = 50; 
float bottom = 50; 
canvas.drawRect(left, topy, right, bottom, paint); 

請告訴我的代碼有什麼問題嗎?

+0

我誤改... – user2291423

+0

現在看起來公平! –

+0

任何想法這裏錯了什麼? – user2291423

回答

1

您已設置StrokeWidth50。這是非常巨大的。刪除它或減少。否則,代碼可以很好地繪製矩形。

我試過下面的代碼。

Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
button.setImageBitmap(bitmap); 

Paint paint = new Paint(); 
paint.setColor(Color.BLACK); // canvas background fill 
canvas.drawPaint(paint); // just to check how big rectangle draw on canvas 
    //you can remove 2 above lines. its my testing 
paint.setColor(Color.RED); 
paint.setStyle(Paint.Style.FILL_AND_STROKE); 
paint.setStrokeWidth(5); //5 instead of 50 
float left = 20; 
float topy = 20; 
float right = 50; 
float bottom = 50; 
canvas.drawRect(left, topy, right, bottom, paint); 

編輯

你可以嘗試以下佈局

<LinearLayout 
    android:id="@+id/linearParentHolder" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:gravity="center" > 

    <RelativeLayout 
     android:id="@+id/relativeHolder" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/imgMainImage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/app_name" 
      android:scaleType="fitCenter" /> 

     <com.customview.CustomRectangleOverlayView 
      android:id="@+id/photoSortrView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/imgMainImage" 
      android:layout_alignLeft="@+id/imgMainImage" 
      android:layout_alignRight="@+id/imgMainImage" 
      android:layout_alignTop="@+id/imgMainImage" /> 
    </RelativeLayout> 
</LinearLayout> 

其中,兩種觀點都在RelativeLayout的。一個是圖像視圖,第二個是用於繪製矩形的自定義視圖。您的ImageView將與第二個重疊。第二視圖具有透明度。所以,這看起來像是在原始圖像上繪製矩形。

+0

我試過這個,但它仍然隱藏我的image – user2291423

+0

爲什麼你不使用另一種視圖來繪製矩形透明度?或者在背景中設置你的圖像而不是imageview的來源 –

+0

@chinthan其實我想要的,就像在Facebook上標記一些照片的一部分 – user2291423

2

試試這個

Bitmap bm = new BitmapFactory().decodeResource(getResources(), your_image_id, Bitmap.Config.ARGB_8888); 
    Bitmap bitmap = Bitmap.createBitmap(bm); 
    Canvas canvas = new Canvas(bitmap); 


    Paint paint = new Paint(); 
    paint.setColor(Color.RED); 
    paint.setStyle(Paint.Style.FILL_AND_STROKE); 
    paint.setStrokeWidth(50); 
    float left = 20; 
    float topy = 20; 
    float right = 50; 
    float bottom = 50; 
    canvas.drawRect(left, topy, right, bottom, paint); 

imageView.setImageBitmap(bitmap); 
+0

您好,但在第一行,當改變到我的形象顯示錯誤.. :( – user2291423

+0

我以爲你需要在圖像上畫一個矩形,所以你引用@ Arun的答案。 –

0

你需要做的圖像第一可變的,然後在其上繪製矩形,並獲得使其immutable-

bitmap3=mutableimage3.copy(Bitmap.Config.ARGB_4444,true);// mutable image. 

bitmap3=mutableimage3.copy(Bitmap.Config.ARGB_4444,false);//immutable