Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically darken a View android

Tags:

android

I found how to change the opacity of a View, but I need to actually darken a View. My best idea is to put a transparent black rectangle over it and then slowly increase the opacity of the rectangle.

Do you know a nicer way to do it?

public class Page07AnimationView extends ParentPageAnimationView {
    private final String TAG = this.getClass().getSimpleName();
    private ImageView overlay;
    private int mAlpha = 0;

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

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

    protected void init()
    {
        overlay = new ImageView(mContext);
        overlay.setImageResource(R.drawable.black_background);
        overlay.setAlpha(0);
        overlay.setWillNotDraw(false);
        // make the PageAniSurfaceView focusable so it can handle events
        setFocusable(true);
    }

    protected void draw_bitmaps(Canvas canvas)
    {
        overlay.draw(canvas);
        update_bitmaps();
        invalidate();
    }

    public void update_bitmaps()
    {
        if(mAlpha < 250)
        {
            mAlpha += 10;
            overlay.setAlpha(mAlpha);
        }
    }
}

The code above isn't doing what I had hoped. Page07AnimationView is added to a FrameLayout over the view I need to darken. R.drawable.black_background points to a 787px x 492px black png image.

I added overlay.setWillNotDraw(false); but it didn't help. I changed the first setAlpha(0) to setAlpha(255) but that didn't help. I removed the setAlpha() calls altogether, but it didn't help.

This basic technique of adding a PageNNAnimationView has been working to draw Bitmaps, but not to draw ImageView overlay. (I would use Bitmaps, but they don't seem to have an alpha component.)

Edit2: this is the parent of the class above:

public class ParentPageAnimationView extends View {
    private final String TAG = this.getClass().getSimpleName();
    protected Context mContext;

    public ParentPageAnimationView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public ParentPageAnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    protected void init()
    {
    }

    protected void draw_bitmaps(Canvas canvas)
    {
        // will be overridden by child classes
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(this.getVisibility() == View.VISIBLE)
        {
            if(canvas != null)
            {
                draw_bitmaps(canvas);
            }
        }
    }

    public void update_bitmaps() 
    {
        // will be overridden by child classes
    }

    public void elementStarted(PageElement _pageElement) {
        // Nothing in parent class
    }

    public void elementFinished(PageElement mElement) {
        // Nothing in parent class
    }
}
like image 811
Thunder Rabbit Avatar asked Jul 05 '11 11:07

Thunder Rabbit


People also ask

How do I dim a view in Android?

In Android 13 and later versions, you can dim your phone's wallpaper at bedtime. Open your phone's Settings app. Tap Digital Wellbeing & parental controls. Tap Bedtime Mode.


2 Answers

In case of an ImageView, here's one way to achieve it:

imageView.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);
like image 101
android developer Avatar answered Oct 17 '22 17:10

android developer


I would rather do it in the opposite way - put a dark rectangle behind the view and set the view's opacity. This saves painting the rectangle when the view is 100% opaque.

like image 14
JBM Avatar answered Oct 17 '22 15:10

JBM