gpt4 book ai didi

android - 如何在Imageview上画一个黑色边框背景透明的矩形?

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:16 26 4
gpt4 key购买 nike

我想在 ImageView 上绘制一个矩形,如下图所示(黑色边框和透明背景)。基本上,我下载一个图像,放入一个 ImageView 中,然后在我收到 4 个点后绘制一个矩形。提前致谢

enter image description here

最佳答案

android.view.InflateException 的问题可以通过从 DrawView 类中删除构造函数并重新自动生成它们来解决。现在对于矩形,您必须像这样使用 onDraw:

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

Paint paint = new Paint();
paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.FILL);
float leftx = 50;
float topy = 50;
float rightx = 150;
float bottomy = 150;
// FILL
canvas.drawRect(leftx, topy, rightx, bottomy, paint);

paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
// BORDER
canvas.drawRect(leftx, topy, rightx, bottomy, paint);
}

如果你想在点击时获取坐标然后绘制矩形覆盖onTouchEvent方法并做这样的事情

class DrawView extends ImageView {

float x, y;
float width = 60.0f;
float height = 50.0f;
boolean touched = false;

public DrawView(Context context) {
super(context);
}

public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);


if (touched) {
Paint paint = new Paint();
paint.setColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.FILL);
// FILL
canvas.drawRect(x, y, (x + width) / 0.5f, (y + height) / 0.5f, paint);

paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
// BORDER
canvas.drawRect(x, y, (x + width) / 0.5f, (y + height) / 0.5f, paint);
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
touched = true;
//getting the touched x and y position
x = event.getX();
y = event.getY();
invalidate();
return true;
}

}

关于android - 如何在Imageview上画一个黑色边框背景透明的矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com