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.
mon erreur ... changé – user2291423
semble juste maintenant! –
une idée de ce qui ne va pas ici ?? – user2291423