ANDROID_MARS学习笔记_S02_008_ANIMATION第二种使用方式:xml

一、简介

  

二、代码
1.resanim下的xml
(1)alpha.xml.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     <alpha 
5         android:fromAlpha="1.0"
6         android:toAlpha="0.0"
7         android:startOffset="500"
8         android:duration="1000"/>
9 </set>

(2)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     <rotate 
 5         android:fromDegrees="0"
 6         android:toDegrees="720"
 7         android:pivotX="50%"
 8         android:pivotY="100%"
 9         android:duration="3000"/>
10 </set>

(3)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     <scale 
 5         android:fromXScale="1.0"
 6         android:toXScale="0.0"
 7         android:fromYScale="1.0"
 8         android:toYScale="0.0"
 9         android:pivotX="50%"
10         android:pivotY="50%"
11         android:duration="2000"/>
12     
13 </set>

(4)tranlate.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     <translate 
 5         android:fromXDelta="50%"
 6         android:toXDelta="100%"
 7         android:fromYDelta="0%"
 8         android:toYDelta="100%"
 9         android:duration="2000"/>
10 </set>

2.java
(1)MainActivity.java

 1 package com.animation2;
 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 public class MainActivity extends Activity {
13 
14     private ImageView imageView = null;
15     private Button rotateButton = null;
16     private Button scaleButton = null;
17     private Button alphaButton = null;
18     private Button translateButton = null;
19     
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         rotateButton.setOnClickListener(new RotateButtonListener());
29 
30         scaleButton = (Button) findViewById(R.id.scaleButtonId);
31         scaleButton.setOnClickListener(new ScaleButtonListener());
32 
33         alphaButton = (Button) findViewById(R.id.alphaButtonId);
34         alphaButton.setOnClickListener(new AlphaButtonListener());
35 
36         translateButton = (Button) findViewById(R.id.translateButtonId);
37         translateButton.setOnClickListener(new TranslateButtonListener());
38     }
39     
40     class TranslateButtonListener implements OnClickListener {
41         @Override
42         public void onClick(View v) {
43             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.tranlate);
44             imageView.startAnimation(animation);
45         }
46     }
47 
48     class AlphaButtonListener implements OnClickListener {
49         @Override
50         public void onClick(View v) {
51             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
52             imageView.startAnimation(animation);
53         }
54     }
55 
56     class ScaleButtonListener implements OnClickListener {
57         @Override
58         public void onClick(View v) {
59             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
60             imageView.startAnimation(animation);
61         }
62     }
63 
64     class RotateButtonListener implements OnClickListener {
65         @Override
66         public void onClick(View v) {
67             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
68             imageView.startAnimation(animation);
69         }
70     }
71 }

 

 

原文地址:https://www.cnblogs.com/shamgod/p/5201215.html