Android动画一

Android动画的两种形式

 

布局文件

 

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

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/imageView"
        android:onClick="click"
        android:src="@android:drawable/ic_lock_idle_alarm" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="move"
        android:onClick="move"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="102dp" />

</RelativeLayout>

 

代码文件:

package com.androidbase.zjn.androidbase;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View view){
        Toast.makeText(this,"clicked",Toast.LENGTH_LONG).show();
    }
    public void move(View view){
        TranslateAnimation  animation = new TranslateAnimation(0,200,0,0);
        animation.setDuration(1000);//运动1秒钟
        animation.setFillAfter(true);//动画结束后果停留到原来的位置
        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        imageView.startAnimation(animation);//imageView绑定动画
    }

}

使用Animator的效果:

ImageView imageView =(ImageView) findViewById(R.id.imageView);
        ObjectAnimator.ofFloat(imageView,"translationX",0,200F).setDuration(1000).start();
        ObjectAnimator.ofFloat(imageView,"translationY",0,200F).setDuration(1000).start();
        ObjectAnimator.ofFloat(imageView,"rotation",0,360F).setDuration(1000).start();

使用几个一起懂:

        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0,360F);
        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX",0,200F);
        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY",0,200F);
        ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();
    //几个动画一起显示
        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView,"translationX",0,200F);
        ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView,"translationY",0,200F);
        ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView,"rotation",0,360F);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator1,animator2,animator3);
        set.setDuration(1000);
        set.start();

完整的代码:

package com.androidbase.zjn.androidbase;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View view){
        Toast.makeText(this,"clicked",Toast.LENGTH_LONG).show();
    }
    public void move(View view){
        //TranslateAnimation只是简单的移动
//        TranslateAnimation  animation = new TranslateAnimation(0,200,0,0);
//        animation.setDuration(1000);//运动1秒钟
//        animation.setFillAfter(true);//动画结束后果停留到原来的位置
//        ImageView imageView =(ImageView) findViewById(R.id.imageView);
//        imageView.startAnimation(animation);//imageView绑定动画

//        ImageView imageView =(ImageView) findViewById(R.id.imageView);
//        ObjectAnimator.ofFloat(imageView,"translationX",0,200F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"translationY",0,200F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"rotation",0,360F).setDuration(1000).start();

//        上面的优化
//        ImageView imageView =(ImageView) findViewById(R.id.imageView);
//        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0,360F);
//        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX",0,200F);
//        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY",0,200F);
//        ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();

        //几个动画一起显示
        ImageView imageView =(ImageView) findViewById(R.id.imageView);
        ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView,"translationX",0,200F);
        ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView,"translationY",0,200F);
        ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView,"rotation",0,360F);
        AnimatorSet set = new AnimatorSet();
        //set.playTogether(animator1,animator2,animator3);
        //set.playSequentially(animator1,animator2,animator3);//按照顺序动画
        set.play(animator1).with(animator2).before(animator3);//动画1和动画2一块进行,让后再进行动画3
        set.setDuration(1000);
        set.start();

    }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。