android 阴影效果

    xiaoxiao2021-04-15  69

    昨天,我在做android界面时,发现一个textView需要加上阴影的效果,自己当时不知道怎么做,所以在网上查了一些资料,我发现看懂还是需要一些时间,所以现在记录一下。

    1,直接使用属性: Android:elevation="4dp"这样一句代码,就实现了效果,elevation表示海拔,就是布局的z轴的高度,调整高度,可以选择阴影的轻重。

    <TextView                 android:layout_width="0dp"                 android:layout_height="match_parent"                 android:layout_weight="1"                 android:gravity="center"                 android:elevation="4dp"                 android:background="@drawable/home_waitcourse_yellow_shape"                 android:textColor="@color/foorYellow"                   android:text="报道"/>

    这个我没有使用,也不知道效果怎么样,所以大家可以自己使用者试试。

    2,这种方式就需要写点代码了,但是也不多,是通过写一个xml来实现的。

    <?xml version="1.0" encoding="utf-8"?>  <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >      <!-- 阴影部分 -->      <!-- 个人觉得更形象的表达:top代表下边的阴影高度,left代表右边的阴影宽度。其实也就是相对应的offset,solid中的颜色是阴影的颜色,也可以设置角度等等 -->      <item android:left="2dp" android:top="2dp" android:right="0dp" android:bottom="0dp">          <shape android:shape="rectangle" >              <gradient android:angle="270" android:endColor="#0F000000" android:startColor="#0F000000" />              <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dip" android:topLeftRadius="6dip" android:topRightRadius="6dip" />          </shape>      </item>      <!-- 背景部分 -->      <!-- 形象的表达:bottom代表背景部分在上边缘超出阴影的高度,right代表背景部分在左边超出阴影的宽度(相对应的offset) -->      <item android:left="0dp" android:top="0dp" android:right="3dp" android:bottom="3dp">          <shape android:shape="rectangle" >              <gradient android:angle="270" android:endColor="#FFFFFF" android:startColor="#FFFFFF" />              <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dip" android:topLeftRadius="6dip" android:topRightRadius="6dip" />          </shape>      </item>  </layer-list>

    这个代码样式我当时没有看懂,后来发现其实很简单。主要是在代码定义了一个textView的背景样式和阴影样式。通过item的top,left,right,bottom属性来区分。阴影样式主要设置left,top的值,来让阴影右移和下移,背景主要设置right,bottom来设置左移和上移。接下来,只要把textView设置为这个自定义的样式就可以了。

    android 的阴影效果就讲完了。

    就这么简单。

    
    转载请注明原文地址: https://ju.6miu.com/read-670952.html

    最新回复(0)