2013-06-18 3 views
0

Ceci est mon code pour dessiner un rectangle sur une image dans la position spécifiée. le rectangle est dessiné mais mon problème est que maintenant mon image n'est pas montrée.Problème d'image bitmap 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); 

S'il vous plaît dire quelque chose de mal sur mon code?

+0

mon erreur ... changé – user2291423

+0

semble juste maintenant! –

+0

une idée de ce qui ne va pas ici ?? – user2291423

Répondre

1

Vous avez défini StrokeWidth-50. ce qui est très énorme. Soit l'enlever ou diminuer. Sinon, le code est bien pour dessiner un rectangle.

J'ai essayé le code suivant.

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); 

Modifier

Vous pouvez essayer la mise en page suivantes

<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> 

Dans lequel, deux points de vue sont RelativeLayout. L'un est l'affichage d'image, l'autre est votre vue personnalisée qui est utilisée pour dessiner des rectangles. Votre ImageView sera chevauché avec le second. La deuxième vue a la transparence. Ainsi, vous aurez l'air de dessiner un rectangle sur l'image originale.

+0

j'ai essayé ceci mais encore il me cache mon image .. – user2291423

+0

pourquoi n'utilisez-vous pas une autre vue pour dessiner un rectangle avec la transparence? ou définir votre image en arrière-plan au lieu de la source d'imageview –

+0

@chinthan réellement je veux, comme dans Facebook tag une partie des photos – user2291423

2

essayer cette

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

Salut essayé ceci mais sur la première ligne en changeant à mon image montre l'erreur .. :( – user2291423

+0

Je pensais que vous devez dessiner un rectangle sur une image, ainsi vous référez la réponse de @ Arun –

0

vous devez faire d'abord l'image mutable puis dessinez un rectangle sur lui et faire gagner immutable-

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

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