慎用alpha值——setAlpha与getBackground.setAlpha的差别

来源:互联网 发布:h5场景节日软件 编辑:程序博客网 时间:2024/05/29 18:24

最近在做Android项目时,需要给控件添加一个外边框,该控件中的页面布局文件如下所示:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:clipChildren="false"    android:clipToPadding="false"    android:orientation="vertical"    android:gravity="center_vertical">    <LinearLayout        android:id="@+id/text_view"        android:layout_width="100dp"        android:layout_height="100dp"        android:orientation="vertical"        android:layout_gravity="center"        android:gravity="center"        android:background="@drawable/text_view_bg_sector"        android:descendantFocusability="blocksDescendants"        android:focusableInTouchMode="true"        android:focusable="true"        android:layout_marginBottom="50dp">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="2dp"            android:focusable="true"            android:text="1"            android:textSize="20sp"/>    </LinearLayout></LinearLayout>

  边框的样式如下图所示:一个白色的衬底,外面有8dp的绿色边框

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item>        <shape android:shape="rectangle">            <solid android:color="#f1f1f1"/>        </shape>    </item>    <item android:bottom="-8dp"        android:left="-8dp"        android:right="-8dp"        android:top="-8dp">        <shape android:shape="rectangle">            <solid android:color="@android:color/transparent"/>            <stroke android:color="#128128"                android:width="8dp"/>        </shape>    </item></layer-list>

  另外还在drawable文件中添加了一个selector,用来选择当控件获得焦点时的背景样式:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/text_view_bg"           android:state_focused="true"/>    <item android:drawable="@android:color/transparent"           android:state_focused="false"/></selector>

  我们使用外边框时,为了让外边框显示出来,通常都要在父控件上添加

  android:clipChildren="false"   android:clipToPadding="false"

  但是我们有时候在添加上这两个属性后,控件仍然不能超出父控件,后来发现,原来是我在代码中错误的使用了alpha值设置控件的透明度。
  
  当id名为text_view的控件获得焦点时,如果我们不做任何处理,则显示效果如下所示:
不加透明度的效果
  但是如果在我们在该控件获取焦点的时候同时给该控件设置alpha值,直接通过view.setAlpha(float float)来设置透明度,那么该控件中使用的负值元素都将无法进行显示。显示出的效果是这样的:
 直接设置alpha值的结果
  只有内部的白色部分,外边框都被切掉了。但是如果我们通过使用view.getBackground.setAlpha(int int)设置透明度,则能够显示出正常结果,如图所示:
  使用getbackground.setalpha设置不透明度为60%的效果
  上图中显示的是alpha为100的效果。
  当我们使用view.getBackground.setAlpha(int int)设置时,参数的取值范围是0~255,但是当使用view.setAlpha(float float)设置时,参数取值范围是0~1。数值越高,不透明度越高。
  上面我们说的是在java代码中设置alpha值。后来经过我的实验,如果我们直接在布局文件中设置alpha值(alpha<1),那么在页面显示时,同样不会显示出我们设置的边框。
  以上说的是我们直接在Layout上设置alpha值和背景,但是如果我们在Layout里面的控件中添加边框、添加透明度信息,那么我们设置的边框依然无法显示。
  因此我们应该谨慎使用alpha值,尤其是在有外边框的时候。如果我们真的想要设置背景的透明度情况,我们可以在Java代码中使用getBackground.setAlpha(int int)来设置,或者可以在设置背景时同时添加透明度。
  如果我们的控件边距等中不会出现负值时,两种方法不会有很大区别,但是如果我们直接使用setalpha值,会将整个控件内的所有元素都会改变透明度,但是如果使用getBackground.setAlpha,则只会改变背景图片的透明度
  

PS:背景颜色与透明度信息:

我们设置颜色时。一般会用下面的格式#FF9900或者#F90的样式。但是设置颜色时,也可以使用下面的方式:#B3FF9900,前面的B3就表示透明度百分比70%
参考文档:Android:alpha换算表

阅读全文
0 0
原创粉丝点击