Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式使视图变暗_Android - Fatal编程技术网

Android 以编程方式使视图变暗

Android 以编程方式使视图变暗,android,Android,我找到了方法,但实际上我需要使视图变暗。我最好的办法是在上面放一个透明的黑色矩形,然后慢慢增加矩形的不透明度 你知道更好的方法吗 public class Page07AnimationView extends ParentPageAnimationView { private final String TAG = this.getClass().getSimpleName(); private ImageView overlay; private int mAlpha

我找到了方法,但实际上我需要使
视图变暗。我最好的办法是在上面放一个透明的黑色矩形,然后慢慢增加矩形的不透明度

你知道更好的方法吗

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

我宁愿用相反的方法来做——在视图后面放置一个黑色矩形,并设置视图的不透明度。这将在视图100%不透明时保存绘制矩形。

您可以尝试像这样使用Alpha动画(可能在矩形上):


这将导致矩形在350秒内逐渐变为不透明…

您应该查看iPaulPro的答案。您需要扩展
ImageView
并重写
onDraw()
方法

取决于你要做什么,亚历山德鲁·克里斯特斯库的回答也是有效的,但你应该这样做
调用动画在完成后保持不变。

我就是这样做的。关键是使用一种阿尔法设置为我想要的任何颜色的颜料

public class Page07AnimationView extends ParentPageAnimationView {
    private final String TAG = this.getClass().getSimpleName();
    private Bitmap bitmap;
    private BitmapDrawable drawable;
    private ImageView overlay;
    private int which = -1;
    private long last_time;
    private Page07State state;
    private int mAlpha;
    private int maxAlpha;
    private Paint mPaint;
    private int _alpha_step;
    private int minAlpha;

    public enum Page07State {
        WAITING, DARKENING, DARKENED
    }

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

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

    protected void init()
    {
        minAlpha = 0;
        mAlpha = minAlpha;
        _alpha_step = 5;
        maxAlpha = 255;
        mPaint = new Paint();
        mPaint.setAlpha(minAlpha);
        state = Page07State.WAITING;
        overlay = new ImageView(mContext);
        overlay.setImageResource(R.drawable.black_background);
        drawable = (BitmapDrawable) overlay.getDrawable();
        bitmap = drawable.getBitmap();
        last_time = 0;
    }

    protected void draw_bitmaps(Canvas canvas)
    {
        if(state != Page07State.WAITING)
        {
            DebugLog.d(TAG, "drawing " + Integer.toString(which));
            canvas.drawBitmap(bitmap, 0, 0, mPaint);
        }
        update_bitmaps();
        invalidate();
    }

    public void update_bitmaps()
    {
        if(state == Page07State.DARKENING)
        {
            if(mAlpha < maxAlpha)
            {
                if(System.currentTimeMillis() > last_time + 12)
                {
                    last_time = System.currentTimeMillis();
                    mAlpha += _alpha_step;
                    mPaint.setAlpha(mAlpha);
                }
            }
            else
            {
                state = Page07State.DARKENED;
            }
        }
    }

    public void runAnimation()
    {
        state = Page07State.DARKENING;
    }
}
public类Page07AnimationView扩展了ParentPageAnimationView{
私有最终字符串标记=this.getClass().getSimpleName();
私有位图;
私有位图可绘制;
私有图像视图覆盖;
私有int,它=-1;
私人长假;
私人和国家;
私家医院;
私有int-maxAlpha;
私人油漆;
私有整数α步;
米纳尔法私人酒店;
公共枚举Page07State{
等待,黑暗,黑暗
}
公共页面07动画视图(上下文){
超级(上下文);
}
公共页面07动画视图(上下文、属性集属性){
超级(上下文,attrs);
}
受保护的void init()
{
minAlpha=0;
马尔法=米纳尔法;
_alpha_阶跃=5;
maxapha=255;
mPaint=新油漆();
mPaint.setAlpha(米纳尔法);
state=Page07State.WAITING;
覆盖=新图像视图(mContext);
覆盖.setImageResource(R.drawable.black_背景);
drawable=(BitmapDrawable)overlay.getDrawable();
bitmap=drawable.getBitmap();
最后一次=0;
}
受保护的void draw_位图(画布)
{
if(state!=Page07State.WAITING)
{
DebugLog.d(标记“drawing”+Integer.toString(which));
drawBitmap(位图,0,0,mPaint);
}
更新位图();
使无效();
}
公共无效更新\u位图()
{
if(state==Page07State.darking)
{
if(mAlpha上次时间+12)
{
last_time=System.currentTimeMillis();
mAlpha+=\uα\u阶跃;
mPaint.setAlpha(马尔法);
}
}
其他的
{
state=Page07State.darked;
}
}
}
public void runAnimation()
{
state=Page07State.darking;
}
}

对于ImageView,这里有一种实现方法:

imageView.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);

我会这样做:

view.getBackground().setColorFilter(color, PorterDuff.Mode.DARKEN);
GradientDrawable gd = (GradientDrawable) textView.getBackground();
gd.setColor(color); //you can also set BG color to a textview like this
gd.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);
使用黑色和一些类似alpha的
0x7f000000
进行典型的变暗


它更简洁,您还可以使用动画或滚动事件等使
视图变暗。只需将
Color.argb(alpha,0,0,0)
设置为颜色并设置动画
alpha
,或者根据滚动偏移量更改它。

添加到android开发者的答案中:

imageView.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);
您可以在任何视图上设置ColorFilter,如下所示:

view.getBackground().setColorFilter(color, PorterDuff.Mode.DARKEN);
GradientDrawable gd = (GradientDrawable) textView.getBackground();
gd.setColor(color); //you can also set BG color to a textview like this
gd.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);

安卓系统实际上公开了一个可以用来使视图变暗的绘图工具。您可以使用覆盖轻松地将其附着到任何视图

这里有两个扩展函数,可用于使任何视图变暗

fun View.darken() {
    val darkOverlay = ResourcesCompat.getDrawable(
            resources,
            android.R.drawable.screen_background_dark_transparent,
            context.theme
    )!!.mutate() // We mutate the drawable so we can later implement a fade in/out animation and animate the Drawable's alpha property. Since Drawables share their state we need to mutate otherwise we would impact all instances of this drawable
    darkOverlay.setBounds(0, 0, width, height)
    setTag(R.id.dark_overlay, darkOverlay)
    overlay.add(darkOverlay)
}

fun View.lighten() {
    (getTag(R.id.dark_overlay) as? Drawable)?.let {
        overlay.remove(it)
        setTag(R.id.dark_overlay, null)
    }
}
确保将id添加到
ids.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="dark_overlay" type="id" />
</resources>
<style name="BaseTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- required for api 29 otherwise the system will set a white background color to the NavigationBar to ensure the buttons are visible -->
    <item name="android:enforceNavigationBarContrast">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>

这是个好主意!不过,我想我不得不在视图上做一些“覆盖”的事情,因为我的布局是如何布置的。但在它们后面有一个动画布局可能会解决另一个问题;谢谢在半透明视图后面运行动画时,可能会触发重新绘制整个屏幕。在模拟器的DevTools中启用“Show screen updates”(显示屏幕更新)并查看重新绘制的内容。我认为,当他说得更好时,他的意思是“更合适”,而不是“更可爱”,我已经确认draw_位图和update_位图正在被反复调用。这是否会占用更多或更少的资源,然后设置
。setAlpha((float)1.0)
(适用于sdk 11及更高版本)或
。setAlpha(100)
(适用于sdk 11及以下版本)。我问的原因是因为我设置了不透明度来实现这一点,如果我可以调用它,如果它更兼容,那就太好了。@AaronRussell我认为如果你使用setAlpha,它会更好,因为它可能是GPU优化的。当然,我可能在这一点上是错的。最好的事情应该是始终使用最新的API函数。这种方法现已弃用,但您可以使用
ColorMatrixColorFilter
PorterDuffColorFilter