AlphaAnimation aa = new AlphaAnimation(0, 1); aa.setDuration(1000); v.startAnimation(aa);
透明动画效果
<?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" > </alpha>
v.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.aa));
旋转动画效果
//ra = new RotateAnimation(0, 360, 100, 50);
ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(1000);
v.startAnimation(ra);
或新建一个xml文件
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:duration="1000" android:pivotX="50%" android:pivotY="50%" > </rotate>
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ra));
移动动画效果
ta = new TranslateAnimation(0, 200, 0, 200); ta.setDuration(1000); findViewById(R.id.btnTranslateMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub v.startAnimation(ta); } });
或新建一个xml文件
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0" android:toYDelta="200" android:duration="1000" > </translate>
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.ta));
缩放动画效果
// sa = new ScaleAnimation(0, 1, 0, 1, 100, 50); sa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(1000); findViewById(R.id.btnScaleMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub v.startAnimation(sa); } });
或新建一个xml文件
<?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:duration="1000" android:pivotX="50%" android:pivotY="50%" > </scale>
</pre><pre name="code" class="java">v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.sa));
混合动画效果
private AnimationSet as; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); as = new AnimationSet(true); as.setDuration(1000); AlphaAnimation aa = new AlphaAnimation(0, 1); aa.setDuration(1000); as.addAnimation(aa); TranslateAnimation ta = new TranslateAnimation(200, 0, 200, 0); ta.setDuration(1000); as.addAnimation(ta); findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub v.startAnimation(as); } });
或新建一个xml文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" android:duration="1000" > <alpha android:fromAlpha="0" android:toAlpha="1" /> <translate android:fromXDelta="200" android:toXDelta="0" android:fromYDelta="200" android:toYDelta="0" /> </set>
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim));
动画效果侦听
Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim); a.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Animation end", Toast.LENGTH_SHORT).show(); } }); v.startAnimation(a);
自定义动画效果
public class CustomAnim extends Animation { @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { // TODO Auto-generated method stub // System.out.println("init"); super.initialize(width, height, parentWidth, parentHeight); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { // TODO Auto-generated method stub // System.out.println(interpolatedTime); // t.setAlpha(interpolatedTime); t.getMatrix().setTranslate((float) (Math.sin(interpolatedTime * 20) * 50), 0); // t.getMatrix().setTranslate(200 * interpolatedTime, 200 * interpolatedTime); super.applyTransformation(interpolatedTime, t); } }
ca = new CustomAnim(); ca.setDuration(1000); findViewById(R.id.btnAnimMe).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub v.startAnimation(ca); } });