以xml的方式实现动画

1、java代码

 1 package com.example.tweenanim;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.view.View;
 7 import android.view.animation.AlphaAnimation;
 8 import android.view.animation.Animation;
 9 import android.view.animation.AnimationSet;
10 import android.view.animation.AnimationUtils;
11 import android.view.animation.RotateAnimation;
12 import android.view.animation.ScaleAnimation;
13 import android.view.animation.TranslateAnimation;
14 import android.widget.ImageView;
15 
16 public class MainActivity extends Activity {
17     private ImageView iv;
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         iv = (ImageView) findViewById(R.id.iv);
23     }
24 
25     //透明度
26     public void click1(View view){
27         Animation animation = AnimationUtils.loadAnimation(this,R.anim.alpha_anim);
28         iv.startAnimation(animation);
29     }
30     //缩放
31     public void click2(View view){
32         Animation animation = AnimationUtils.loadAnimation(this,R.anim.scale_anim);
33         iv.startAnimation(animation);
34     }
35     //旋转
36     public void click3(View view){
37         Animation animation = AnimationUtils.loadAnimation(this,R.anim.rotate_anim);
38         iv.startAnimation(animation);
39     }
40     //平移
41     public void click4(View view){
42         Animation animation = AnimationUtils.loadAnimation(this,R.anim.translate_anim);
43         iv.startAnimation(animation);
44     }
45     //组合
46     public void click5(View view){
47         AnimationSet set = new AnimationSet(false);
48         
49         TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
50         ta.setDuration(2000);
51         ta.setRepeatCount(2);
52         ta.setRepeatMode(Animation.REVERSE);
53         
54         RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
55         ra.setDuration(2000);
56         ra.setRepeatCount(2);
57         ra.setRepeatMode(Animation.REVERSE);
58         
59         ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
60         sa.setDuration(2000);
61         sa.setRepeatCount(2);
62         sa.setRepeatMode(Animation.REVERSE);
63         
64         set.addAnimation(ta);
65         set.addAnimation(ra);
66         set.addAnimation(sa);
67         iv.startAnimation(set);
68     }
69 
70 }

2、动画xml,需要在res目录下新建anim文件夹,然后把以下的xml文件放进去

1)透明度alpha_anim.xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <alpha xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromAlpha="1.0"
 4     android:toAlpha="0.5"
 5     android:fillAfter="true"
 6     android:duration="2000"
 7     >
 8     
 9 
10 </alpha>

2)缩放scale_anim.xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <scale xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromXScale="0.2" 
 4     android:toXScale="2.0"
 5     android:fromYScale="0.2"
 6     android:toYScale="2.0"
 7     android:pivotX="50%"
 8     android:pivotY="50%"
 9     android:duration="2000"
10     >
11     
12 
13 </scale>

3)旋转rotate_anim.xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <rotate xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromDegrees="0"
 4     android:toDegrees="360"
 5     android:pivotX="50%"
 6     android:pivotY="50%"
 7     android:duration="2000"
 8     >
 9     
10 
11 </rotate>

4)平移translate_anim.xml文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <translate xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:fromXDelta="20%p"
 4     android:toXDelta="100%p"
 5     android:fromYDelta="20%p"
 6     android:toYDelta="100%p"
 7     android:duration="2000"
 8     >
 9     
10 
11 </translate>

 注意:%后面的p说明是相对于父窗口;如果没有p,则相对于它自己

转载于:https://www.cnblogs.com/zhongyinghe/p/5365780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值