Android中的属性动画(Property Animation)

来源:互联网 发布:北京电脑数据恢复公司 编辑:程序博客网 时间:2024/05/21 17:52

Android提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。View Animation只能支持简单的缩放、平移、旋转、淡入淡出动画,且有一定的局限性。比如:你希望View有一个颜色的切换动画;你希望当动画停止时,View的位置就是当前的位置,这时候只能用Property Animation了。

首先在res文件夹下新建一个Property Animation,为其添加平移动画:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="translationX"
    android:duration="3000"
    android:valueFrom="0"
    android:valueTo="200">
</objectAnimator>

在布局文件中:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp"
        android:src="@drawable/ic_launcher"
        android:onClick="onImgClick" />

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="80dp"
        android:text="View Animation"
        android:onClick="onClick01" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@id/button1"
        android:layout_centerVertical="true"
        android:onClick="onClick02"
        android:text="Property Animation" />

</RelativeLayout>

在MainActivity中设置点击按钮实现平移

package com.example.property;

import android.app.Activity;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity{
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView) findViewById(R.id.imageView1);
        
    }

    public void onImgClick(View v){
        Toast.makeText(this, "image click", 0).show();
    }
   public void onClick01(View v){
        TranslateAnimation ta=new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT, //fromXType
        0,
        Animation.RELATIVE_TO_PARENT, //toXType
        0.5f,//父容器的宽度的一半
        Animation.RELATIVE_TO_PARENT, //fromYType
        0,
        Animation.RELATIVE_TO_PARENT, //toYType
        0);
        ta.setDuration(3000);
        ta.setFillAfter(true);
        imageView.startAnimation(ta);
        
    }
    public void onClick02(View v){
        ObjectAnimator oa=(ObjectAnimator)
        AnimatorInflater.loadAnimator(this,R.animator.oa);
        oa.setTarget(imageView);
        oa.start();
    }

}

在MainActivity中对ImageView设置了点击监听事件,动画效果结束后,如果用View Animation的话只有点击原来的位置才会响应监听事件,而Property Animation则只有点击平移后的位置才会响应监听事件。


0 0
原创粉丝点击