首页 > 代码库 > Android动画一
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(); }}
Android动画一
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。