【发布时间】:2012-03-13 21:06:05
【问题描述】:
我能否以编程方式设置 ImageView 的 alpha 而无需依赖于引入 setAlpha 的 API 级别 11?
【问题讨论】:
-
哦.. 在 ImageView 中还有一个 setAlpha(int alpha) 以及从 API 级别 1 工作的 :)
我能否以编程方式设置 ImageView 的 alpha 而无需依赖于引入 setAlpha 的 API 级别 11?
【问题讨论】:
ImageView 自 API 级别 1 起就有一个方法 setAlpha(int)。因此您可以在任何 API 级别使用它。
就是API level 11中引入的View的setAlpha(float)方法。
【讨论】:
setImageAlpha(X) >=16!
我使用代码来设置图像本身的Alpha,而不是视图。这在 API 级别 1 中可用..
public void toggleButton(int i) {
if (indImageBtnEnabled[i]) {
int di = getDrawableId(findViewById(myImagebtns[i]));
Drawable d = this.getResources().getDrawable(di);
d.setAlpha(25);
((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);
indImageBtnEnabled[i] = false;
} else {
// findViewById(myImagebtns[i]).setAlpha(1f) << NEEDS API11;
int di = getDrawableId(findViewById(myImagebtns[i]));
Drawable d = this.getResources().getDrawable(di);
d.setAlpha(255);
((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);
indImageBtnEnabled[i] = true;
}
}
【讨论】:
我认为(但不确定)您可以将 alpha 动画应用到您的 ImageView,并且只要动画上的 fillAfter() 设置为 true,Image 应该保持在它结束的任何 alpha 级别在。
【讨论】:
您可以仅使用您的图像创建新布局,设置布局所需的 alpha,将其设置为叠加层并在需要时膨胀。
LayoutInflater inflater = LayoutInflater.from(this);
View overView = inflater.inflate(R.layout.myAlpahImageLayout, null);
this.addContentView(overView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/myImage1" />
</RelativeLayout>
【讨论】: