代码
MainActivity
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView1=(TextView)findViewById(R.id.tv1);
textView1.setBackgroundColor(0xFA8072);
textView1.setAlpha(0.2f);
TextView textView2=(TextView)findViewById(R.id.tv2);
textView2.setBackgroundColor(0xFA8072);
float alf=textView2.getAlpha();
int balf=textView2.getBackground().getAlpha();
Log.e("linux","-getAlpha-"+alf+" getBackground.getAlpha "+balf);
TextView textView3=(TextView)findViewById(R.id.tv3);
textView3.setBackgroundColor(0xFA8072);
textView3.getBackground().setAlpha(100);
TextView textView4=(TextView)findViewById(R.id.tv4);
TextView textView5=(TextView)findViewById(R.id.tv5);
TextView textView6=(TextView)findViewById(R.id.tv6);
TextView textView7=(TextView)findViewById(R.id.tv7);
Log.e("linux","---默认-- ");
Log.e("linux","-textView3- "+textView3.getBackground().getAlpha());
Log.e("linux","-textView4- "+textView4.getBackground().getAlpha());
Log.e("linux","-textView5- "+textView5.getBackground().getAlpha());
Log.e("linux","-textView6- "+textView6.getBackground().getAlpha());
Log.e("linux","-textView7- "+textView7.getBackground().getAlpha());
textView4.getBackground().setAlpha(10);
Log.e("linux","--setAlpha(10)-- ");
Log.e("linux","-textView3- "+textView3.getBackground().getAlpha());
Log.e("linux","-textView4- "+textView4.getBackground().getAlpha());
Log.e("linux","-textView5- "+textView5.getBackground().getAlpha());
Log.e("linux","-textView6- "+textView6.getBackground().getAlpha());
Log.e("linux","-textView7- "+textView7.getBackground().getAlpha());
textView5.getBackground().setAlpha(255);
Log.e("linux","---setAlpha(255)-- ");
Log.e("linux","-textView3- "+textView3.getBackground().getAlpha());
Log.e("linux","-textView4- "+textView4.getBackground().getAlpha());
Log.e("linux","-textView5- "+textView5.getBackground().getAlpha());
Log.e("linux","-textView6- "+textView6.getBackground().getAlpha());
Log.e("linux","-textView7- "+textView7.getBackground().getAlpha());
textView6.getBackground().mutate().setAlpha(88);
Log.e("linux","---mutate().setAlpha(88)-- ");
Log.e("linux","-textView3- "+textView3.getBackground().getAlpha());
Log.e("linux","-textView4- "+textView4.getBackground().getAlpha());
Log.e("linux","-textView5- "+textView5.getBackground().getAlpha());
Log.e("linux","-textView6- "+textView6.getBackground().getAlpha());
Log.e("linux","-textView7- "+textView7.getBackground().getAlpha());
}
}
activity_main文件
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world"
android:textSize="15sp" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world"
android:textSize="15sp"
/>
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world"
android:background="#00FFFF"
android:textSize="15sp"
/>
<TextView
android:id="@+id/tv5"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world"
android:background="#00FFFF"
android:textSize="15sp"
/>
<TextView
android:id="@+id/tv6"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world"
android:background="#00FF00"
android:textSize="15sp"
/>
<TextView
android:id="@+id/tv7"
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/hello_world"
android:background="#00FF00"
android:textSize="15sp"
/>
</LinearLayout>
效果图
日志
09-07 13:45:36.027: E/linux(14636): -getAlpha-1.0 getBackground.getAlpha 0
09-07 13:45:36.027: E/linux(14636): ---默认--
09-07 13:45:36.027: E/linux(14636): -textView3- 0
09-07 13:45:36.027: E/linux(14636): -textView4- 255
09-07 13:45:36.027: E/linux(14636): -textView5- 255
09-07 13:45:36.027: E/linux(14636): -textView6- 255
09-07 13:45:36.027: E/linux(14636): -textView7- 255
09-07 13:45:36.027: E/linux(14636): --setAlpha(10)--
09-07 13:45:36.027: E/linux(14636): -textView3- 0
09-07 13:45:36.027: E/linux(14636): -textView4- 9
09-07 13:45:36.027: E/linux(14636): -textView5- 9
09-07 13:45:36.027: E/linux(14636): -textView6- 255
09-07 13:45:36.037: E/linux(14636): -textView7- 255
09-07 13:45:36.037: E/linux(14636): ---setAlpha(255)--
09-07 13:45:36.037: E/linux(14636): -textView3- 0
09-07 13:45:36.037: E/linux(14636): -textView4- 255
09-07 13:45:36.037: E/linux(14636): -textView5- 255
09-07 13:45:36.037: E/linux(14636): -textView6- 255
09-07 13:45:36.037: E/linux(14636): -textView7- 255
09-07 13:45:36.037: E/linux(14636): ---mutate().setAlpha(88)--
09-07 13:45:36.037: E/linux(14636): -textView3- 0
09-07 13:45:36.037: E/linux(14636): -textView4- 255
09-07 13:45:36.037: E/linux(14636): -textView5- 255
09-07 13:45:36.037: E/linux(14636): -textView6- 87
09-07 13:45:36.037: E/linux(14636): -textView7- 255
分析
1、直接setAlpha是改变整个view的透明度
getBackground().setAlpha只是改变view的背景的透明度
2、有背景才能够设置透明度。
必须先设置背景,之后才能设置背景的透明度,不能直接getBackground().setAlpha。
TextView textView3=(TextView)findViewById(R.id.tv3);
textView3.getBackground().setAlpha(100);
#textView3.setBackgroundColor(0xFA8072);
/*如果代码这么写,会直接报错
Caused by: java.lang.NullPointerException:
'void android.graphics.drawable.Drawable.setAlpha(int)'
on a null object reference
*/
3、textView3是不变的;
看到textView4和textView5是一起改变的;
而textView6和textView7又能够不一起改变
更多
1、getBackground().setAlpha导致其他布局背景透明度都改变
http://www.knowsky.com/1049068.html
2、android代码设置背景setBackgroundColor无效
http://blog.csdn.net/u013351986/article/details/44497491
'为什么textView3设置了setAlpha依然没有背景色?'
1、效果
我们自己定义一个color.xml文件,然后再xml中引用此文件中的颜色,一切顺利。
但当我们在代码中如view.setBackgroundColor(R.color.red)来引用我们自定义的xml文件时,是无效的。
2、原因
颜色4个字节从高到低对应 不透明度 红 绿 蓝 四个分量,0xFFFFFF对应不透明度的分量是0,所以后面不管是什么背景都是透明的。0xFFFFFFFF就是白色了,0x80FFFFFF就是半透明的白色。Color.rgb()会自动把不透明读设成255,而解析字符串颜色"#FFFFFF"也能判断出只给出了三个分量会自动把不透明度设成255。而对于整数0xFFFFFF和0x00FFFFFF是一样的,是完全透明的。
3、解决方案:
view.setBackgroundColor(Color.rgb(255, 0, 0));