在ImageVIew上绘制矩形[英] Draw rectangle Over ImageVIew

本文是小编为大家收集整理的关于在ImageVIew上绘制矩形的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想实现一个裁剪功能,我想在图像视图上有一个小矩形.矩形应该是静态的,我想移动图像并将图像在矩形区域内裁剪.然后作为裁剪图像中的矩形中获取图像. 我尝试使用Bitmap作为参数创建画布,但它无效.我已经尝试了很多搜索方法.但找不到任何地方.请帮助..

Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.indoor);

    Bitmap mutBitmap = Bitmap.createBitmap(200, 400,bitmap.getConfig());

    Canvas canvas = new Canvas(mutBitmap);

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

我正在使用上面的代码,但是在映像视图上没有绘制矩形.

推荐答案

您需要将绘图代码放在视图的onDraw()方法中,以显示它.您应该创建一个从ImageView继承的自定义类,然后覆盖onDraw()方法如下:

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

现在在您的布局中,包括DrawView而不是当前ImageView

本文地址:https://www.itbaoku.cn/post/102552.html

问题描述

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 to be cropped within the rectangular area. Then fetch the image within the rectangle as an cropped image. I have tried creating a canvas using Bitmap as a parameter but it doesn't worked. I have tried a lot to search how to do this. but couldn't find it anywhere. Please help..

Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.indoor);

    Bitmap mutBitmap = Bitmap.createBitmap(200, 400,bitmap.getConfig());

    Canvas canvas = new Canvas(mutBitmap);

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

I'm using the above code, but no rectangle is drawn on the imageView..

推荐答案

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