绘制矩形过的ImageView [英] Draw rectangle Over ImageVIew
问题描述
我想实现一个裁剪功能,在这里我想有一个小矩形了一个ImageView的。矩形应该是静态的,我想移动的图像,并获得图像要在矩形区域内冒出。然后取矩形作为裁剪后的图像内的图像。 我曾尝试创建使用位图作为参数画布,但它并没有奏效。我已经尝试了很多寻找如何做到这一点。但找不到它的任何地方。请帮助..
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);
我使用上述code,但没有绘制矩形的ImageView的。
I'm using the above code, but no rectangle is drawn on the imageView..
推荐答案
您需要把图纸code在视图中的的OnDraw()
方法它被证明。您应该创建一个继承自ImageView的自定义类,然后重写的OnDraw()
的方法如下:
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);
}
}
现在在你的布局,包括 drawView函数
,而不是你现在的的ImageView
Now in your layout, include DrawView
instead of your current ImageView
这篇关于绘制矩形过的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!