xml文件定义 几个btn 一个用于显示的图片
补间动画:位置变化。但真实的图片没有动地方,只是看不见 了;
package com.lyz.news.d35bujian; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private ImageView iv; private TranslateAnimation ta; private RotateAnimation ra; private ScaleAnimation sa; private AlphaAnimation aa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //你的动画都是作业在imageview上面的把,所以要拿到他 iv = (ImageView) findViewById(R.id.iv); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你点不到我--------------", Toast.LENGTH_SHORT).show(); } }); } public void translate(View v) { //平移 // 10 到100不是绝对坐标,可以理解为相对坐标把x起始位改为0就不起始就位移了,起始y0 // x y x y 起始x 结束y 起始x 结束y // ta=new TranslateAnimation(0,10,0,150); ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);//y的起始位置,0.5Y //设置持续时间 ta.setDuration(1000); // 重复 ta.setRepeatCount(2); // 重复模式 ta.setRepeatMode(Animation.RESTART); // 在imageView开启 iv.startAnimation(ta); } public void scale(View v) { // 缩放动画,从小到大的变化 0.5f 放大到2倍,y从0.1f放大到3倍 // ScaleAnimation sa=new ScaleAnimation(0.5f,2,0.1f,3); // ScaleAnimation sa=new ScaleAnimation(0.5f,2,0.1f,3,iv.getWidth()/2,iv.getHeight()/2); sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // sa.setDuration(1000); sa.setRepeatCount(1); sa.setRepeatMode(Animation.RESTART); sa.setFillAfter(true); //这里是把动画ScaleAnimation加载到imageview到图片视图上别忘了 // sa.start(); iv.startAnimation(sa); } public void alpha(View v) { aa = new AlphaAnimation(0, 1); sa.setRepeatCount(1); aa.setDuration(1000); iv.startAnimation(aa); } public void rotate(View v) { // RotateAnimation ra=new RotateAnimation(0,360);//相对于自己的大小转圈 // RotateAnimation ra=new RotateAnimation(0,360,iv.getWidth()/2,iv.getHeight()/2);//在自身圆心 ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(1000); sa.setRepeatCount(1); iv.startAnimation(ra); } public void fly(View v) { //动画集合,false是使用自己的设置 AnimationSet as = new AnimationSet(false); as.addAnimation(ra); as.addAnimation(sa); as.addAnimation(ta); as.addAnimation(aa); iv.startAnimation(as); } /* public void shuxing(View v) { //属性,可以真实的改变坐标 // target 作用于哪个组件图片视图 ObjectAnimator oba=ObjectAnimator.ofFloat(iv,"translationX",10,100); oba.setDuration(2000); oba.start(); } */ }属性动画:真实改变图片的属性,比如大小位置;
package com.lyz.news.d35bujian; import android.animation.Animator; import android.animation.AnimatorInflater; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.Toast; public class MainShuxing extends Activity { private ImageView iv; private TranslateAnimation ta; private RotateAnimation ra; private ScaleAnimation sa; private AlphaAnimation aa; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //你的动画都是作业在imageview上面的把,所以要拿到他 iv = (ImageView) findViewById(R.id.iv); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainShuxing.this, "你点不到我--------------", Toast.LENGTH_SHORT).show(); } }); } public void translate(View v) { //平移 //属性,可以真实的改变坐标 // target 作用于哪个组件图片视图 // iv.transl ObjectAnimator oba=ObjectAnimator.ofFloat(iv,"translationX",10,70,20,100); oba.setDuration(2000); oba.start(); } public void scale(View v){ //缩放,分为水平缩放和竖直缩放,把小胖拉长和拉扁不一样的 // iv.setScaleX(); //这里的属性1是本身2是2倍 ObjectAnimator ob=ObjectAnimator.ofFloat(iv,"ScaleX",1,1.6f,1.2f,3); ob.setDuration(2000); ob.start(); } public void alpha(View v){ // iv.setAlpha(); ObjectAnimator ob=ObjectAnimator.ofFloat(iv,"alpha",0,0.6f,0.1f,1); ob.setDuration(2000); ob.start(); } public void rotate(View v){ // iv.setRotationX();旋转 // iv.setRotation旋转有3个 ,分为水平(Y)旋转和竖直旋转(x)还有顺时针旋转; ObjectAnimator ob=ObjectAnimator.ofFloat(iv,"rotation",0,180,90,30); ob.setDuration(2000); ob.start(); } public void fly(View v){ ObjectAnimator oba1 =ObjectAnimator.ofFloat(iv,"translationX",10,70,20,100); oba1.setDuration(2000); oba1.start(); ObjectAnimator ob1=ObjectAnimator.ofFloat(iv,"rotation",0,180,90,30); ob1.setDuration(2000); ob1.start(); ObjectAnimator ob2=ObjectAnimator.ofFloat(iv,"alpha",0,0.6f,0.1f,1); ob2.setDuration(2000); ob2.start(); AnimatorSet ars=new AnimatorSet(); // ars.playSequentially(ob1,ob2,oba1); // 一个一个飞 //一起飞 ars.playTogether(ob1, ob2, oba1); ars.start(); } public void xml(View v){ //布局文件用布局填充器 //动画用动画填充器 final Animator animator = AnimatorInflater.loadAnimator(this, R.animator.abjanimaor); // 设置目标在哪个图片视图上显示动画 animator.setTarget(iv); animator.start(); } }