要实现Android中的图片视图选择标记功能,可以使用以下解决方法:
public class MarkedImageView extends ImageView {
private boolean isMarked;
public MarkedImageView(Context context) {
super(context);
isMarked = false;
}
public MarkedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
isMarked = false;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isMarked) {
// 绘制标记
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, Math.min(getWidth(), getHeight()) / 2, paint);
}
}
public void setMarked(boolean marked) {
isMarked = marked;
invalidate(); // 重新绘制视图
}
}
MarkedImageView imageView = findViewById(R.id.image_view);
imageView.setMarked(true); // 设置为已标记
这样,当调用setMarked(true)方法时,MarkedImageView就会在图片上绘制一个红色的圆形标记。
下一篇:Android 图像拉伸问题