Android_Animation

Xml:

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a"
        />

</LinearLayout>

 

 

java:

package com.wangbiao.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class AnimationActivity extends Activity {

	private ImageView image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animation);
		image = (ImageView) this.findViewById(R.id.image);

		// 淡入淡出
		// image.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// AnimationSet set=new AnimationSet(true);
		// AlphaAnimation alpha=new AlphaAnimation(0,1);
		// alpha.setDuration(3000);
		// set.addAnimation(alpha);
		// image.startAnimation(set);
		// }
		// });

		// 伸缩
		// image.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// AnimationSet set = new AnimationSet(true);
		// ScaleAnimation scale = new ScaleAnimation(1, 0, 1, 0,
		// Animation.RELATIVE_TO_SELF, 0.5f,
		// Animation.RELATIVE_TO_SELF, 0.5f);
		// scale.setDuration(5000);
		// set.addAnimation(scale);
		// image.startAnimation(scale);
		//
		// }
		// });

		// 平移
//		image.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				AnimationSet set = new AnimationSet(true);
//				TranslateAnimation translate = new TranslateAnimation(
//						Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0.5f);
//				translate.setDuration(5000);
//				set.addAnimation(translate);
//				image.startAnimation(translate);
//
//			}
//		});
		
		//旋转
		image.setOnClickListener(new View.OnClickListener() {
		
					@Override
					public void onClick(View v) {
						//AnimationSet是Animation的子类,可以理解为是动画效果的集合。
						//new AnimationSet(true);true表示的是,将所有的速率交给AnimationSet对象统一设置(可setInterpolator()统一设置),而各个不同的动画中的速率不起作用。
						AnimationSet set = new AnimationSet(true);
						//Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.3f表示的是父控件的旋转中心的坐标X为X轴的一半,Y为Y轴的3/10.
						RotateAnimation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.3f);
						rotate.setRepeatCount(3);
						//动画效果叠加
						AlphaAnimation alpha=new AlphaAnimation(0,1);
						alpha.setRepeatCount(3);
						//设置动画速率
						/**
						 *AccelerateInterpolator   加速
						 *DecelerateInterpolator   减速
						 *AccelerateDecelerateInterpolator 加速——减速
						 *CycleInterpolator 速度沿着正弦曲线变化
						 *LinearInterpolator 匀速
						 */
						set.setInterpolator(new AccelerateInterpolator());
						//set.addAnimation(alpha);
						set.addAnimation(rotate);
						set.setDuration(5000);
						set.setRepeatCount(3);//设置的没有用,各个动画效果自行设置重复数,测试发现,AnimationSet的重复事件不会被触发,若是想触发重复事件,只能对各个动画单独进行监听。
						set.setAnimationListener(new AnimationListener() {
							
							@Override
							public void onAnimationStart(Animation animation) {
								//动画开始时触发
								Toast.makeText(AnimationActivity.this, "动画开始了", Toast.LENGTH_LONG).show();
							}
							
							@Override
							public void onAnimationRepeat(Animation animation) {
								//动画重复时触发
								Toast.makeText(AnimationActivity.this, "动画在重复", Toast.LENGTH_SHORT).show();
							}
							
							@Override
							public void onAnimationEnd(Animation animation) {
								//动画结束时触发
								Toast.makeText(AnimationActivity.this, "动画结束了", Toast.LENGTH_LONG).show();
							}
						});
						image.startAnimation(set);
		
					}
				});
		

	}

}

 

你可能感兴趣的:(android,animation)