getBackground().setAlpha和setAlpha有什么区别
来源:互联网 发布:2016淘宝开店步骤 编辑:程序博客网 时间:2024/06/05 03:55
代码
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 009-07 13:45:36.027: E/linux(14636): ---默认-- 09-07 13:45:36.027: E/linux(14636): -textView3- 009-07 13:45:36.027: E/linux(14636): -textView4- 25509-07 13:45:36.027: E/linux(14636): -textView5- 25509-07 13:45:36.027: E/linux(14636): -textView6- 25509-07 13:45:36.027: E/linux(14636): -textView7- 25509-07 13:45:36.027: E/linux(14636): --setAlpha(10)-- 09-07 13:45:36.027: E/linux(14636): -textView3- 009-07 13:45:36.027: E/linux(14636): -textView4- 909-07 13:45:36.027: E/linux(14636): -textView5- 909-07 13:45:36.027: E/linux(14636): -textView6- 25509-07 13:45:36.037: E/linux(14636): -textView7- 25509-07 13:45:36.037: E/linux(14636): ---setAlpha(255)-- 09-07 13:45:36.037: E/linux(14636): -textView3- 009-07 13:45:36.037: E/linux(14636): -textView4- 25509-07 13:45:36.037: E/linux(14636): -textView5- 25509-07 13:45:36.037: E/linux(14636): -textView6- 25509-07 13:45:36.037: E/linux(14636): -textView7- 25509-07 13:45:36.037: E/linux(14636): ---mutate().setAlpha(88)-- 09-07 13:45:36.037: E/linux(14636): -textView3- 009-07 13:45:36.037: E/linux(14636): -textView4- 25509-07 13:45:36.037: E/linux(14636): -textView5- 25509-07 13:45:36.037: E/linux(14636): -textView6- 8709-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));
源码
http://download.csdn.net/download/codehxy/9968275
阅读全文
0 0
- getBackground().setAlpha和setAlpha有什么区别
- view.setAlpha()和 view.getBackground().setAlpha()的区别
- getBackground().setAlpha 奇葩问题
- getBackground().setAlpha导致所有资源都有变化
- getBackground().setAlpha所导致问题
- getBackground().setAlpha所导致问题
- getBackground().setAlpha所导致问题
- getBackground().setAlpha所导致问题
- setalpha和setimagealpha的区别详解
- 慎用alpha值——setAlpha与getBackground.setAlpha的差别
- getBackground().setAlpha(ScrollY)遇到的奇怪问题解决方案
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- getBackground().setAlpha(0)设置透明度连下层控件背景色都被改变
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- android 控件 getBackground().setAlpha设置透明值,影响其他页面样式问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- 使用getBackground().setAlpha,导致其他布局背景透明度都改变的问题
- getBackground().setAlpha导致其他布局背景透明度都改变的问题
- java中序列化与反序列化
- Hive学习笔记
- ASP.Net Core WebApi几种版本控制对比
- rt-thread创建动态线程失败
- [k8s]docker entrypoint图解
- getBackground().setAlpha和setAlpha有什么区别
- PPT设计笔记
- 用户需求浅谈
- Hibernate之关联映射(上)
- HDU 1905Pseudoprime numbers(快速幂)
- 2017开学训练第二周周中总结
- Apache-ab安装使用
- Windows程序设计-GDI图形
- ?[4]621. Task Scheduler(Java)