对于我正在构建的Android应用,我制作了一个自定义的ImageView类,需要在图像上显示勾号,表示已选择此图像。 我从一个可绘制的资源加载了刻度图像并将其显示在图像的顶部,它工作正常。但我想做的是,当勾选可见时(即选择图像时),图像变暗,以便您可以清楚地看到勾号。 我怎么能这样做?
我的代码目前如下:
public class TickedImageView extends ImageView {
private boolean selected = true;
private Bitmap tickBmp;
private Paint paint = new Paint();
public TickedImageView(Context context) {
super(context);
tickBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_done_white_48dp);
}
public TickedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
tickBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_done_white_48dp);
}
public void setSelected(boolean selected) {
this.selected = selected;
invalidate();
}
public boolean isSelected() {
return selected;
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
if(selected) {
int margin = 15;
int x = (canvas.getWidth() / 2) - (tickBmp.getWidth() / 2) - margin;
int y = (canvas.getHeight() / 2) - (tickBmp.getHeight() / 2) - margin;
canvas.drawBitmap(tickBmp, x, y, paint);
}
}
}
谢谢。
答案 0 :(得分:0)
如果我是你,我会使用2张图片,一张浅色,另一张较暗的图片,都来自@drawable
文件夹,而且在选定的项目上,我会调用较暗的图像。
希望我的想法能给你答案
更新:
尝试使用Alpha属性(yourPaintObject.setAlpha(60);
)来帮助你
类似
paint.setAlpha(60); //you can set your transparent value here
canvas.drawBitmap(tickBmp, x, y, paint);
在SO
上看到此answer答案 1 :(得分:0)
在画布中,只需在原始图像上绘制另一个深色和透明度的矩形。见下文
public class TickedImageView extends ImageView {
private boolean selected = true;
private Bitmap tickBmp;
private Paint paint;
private Paint mDarkerPaint;
private int measuredWidth, measuredHeight;
public TickedImageView(Context context) {
super(context);
init();
}
public TickedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint = new Paint();
mDarkerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mDarkerPaint.setStyle(Paint.Style.FILL);
// Keep changing this color till it looks ok for you
mDarkerPaint.setColor(0x80142030);
tickBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_done_white_48dp);
}
public void setSelected(boolean selected) {
this.selected = selected;
invalidate();
}
public boolean isSelected() {
return selected;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
setMeasuredDimension(measuredWidth, measuredHeight);
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
if (selected) {
int margin = 15;
int x = (canvas.getWidth() / 2) - (tickBmp.getWidth() / 2) - margin;
int y = (canvas.getHeight() / 2) - (tickBmp.getHeight() / 2) - margin;
canvas.drawRect(0, 0, measuredWidth, measuredHeight, mDarkerPaint);
canvas.drawBitmap(tickBmp, x, y, paint);
}
}
}
答案 2 :(得分:0)
你可以使用setAlpha(float), 0.0 - >不透明的1.0 - >完全可见
TickeredImageView imageView = new TickeredImageView(this);
imageView.setAlpha((float) desired_opacity)
如果使用APK 16+,您可以使用setImageAlpha(int):
imageView.setImageAlpha((int) desired_opacity)