【发布时间】:2011-06-23 07:07:50
【问题描述】:
真的没有与setAlpha(int) 对应的 XML 属性吗?
如果没有,有什么替代方案?
【问题讨论】:
标签: android xml android-layout android-imageview alpha
真的没有与setAlpha(int) 对应的 XML 属性吗?
如果没有,有什么替代方案?
【问题讨论】:
标签: android xml android-layout android-imageview alpha
要降低 XML Android 中任何内容的不透明度,请使用 Alpha 属性。 示例:
android:alpha="0.6"
您必须输入介于 0.0 到 1.0 之间的值,以磅为单位。
【讨论】:
setAlpha(int) 自 API 起已弃用 16: Android 4.1
请改用setImageAlpha(int)
【讨论】:
不,没有,请参阅ImageView.setAlpha(int) 文档中的“相关 XML 属性”部分是如何缺失的。另一种方法是使用View.setAlpha(float),其XML counterpart 是android:alpha。它的范围是 0.0 到 1.0 而不是 0 到 255。使用它,例如喜欢
<ImageView android:alpha="0.4">
但是,后者仅在 API 级别 11 之后才可用。
【讨论】:
ImageView.setAlpha(int) 采用 int 而 android:alpha 采用浮动,所以严格来说后者不是前者的确切 XML 对应, 但它是 View.setAlpha(float) 的对应物。正如这里多次提到的,android:alpha / View.setAlpha(float) 仅在 API 级别 11 中可用。
使用 android:alpha=0.5 来实现 50% 的不透明度,并将 Android Material 图标从黑色变为灰色。
【讨论】:
我不确定 XML,但您可以通过以下方式通过代码来完成。
ImageView myImageView = new ImageView(this);
myImageView.setAlpha(xxx);
在 API 11 之前的版本中:
在 API 11+ 中:
【讨论】:
alpha 在各种尺寸、位置都有对应的 XML 属性时没有意义。
它比其他响应更容易。
有一个 xml 值 alpha 采用双精度值。
android:alpha="0.0" 那是不可见的
android:alpha="0.5"透视
android:alpha="1.0" 完全可见
这就是它的工作原理。
【讨论】:
setAlpha(float) 和 android:alpha。之前的 API 11 必须使用代码来设置图像的 alpha。正如 sschuberth 在上面的 anser 中所说的那样。
现在有一个 XML 替代方案:
<ImageView
android:id="@+id/example"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/example"
android:alpha="0.7" />
它是:android:alpha="0.7"
具有从 0(透明)到 1(不透明)的值。
【讨论】:
在古版android中使用这种形式。
ImageView myImageView;
myImageView = (ImageView) findViewById(R.id.img);
AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
alpha.setDuration(0);
alpha.setFillAfter(true);
myImageView.startAnimation(alpha);
【讨论】:
也许是纯色背景的有用替代方案:
在 ImageView 上放置一个 LinearLayout 并使用 LinearLayout 作为不透明度过滤器。下面是一个黑色背景的小例子:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000" >
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_stop_big" />
<LinearLayout
android:id="@+id/opacityFilter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CC000000"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
在 #00000000(完全透明)和 #FF000000 之间改变 LinearLayout 的 android:background 属性(完全不透明)。
【讨论】:
可以使用以下十六进制格式 #ARGB 或 #AARRGGBB 设置 alpha 和颜色。 见http://developer.android.com/guide/topics/resources/color-list-resource.html
【讨论】: