首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发_Animation(2)

Android开发_Animation(2)

作者头像
Hongten
发布2018-09-13 17:06:20
3290
发布2018-09-13 17:06:20
举报
文章被收录于专栏:HongtenHongten

新建项目:

http://www.cnblogs.com/hongten/gallery/image/112169.html

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112168.html

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"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 
 8     <!-- translate移动位置 -->
 9     <Button
10         android:id="@+id/btn_translate"
11         android:layout_width="fill_parent"
12         android:layout_height="wrap_content"
13         android:layout_alignParentBottom="true"
14         android:text="测试translate移动动画效果"
15     />
16     <!-- alpha淡入淡出效果 -->
17     <Button
18         android:id="@+id/btn_alpha"
19         android:layout_width="fill_parent"
20         android:layout_height="wrap_content"
21         android:layout_above="@id/btn_translate"
22         android:text="测试alpha淡入/出效果"
23     />
24     <!-- rotate旋转效果 -->
25     <Button
26         android:id="@+id/btn_rotate"
27         android:layout_width="fill_parent"
28         android:layout_height="wrap_content"
29         android:layout_above="@id/btn_alpha"
30         android:text="测试rotate旋转效果"
31     />
32     <!-- scale伸缩效果 -->
33     <Button
34         android:id="@+id/btn_scale"
35         android:layout_width="fill_parent"
36         android:layout_height="wrap_content"
37         android:layout_above="@id/btn_rotate"
38         android:text="测试scale伸缩效果"
39     />
40     <LinearLayout
41         android:orientation="vertical"
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44     >
45     <!-- 存放测试图片 -->
46         <ImageView 
47         android:id="@+id/imageViewId"
48         android:layout_width="wrap_content"
49         android:layout_height="wrap_content"
50         android:layout_centerInParent="true"
51         android:layout_marginTop="100dip"
52         android:src="@drawable/icon"
53         />
54     </LinearLayout>
55 </RelativeLayout>

anim/alpha.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:interpolator="@android:anim/accelerate_interpolator">
 4     <!-- 淡入淡出,从完全不透明变化到完全透明,持续时间为5秒 -->
 5     <alpha
 6         android:fromAlpha="1.0"
 7         android:toAlpha="0.0"
 8         android:startOffset="500"
 9         android:duration="5000"
10     />
11 </set>

anim/translate.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:interpolator="@android:anim/accelerate_interpolator">
 4     <!-- 移动位置,持续时间为5秒 -->
 5     <translate
 6         android:fromXDelta="50%"
 7         android:toXDelta="50%"
 8         android:fromYDelta="0%"
 9         android:toYDelta="100%"
10         android:duration="5000"
11     />
12 </set>

anim/scale.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:interpolator="@android:anim/accelerate_interpolator">
 4     <!-- 伸缩,从不可见状态变化到原本状态,持续时间为5秒 -->
 5     <scale
 6         android:fromXScale="0.0"
 7         android:toXScale="1.0"
 8         android:fromYScale="0.0"
 9         android:toYScale="1.0"
10         android:pivotX="50%"
11         android:pivotY="50%"
12         android:duration="5000"
13     />
14 </set>

anim/rotate.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:interpolator="@android:anim/accelerate_interpolator">
 4     <!-- 旋转,根据自己的中心位置旋转,持续时间为5秒 -->
 5     <rotate 
 6         android:fromDegrees="0" 
 7         android:toDegrees="+350"
 8         android:pivotX="50%" 
 9         android:pivotY="50%" 
10         android:duration="5000" />
11 </set>

MainActivity.java

 1 package com.b510;
 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.Animation;
 8 import android.view.animation.AnimationUtils;
 9 import android.widget.Button;
10 import android.widget.ImageView;
11 
12 /**
13  * 
14  * @author Hongten
15  * 
16  */
17 public class MainActivity extends Activity {
18     /** 图片显示 */
19     private ImageView imageView;
20     /** 旋转 */
21     private Button rotate;
22     /** 淡入淡出 */
23     private Button alpha;
24     /** 伸缩 */
25     private Button scale;
26     /** 移动位置 */
27     private Button translate;
28 
29     /** Called when the activity is first created. */
30     @Override
31     public void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         setContentView(R.layout.main);
34 
35         imageView = (ImageView) findViewById(R.id.imageViewId);
36 
37         rotate = (Button) findViewById(R.id.btn_rotate);
38         alpha = (Button) findViewById(R.id.btn_alpha);
39         scale = (Button) findViewById(R.id.btn_scale);
40         translate = (Button) findViewById(R.id.btn_translate);
41 
42         rotate.setOnClickListener(new OnClickListener() {
43             @Override
44             public void onClick(View v) {
45                 // 使用AnimationUtils装载动画设置
46                 Animation animation = AnimationUtils.loadAnimation(
47                         MainActivity.this, R.anim.rotate);
48                 // 使用imageView对象的startAnimation方法执行动画
49                 imageView.startAnimation(animation);
50             }
51         });
52         alpha.setOnClickListener(new OnClickListener() {
53             @Override
54             public void onClick(View v) {
55                 // 使用AnimationUtils装载动画设置
56                 Animation animation = AnimationUtils.loadAnimation(
57                         MainActivity.this, R.anim.alpha);
58                 // 使用imageView的startAnimation方法执行动画
59                 imageView.startAnimation(animation);
60             }
61         });
62         scale.setOnClickListener(new OnClickListener() {
63             @Override
64             public void onClick(View v) {
65                 // 使用AnimationUtils装载动画设置
66                 Animation animation = AnimationUtils.loadAnimation(
67                         MainActivity.this, R.anim.scale);
68                 // imageView对象调用startAnimation方法执行动画
69                 imageView.startAnimation(animation);
70             }
71         });
72         translate.setOnClickListener(new OnClickListener() {
73             @Override
74             public void onClick(View v) {
75                 // 使用AnimationUtils装载动画设置
76                 Animation animation = AnimationUtils.loadAnimation(
77                         MainActivity.this, R.anim.translate);
78                 // imageView对象调用startAnimation方法执行动画
79                 imageView.startAnimation(animation);
80             }
81         });
82     }
83 }

运行效果

1.初始化

http://www.cnblogs.com/hongten/gallery/image/112170.html

2.scale效果

http://www.cnblogs.com/hongten/gallery/image/112171.html

3.rotate效果

http://www.cnblogs.com/hongten/gallery/image/112172.html

4.alpha效果

http://www.cnblogs.com/hongten/gallery/image/112173.html

5.translate效果

http://www.cnblogs.com/hongten/gallery/image/112174.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-02-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档