Android---63---Android中的动画效果

Android中有四种动画效果:

AlphaAnimation:透明度动画效果

ScaleAnimation:缩放动画效果

TranslateAnimation:位移动画效果

RotateAnimation:旋转动画效果

 

1.透明动画效果

 

 

 

public class MainActivity extends Activity {

	private AlphaAnimation aa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		aa = new AlphaAnimation(1, 0);
		aa.setDuration(3000);
		// 设置监听事件
		aa.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				Toast.makeText(MainActivity.this, "start", 0).show();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				Toast.makeText(MainActivity.this, "repeat", 0).show();
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				Toast.makeText(MainActivity.this, "end", 0).show();
			}
		});
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.startAnimation(aa);
			}
		});

	}
}


 

通过xml配置文件 完成该动画效果

创建xml文件:右键res->选择android xml file

 

 

 

 

创建完成之后会在res目录下生成一个anim文件夹

aa.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromAlpha="1"
    android:toAlpha="0" >

</alpha>


 

Activity中只需要 声明 AlphaAnimation 并在Button的监听事件中 调用

v.setAnimation (AnimationUtils.loadAnimation(MainActivity.this,R.anim.aa));


 

效果是一样的。

 

 

 

2. 缩放动画效果

 

Android---63---Android中的动画效果_第1张图片

 

 

public class MainActivity extends Activity {

	private ScaleAnimation sa;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		sa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		sa.setDuration(2000);
		
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				v.startAnimation(sa);
			}
		});
	}
}


 

通过xml文件:

 

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1"
    android:toYScale="1" >

</scale>


 

3. 位移动画效果

 

 

 

public class MainActivity extends Activity {

	private TranslateAnimation ta;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ta = new TranslateAnimation(0, 200, 0, 200);
		ta.setDuration(2000);

		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.startAnimation(ta);
				// v.startAnimation(AnimationUtils.loadAnimation(
				// MainActivity.this, R.anim.ta));
			}
		});
	}
}


 

xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="200"
    android:toYDelta="200" >

</translate>


 

 

4. 旋转动画效果

 

 

public class MainActivity extends Activity {

	private RotateAnimation ra;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// ra = new RotateAnimation(0, 360);
		// ra = new RotateAnimation(0, 360, 50, 150);
		ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		ra.setDuration(2000);

		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.startAnimation(ra);
			}
		});
	}
}


 

 

 

动画混合:

AnimationSet

 

 

 

 

public class MainActivity extends Activity {

	AnimationSet as ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		as = new AnimationSet(true);
		as.setDuration(2000);
		
		AlphaAnimation aa = new AlphaAnimation(0, 1);
		aa.setDuration(2000);
		
		TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 200);
		ta.setDuration(2000);
		
		
		as.addAnimation(ta);
		as.addAnimation(aa);
		
		findViewById(R.id.btn).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				v.startAnimation(as);
			}
		});
	}
}


 

创建Xml文件选择set:

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:shareInterpolator="true" >

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1" />

    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="200"
        android:toYDelta="200" />

</set>


 

你可能感兴趣的:(动画,android,动画效果)