Draw rectangle Over ImageVIew

后端 未结 1 1047
面向向阳花
面向向阳花 2021-01-01 06:54

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

相关标签:
1条回答
  • 2021-01-01 07:33

    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

    0 讨论(0)
提交回复
热议问题