[2017-7-28]Android Learning Day7
View动画效果
1.透明动画效果(AlphaAnimation)
有两种方法
第一种在活动中设置,不需要xml文件
1 public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 slove(); 8 } 9 10 private void slove() { 11 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 12 13 @Override 14 public void onClick(View v) { 15 //方法1 16 AlphaAnimation aa = new AlphaAnimation(0,1); 17 aa.setDuration(1500); 18 v.startAnimation(aa);19 } 20 }); 21 } 22 }
方法2 在xml文件中设置
在res文件夹下新建anim Directory,在anim下新建aa.xml
aa.xml
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0"起始透明度 android:toAlpha="1"终止透明度 android:duration="1000"持续时间(ms) > </alpha>
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); slove(); } private void slove() { findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //方法2 v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.aa)); } }); } }
2.旋转动画效果( RoateAnimation)
和上面几乎是一模一样的,直接上代码了
方法1
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); slove(); } private void slove() { findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
//从0°到3600°(转十圈),宽度参照是自己,大小0.5宽度,高度参照是自己,大小0.5高度 RotateAnimation ra = new RotateAnimation(0,3600,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); ra.setDuration(1000); v.startAnimation(ra); } }); } }
方法2
ra.xml
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="720" android:duration="1000" android:pivotX="50%" android:pivotY="50%" > </rotate>
1 public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 slove(); 8 } 9 10 private void slove() { 11 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 12 13 @Override 14 public void onClick(View v) { 15 v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ra)); 16 } 17 }); 18 } 19 }
3.移动动画效果(TranslateAnimation)
方法一
在TranslateAnimation中,坐标都是相对自身的坐标,而不是相对最大布局的
1 public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 slove(); 8 } 9 10 private void slove() { 11 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 12 13 @Override 14 public void onClick(View v) { 15 16 TranslateAnimation ta = new TranslateAnimation(0,200,0,200); 17 ta.setDuration(1000); 18 v.startAnimation(ta); 19 } 20 }); 21 } 22 }
方法二
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="500" android:toYDelta="500" android:duration="1000"> </translate>
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); slove(); } private void slove() { findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ta)); } }); } }
4.缩放动画效果(ScaleAnimation)
方法一
1 public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 slove(); 8 } 9 10 private void slove() { 11 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 12 13 @Override 14 public void onClick(View v) { 15 16 ScaleAnimation sa = new ScaleAnimation(0,1,0,1);//默认相对于左上角缩放 17 sa = new ScaleAnimation(0,1,0,1,50,50);//相对于主布局的(50px,50px)坐标点缩放 18 //相对于自身的50% 50%处缩放 19 sa = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 20 sa.setDuration(1000); 21 v.startAnimation(sa); 22 } 23 }); 24 } 25 }
方法二
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:toXScale="1" android:fromYScale="0" android:toYScale="1" android:pivotX="50%" android:pivotY="50%" android:duration="1000" > </scale>
1 public class MainActivity extends AppCompatActivity { 2 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 slove(); 8 } 9 10 private void slove() { 11 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 12 13 @Override 14 public void onClick(View v) { 15 16 v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.sa)); 17 } 18 }); 19 } 20 }
5.混合动画效果
方法就是添加一个 AnimationSet(动画集合),把不同的Animation添加进去,最后StartAnimation这个容器
方法一
1 public class MainActivity extends AppCompatActivity { 2 3 private AnimationSet as; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 slove(); 10 } 11 12 private void slove() { 13 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 14 15 @Override 16 public void onClick(View v) { 17 as = new AnimationSet(true);//是否使用动画补间 18 as.setDuration(1000); 19 20 AlphaAnimation aa = new AlphaAnimation(0,1); 21 aa.setDuration(1000); 22 as.addAnimation(aa); 23 24 TranslateAnimation ta = new TranslateAnimation(0, 0, 0, -500); 25 ta.setDuration(1000); 26 as.addAnimation(ta); 27 28 v.startAnimation(as); 29 } 30 }); 31 } 32 }
方法二
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shareInterpolator="true" 4 android:duration="1000"> 5 6 <alpha 7 android:fromAlpha="0" 8 android:toAlpha="1" /> 9 10 <rotate 11 android:fromDegrees="0" 12 android:toDegrees="720" 13 android:duration="1000" 14 android:pivotX="50%" 15 android:pivotY="50%" /> 16 17 </set>
1 public class MainActivity extends AppCompatActivity { 2 3 private AnimationSet as; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 slove(); 10 } 11 12 private void slove() { 13 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 14 15 @Override 16 public void onClick(View v) { 17 v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim)); 18 } 19 }); 20 } 21 }