ANDROID_MARS学习笔记_S02_007_Animation第一种使用方式:代码

一、简介

 

 

二、代码
1.xml
(1)activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="fill_parent"
 4     android:layout_height="fill_parent">
 5 
 6     <Button android:id="@+id/scaleButtonId" android:layout_width="fill_parent"
 7         android:layout_height="wrap_content" android:layout_alignParentBottom="true"
 8         android:text="测试scale动画效果" />
 9         
10     <Button android:id="@+id/rotateButtonId" android:layout_width="fill_parent"
11         android:layout_height="wrap_content" android:layout_above="@id/scaleButtonId"
12         android:text="测试rotate动画效果" />
13         
14     <Button android:id="@+id/alphaButtonId" android:layout_width="fill_parent"
15         android:layout_height="wrap_content" android:layout_above="@id/rotateButtonId"
16         android:text="测试alpha动画效果" />
17 
18     <Button android:id="@+id/translateButtonId"
19         android:layout_width="fill_parent" android:layout_height="wrap_content"
20         android:layout_above="@id/alphaButtonId" android:text="测试translate动画效果" />
21         
22     <LinearLayout android:orientation="vertical"
23         android:layout_width="fill_parent" android:layout_height="fill_parent">
24 
25         <ImageView android:id="@+id/imageViewId"
26             android:layout_width="wrap_content" android:layout_height="100dp"
27             android:layout_centerInParent="true" android:layout_marginTop="100dip"
28             android:src="@drawable/android" />
29     </LinearLayout>
30 </RelativeLayout>

 

2.java
(1)MainActivity.java

 1 package com.animation1;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.View.OnClickListener;
 7 import android.view.animation.AlphaAnimation;
 8 import android.view.animation.Animation;
 9 import android.view.animation.AnimationSet;
10 import android.view.animation.RotateAnimation;
11 import android.view.animation.ScaleAnimation;
12 import android.view.animation.TranslateAnimation;
13 import android.widget.Button;
14 import android.widget.ImageView;
15 
16 public class MainActivity extends Activity {
17 
18     private ImageView imageView = null;
19     private Button rotateButton, scaleButton, alphaButton, translateButton = null;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         
25         imageView = (ImageView) findViewById(R.id.imageViewId);
26         
27         rotateButton = (Button) findViewById(R.id.rotateButtonId);
28         scaleButton = (Button) findViewById(R.id.scaleButtonId);
29         alphaButton = (Button) findViewById(R.id.alphaButtonId);
30         translateButton = (Button) findViewById(R.id.translateButtonId);
31         
32         rotateButton.setOnClickListener(new RotateButtonListener());
33         scaleButton.setOnClickListener(new ScaleButtonListener());
34         alphaButton.setOnClickListener(new AlphaButtonListener());
35         translateButton.setOnClickListener(new TranslateButtonListener());
36         
37     }
38     
39     private class RotateButtonListener implements OnClickListener {
40         @Override
41         public void onClick(View v) {
42             AnimationSet animationSet = new AnimationSet(true);
43             RotateAnimation rotateAnimation = new RotateAnimation(0, 360, 
44                     AnimationSet.RELATIVE_TO_PARENT, 1f,
45                     Animation.RELATIVE_TO_PARENT, 0f);
46             rotateAnimation.setDuration(5000);
47             animationSet.addAnimation(rotateAnimation);
48             imageView.startAnimation(animationSet);
49         }
50     }
51     
52     private class ScaleButtonListener implements OnClickListener {
53         @Override
54         public void onClick(View v) {
55             AnimationSet animationSet = new AnimationSet(true);
56             ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f, 
57                     Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
58             animationSet.addAnimation(scaleAnimation);
59             animationSet.setStartOffset(1000);
60             animationSet.setFillAfter(true);
61             animationSet.setFillBefore(false);
62             animationSet.setDuration(2000);
63             imageView.startAnimation(animationSet);
64         }
65     }
66     
67     private class AlphaButtonListener implements OnClickListener {
68         @Override
69         public void onClick(View v) {
70             AnimationSet animationSet = new AnimationSet(true);
71             AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
72             alphaAnimation.setDuration(1000);
73             animationSet.addAnimation(alphaAnimation);
74             imageView.startAnimation(animationSet);
75         }
76     }
77     
78     private class TranslateButtonListener implements OnClickListener {
79         @Override
80         public void onClick(View v) {
81             AnimationSet animationSet = new AnimationSet(true);
82             TranslateAnimation translateAnimation = new TranslateAnimation(
83                     Animation.RELATIVE_TO_SELF, 0f, 
84                     Animation.RELATIVE_TO_SELF, 0.5f, 
85                     Animation.RELATIVE_TO_SELF, 0f, 
86                     Animation.RELATIVE_TO_SELF, 1.0f);
87             translateAnimation.setDuration(1000);
88             animationSet.addAnimation(translateAnimation);
89             imageView.startAnimation(translateAnimation);
90         }
91     }
92 }

 

 

 

转载于:https://www.cnblogs.com/shamgod/p/5201196.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值