Dessiner un rectangle sur une vue ImageView
Bonjour à tous,
Je rencontre un souci.
Je voudrai dessiner un rectangle (dessiné par le click de l'utilisateur) sur une image.
Alors j'ai sous-classé la classe Imageview:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public class ImageViewSelection extends AppCompatImageView {
private final Paint paint =
new Paint();
{
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
}
private Rect rect = null;
private boolean isDrawingRect = false;
...
public void setDrawingRectState(boolean isDrawingRect) {
this.isDrawingRect = isDrawingRect;
if(isDrawingRect) {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawRect(rect, paint);
this.draw(canvas);
}
}
private boolean isDrawingRect() {
return isDrawingRect;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(isDrawingRect() && rect != null) {
canvas.drawRect(rect, paint);
}
} |
Et dans l'activité principale, je fais un appel à
Code:
1 2 3 4 5 6 7
| System.out.println("2 points sélectionnés A et B");
ImageViewSelection viewById = findViewById(R.id.currentImageView);
viewById.setDrawingRect(new Rect((int)drawPointA.getX(),(int)drawPointA.getY(),
(int)drawPointB.getX(),(int)drawPointB.getY()));
viewById.setDrawingRectState(true);
System.out.println(
viewById.getDrawingRect().toString()); |
Le rectangle n'est pas dessiné. Je ne sais pas quoi faire, j'ai l'impression de faire du Java 1.0.