java 设置布局的 Alpha/不透明度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4813995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:08:38  来源:igfitidea点击:

Set Alpha/Opacity of Layout

javaandroiduser-interface

提问by Tsuma-Shifu

Is it possible to set and get the Alpha/Opacity of a Layout and all it's child views? I'm not talking about the background. Say a collection of Video Controls like Play, Pause and Progressbar in a Relative Layout.

是否可以设置和获取布局及其所有子视图的 Alpha/Opacity?我不是在谈论背景。在相对布局中说一组视频控件,如播放、暂停和进度条。

I can use animation to fade in and out but wanted to know if there was a direct method I could use.

我可以使用动画淡入淡出,但想知道是否有我可以使用的直接方法。

回答by Alex Orlov

You can set the alpha on the layout and it's children (or any other view for that matter) using AlphaAnimation with 0 duration and setFillAfter option.

您可以使用具有 0 持续时间和 setFillAfter 选项的 AlphaAnimation 在布局上设置 alpha 及其子项(或任何其他视图)。

Example:

例子:

AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
alpha.setDuration(0); // Make animation instant
alpha.setFillAfter(true); // Tell it to persist after the animation ends
// And then on your layout
yourLayout.startAnimation(alpha);

You can use one animation for multiple components to save memory. And do reset() to use again, or clearAnimation() to drop alpha.

您可以为多个组件使用一个动画以节省内存。并执行 reset() 以再次使用,或执行 clearAnimation() 以删除 alpha。

Though it looks crude and hacked it's actually a good way to set alpha on set ov views that doesn't take much memory or processor time.

尽管它看起来很粗糙并且被黑了,但它实际上是一种在 set ov 视图上设置 alpha 的好方法,它不会占用太多内存或处理器时间。

Not sure about getting current alpha value though.

虽然不确定是否获得当前的 alpha 值。

回答by Takhion

Here is a method that works across all versions (thanks to @AlexOrlov):

这是一种适用于所有版本的方法(感谢@AlexOrlov):

@SuppressLint("NewApi")
public static void setAlpha(View view, float alpha)
{
    if (Build.VERSION.SDK_INT < 11)
    {
        final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
        animation.setDuration(0);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }
    else view.setAlpha(alpha);
}

Clearly, for API less than 11 if you can you should create a custom view that calls Canvas.saveLayerAlpha()before drawing its children (thanks to @RomainGuy).

显然,对于小于 11 的 API,如果可以的话,您应该创建一个Canvas.saveLayerAlpha()在绘制其子项之前调用的自定义视图(感谢 @RomainGuy)。

回答by Romain Guy

Alex's solution works but another way is to create a custom view that calls Canvas.saveLayerAlpha() before drawing its children. Note that in Android 3.0 there is a new View.setAlpha() API :)

Alex 的解决方案有效,但另一种方法是创建一个自定义视图,在绘制其子项之前调用 Canvas.saveLayerAlpha()。请注意,在 Android 3.0 中有一个新的 View.setAlpha() API :)

回答by Firstborn

Now you can use ViewCompat.setAlpha(View, float)for this action.

现在您可以使用ViewCompat.setAlpha(View, float)此操作。

回答by vincent091

Sorry for my answer but if you want the alpha you can use the ViewCompat Class

抱歉我的回答,但如果你想要 alpha 你可以使用 ViewCompat 类

ViewCompat.setAlpha(View view, float alpha)

ViewCompat.setAlpha(视图视图,浮动alpha)

ViewCompat is in the appcompat v4

ViewCompat 在 appcompat v4 中