Java Android-自定义图像视图,勾选标记图像为选中状态

Java Android-自定义图像视图,勾选标记图像为选中状态,java,android,imageview,Java,Android,Imageview,对于我正在构建的Android应用程序,我创建了一个自定义ImageView类,该类需要在图像上显示一个勾号,指示该图像已被选中。 我从一个可绘制的资源中加载了tick图像,并将其显示在图像的顶部,效果很好。但我想做的是,当勾号可见时(即,当选择图像时),图像将变暗,以便您可以清楚地看到勾号。 我该怎么做 我目前的代码如下: public class TickedImageView extends ImageView { private boolean selected = true; pri

对于我正在构建的Android应用程序,我创建了一个自定义ImageView类,该类需要在图像上显示一个勾号,指示该图像已被选中。 我从一个可绘制的资源中加载了tick图像,并将其显示在图像的顶部,效果很好。但我想做的是,当勾号可见时(即,当选择图像时),图像将变暗,以便您可以清楚地看到勾号。 我该怎么做

我目前的代码如下:

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);
    }
}
}

谢谢。

如果我是你,我会使用两个图像,一个是浅色的,另一个是深色的,都来自
@drawable
文件夹,在所选项目上,我会调用深色图像

希望我的想法能给你答案

更新:

尝试使用Alpha属性(
yourpainobject.setAlpha(60);
)应该会对您有所帮助

差不多

paint.setAlpha(60);                    //you can set your transparent value here  
canvas.drawBitmap(tickBmp, x, y, paint);

已经看到了,所以在画布中,只需在原始图像的顶部绘制另一个带有深色和一点透明度的矩形。见下文

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);
        }
    }
}
您可以使用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)