以编程方式使查看android变暗

Thu*_*bit 19 android

我发现如何改变a的不透明度View,但我需要实际上变暗a View.我最好的想法是在它上面放一个透明的黑色矩形,然后慢慢增加矩形的不透明度.

你知道更好的方法吗?

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码没有做我所希望的. Page07AnimationView添加到FrameLayout视图中我需要变暗. R.drawable.black_background指向787px x 492px黑色png图像.

我补充说overlay.setWillNotDraw(false);但没有帮助.我改变了第一个setAlpha(0),setAlpha(255)但没有帮助.我setAlpha()完全取消了电话,但没有帮助.

这种添加a的基本技术PageNNAnimationView一直在绘制Bitmaps,而不是绘制ImageView overlay.(我会使用Bitmaps,但它们似乎没有alpha组件.)

Edit2:这是上面类的父级:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

and*_*per 29

在ImageView的情况下,这是实现它的一种方法:

imageView.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)


JBM*_*JBM 11

我宁愿以相反的方式做 - 在视图后面放一个黑色矩形并设置视图的不透明度.当视图100%不透明时,这可以节省绘制矩形.


Ash*_*lak 6

我会做这样的事情:

view.getBackground().setColorFilter(color, PorterDuff.Mode.DARKEN);
Run Code Online (Sandbox Code Playgroud)

0x7f000000对于典型的变暗,使用黑色和一些alpha .

它更简洁,你也可以View用动画或滚动事件变暗.只需设置Color.argb(alpha, 0, 0, 0)为颜色和动画alpha,或根据滚动偏移更改它.