我需要为用户在ImageView上接触的区域设置白色。如果我setOnTouchListener到ImageView并得到触摸的x和y位置,我如何在ImageView中更改适当的像素值?还是有更好的解决方案?
发布于 2015-09-24 07:12:05
我认为最简单的解决方案是扩展ImageView
。
下面是一个简单的例子,它画了一个环绕触点区域的黑色圆圈:
class TouchableImageView extends ImageView {
private float x, y;
private Paint paint;
public TouchableImageView(Context context) {
super(context);
init();
}
public TouchableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TouchableImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TouchableImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
void init() {
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = event.getX();
y = event.getY();
invalidate();
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Just for example - draw a circle around touch area:
if(x!=0 || y!=0)
canvas.drawCircle(x, y, 25, paint);
}
}
编辑:
如果您想要将结果保存为位图-您需要更多的步骤,如描述的here和here。
简而言之,您应该遵循以下步骤:
new Canvas(bitmap)
创建新画布https://stackoverflow.com/questions/32763430
复制