android:elevation


elevation [eli’veiʃən]
elevaltion:可以解释为视图高度,view阴影,view的z轴高度。
3种使用方式:
1,xml中直接设置属性
2,代码中设置
3,选择器。比较喜欢这种更具动画特性。

布局文件中设置:

<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:layout_marginTop="15dip"
    android:background="@android:color/white" >
    <!--你明白的
        android:elevation="5dip"
        android:stateListAnimator="@anim/elevation"
    -->
    <TextView
        android:id="@+id/tv_elevation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="10dip"
        android:background="#ff0000"
        android:clickable="true"
        android:elevation="5dip"
        android:text="elevation10"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="默认button\n带有圆角" />

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="10dip"
        android:background="@android:color/holo_orange_light"
        android:clickable="true"
        android:elevation="5dip"
        android:text="CheckBox" />
</LinearLayout>

代码中设置:

import com.hyc.android5.material.design.R
/**
 * @Description: 
 * @ClassName: Android5Activity 
 * @date 2015年7月27日 上午11:05:46
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class Android5Activity extends Activity {
     protected  final String TAG = this.getClass().getSimpleName();
     private TextView  tv_elevation;
     private CheckBox  cb_1;
     private Button  btn_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

if(android.os.Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP){//5.0一下做的操作。

        }else{
            tv_elevation=(TextView)findViewById(R.id.tv_elevation);
            cb_1=(CheckBox)findViewById(R.id.cb_1);
            btn_1=(Button)findViewById(R.id.btn_1);
        }
        // elevation 代码设置
        tv_elevation.setElevation(10);// 单位是像素
        //tv_elevation.setTranslationZ(50);
        //动态设置elevation  
//      xml属性:android:stateListAnimator="@anim/elevation"
//      set the android:stateListAnimator attribute to @null.

        /*StateListAnimator stateLAnim =          
        AnimatorInflater.loadStateListAnimator(this,R.anim.elevation);
        tv_elevation.setStateListAnimator(stateLAnim);*/

        /**
         * 这种方式没有动画渐变效果,直接就设置了elevation
         */
        /*tv_elevation.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action = motionE
                vent.getActionMasked();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d(TAG, "ACTION_DOWN on view.");
                        view.setTranslationZ(50);
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d(TAG, "ACTION_UP on view.");
                        view.setTranslationZ(0);
                        break;
                    default:
                        return false;
                }
                return true;
            }
        });*/
    }
}

选择器:/anim/elevation.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- animate the translationZ property of a view when pressed -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><set>
            <objectAnimator                      
            android:duration="@android:integer/config_shortAnimTime"    
                android:duration="200" 
                android:propertyName="translationZ" 
                android:valueTo="20dp" 
                android:valueType="floatType" />
            <!--
             you could have other objectAnimator elements
             here for "x" and "y", or other properties
            -->
        </set></item>
    <item 
        android:state_enabled="true" 
        android:state_focused="false" 
        android:state_pressed="false">
          <set>
                <objectAnimator 
                    android:duration="100" 
                    android:propertyName="translationZ" 
                    android:valueTo="5dp" 
                    android:valueType="floatType" />
            </set>
        </item>
</selector>

简单效果图:Android5.0控件都有默认动画。

checkbox也有默认的动画

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值