I want to implement a crop feature, where I want to have a small rectangle over an imageView. The rectangle should be static and I want to move the image and get the image t
You need to put the drawing code in the onDraw()
method of the view for it to be shown. You should create a custom class that inherits from imageView, then override the onDraw()
method as below:
class DrawView extends ImageView {
public DrawView(Context context) {
super(context);
}
DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(10);
float leftx = 20;
float topy = 20;
float rightx = 50;
float bottomy = 100;
canvas.drawRect(leftx, topy, rightx, bottomy, paint);
}
}
Now in your layout, include DrawView
instead of your current ImageView