我有一个加载可绘制图像的图像视图。 imageview设置为wrap_content,但由于原始图像很大,因此它会填充根布局。
我在ORGINAL图像上有一个围绕一个面的Rect的像素值。 当我将此图像加载到imageview时,我想以相同的方式在脸部周围绘制一个矩形。 所以基本上我想将位图像素转换为imageview像素。
After researching, I am using this code:
// Where the rct is on original bitmap/image
Rect rct= new Rect(200,500,300,700);
//get Bitmap
Bitmap bmp = ((BitmapDrawable)ivPicture.getDrawable()).getBitmap();
//Calculate the scaling ratio
double bmpWidthRatio = bmp.getWidth()/(double)ivPicture.getWidth();
double bmpHeightRatio = bmp.getHeight()/(double)ivPicture.getHeight();
//create new image view so I can draw the rect in the canvas
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.MATCH_PARENT);
MyView v = new MyView(this);
v.setLayoutParams(lp);
v.rect.set((int)(rct.left/bmpWidthRatio),(int)( rct.top/bmpHeightRatio) ,
(int)(rct.right/bmpWidthRatio),(int)( rct.bottom/bmpHeightRatio));
这是用于在Rect
上绘制的自定义imageview的代码public class MyView extends View {
public Rect rect = new Rect(0,0,0,0);
public MyView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint bracketsPaint = new Paint();
bracketsPaint.setColor(Color.YELLOW);
bracketsPaint.setStyle(Paint.Style.STROKE);
bracketsPaint.setStrokeWidth(10);
canvas.drawRect(rect, bracketsPaint);
}
}
我对这段代码不理解?我的意思是它看起来像是在正确的X值处绘制的,但不知何故,Y值被搞砸了。
这不是你如何将原始位图像素转换为同一位置的imageview像素吗?
由于