android之Animation<1>

来源:互联网 发布:ubuntu与linux的区别 编辑:程序博客网 时间:2024/04/30 04:24
public class MainActivity extends Activity {private ImageView image;private Button alpha_btn, rotate_btn, scale_btn, translate_btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);image = (ImageView)findViewById(R.id.image);alpha_btn = (Button)findViewById(R.id.alpha);rotate_btn = (Button)findViewById(R.id.rotate);scale_btn = (Button)findViewById(R.id.scale);translate_btn = (Button)findViewById(R.id.translate);alpha_btn.setOnClickListener(new OnClickListener() {public void onClick(View v) {//1.创建一个AnimationSet对象AnimationSet animationSet = new AnimationSet(true);//2.创建一个AlphaAnimation对象,Alpha透明度渐变,全不透到全透的渐变AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//3.为AlphaAnimation设置相应的数据alphaAnimation.setDuration(5000);//4.将AlphaAnimation添加到AnimationSet对象中animationSet.addAnimation(alphaAnimation);//5.执行动画image.startAnimation(animationSet);}});rotate_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AnimationSet animationSet = new AnimationSet(true);//以image的左上角为旋转轴点//RotateAnimation rotate = new RotateAnimation(0, 360);/*绝对像素,相对于view的左上角RotateAnimation rotate = new RotateAnimation(0, 360,Animation.ABSOLUTE, 150, Animation.ABSOLUTE, 150);*//*相对于view自身RotateAnimation rotate = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);*/RotateAnimation rotate = new RotateAnimation(0, 360,Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);rotate.setDuration(5000);animationSet.addAnimation(rotate);image.startAnimation(rotate);}});scale_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AnimationSet animationSet = new AnimationSet(true);ScaleAnimation scale = new ScaleAnimation(0.1f, 1f, 0.1f, 1f, //x,y缩放比例Animation.RELATIVE_TO_SELF, 0.5f,//缩放中心点 Animation.RELATIVE_TO_SELF, 0.5f);scale.setDuration(3000);animationSet.addAnimation(scale);image.startAnimation(animationSet);}});translate_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AnimationSet animationSet = new AnimationSet(true);TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1f,//起始移动的xAnimation.RELATIVE_TO_SELF, 0f,//起始移动的yAnimation.RELATIVE_TO_SELF, 0.5f,//移动终点xAnimation.RELATIVE_TO_SELF, 0f);//移动终点ytranslate.setDuration(3000);animationSet.addAnimation(translate);image.startAnimation(animationSet);}});}}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView     android:id="@+id/image"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:src="@drawable/teas"/><Button     android:id="@+id/alpha"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_marginTop="0dp"    android:text="Alpha"/><Button     android:id="@+id/rotate"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_below="@id/alpha"    android:layout_marginTop="20dp"    android:text="rotate"/><Button     android:id="@+id/scale"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_alignParentBottom="true"    android:text="scale"/><Button     android:id="@+id/translate"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_above="@id/scale"    android:layout_marginBottom="20dp"    android:text="translate"/>        </RelativeLayout>