2011-02-08 60 views

回答

38

我不确定XML,但可以通过以下方式通过代码实现。

ImageView myImageView = new ImageView(this); 
myImageView.setAlpha(xxx); 

在预API 11:

  • 范围是从0到255(含),0是透明和255是不透明的。

在API 11+:

  • 范围为0F至1F(含),0F是透明的和图1F是不透明的。
+2

是的,我知道。 (我希望这个问题隐含在这个问题中。)XML的一点是删除一些这样的代码。这对我来说没有意义,为什么`alpha`在各种大小,位置都没有XML属性对应的时候。 – SK9 2011-02-08 09:31:44

+0

我想知道:为什么不推荐使用?是因为现在他们有一个浮点参数? – 2013-10-03 10:58:40

+0

是的,你可以使用imageView.setAlpha(1.0f),但需要API级别11. – 2013-10-30 23:36:08

12

也许对于素色背景一个有用的替代方法:将一个的LinearLayoutImageView的

,并使用的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> 

改变机器人:背景的LinearLayout之间(完全透明)和#FF000000属性(完全不透明)。

199

它比其他响应更容易后者可用。 有一个xml值alpha,它取值为double值。

android:alpha="0.0"这就是无形的

android:alpha="0.5"看穿

android:alpha="1.0"全可见

这就是它是如何工作的。

3

使用此表单来古代版本的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); 
7

现在有一个XML替代:

 <ImageView 
     android:id="@+id/example" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/example" 
     android:alpha="0.7" /> 

它是:机器人:阿尔法= “0.7”

随着其值从0(透明)到1(不透明) 。

4

使用android:alpha = 0.5来实现50%的不透明度并将Android材质图标从黑色变为灰色。