原创:kylin_zeng http://blog.chinaunix.net/uid/23795897.html在此感谢mars 老师的帮助。转载请注明原创出处,尊重他人的劳动成果。
二、第二种使用:
1、在res文件夹厦门新建一个anim的文件夹;
2、创建xml文件,并首先要加入set标签,如下:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
</set>
3、在该标签当中加入rotate,alpha,scale或者translate标签。
4、在代码中使用AnimationUtils当中转载xml 文件,并生产Animation对象。
例如:
1、在res下面建立一个anim文件夹。
2、里面创建了四个xml文件,alpha.xml,
点击(此处)折叠或打开
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
-
- <alpha
- android:fromAlpha="1.0"
- android:toAlpha="0.0"
- android:startOffset="500"
- android:duration="500" />
-
- </set>
点击(此处)折叠或打开
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
- <rotate android:fromDegrees="0"
- android:toDegrees="360"
- android:pivotX="50%"
- //这里有三种表示方法:50 :是绝对坐标,表示屏幕上真正的50,对应代码控制中的toplant,
- // 50% : 是相对坐标,表示相对自身控件的50%,即控件的中心点。
- // 50%p: 是相对于父控件,表示父控件的50%位置。
- android:pivotY="50%"
- android:duration="5000" />
- </set>
scale.xml,
点击(此处)折叠或打开
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
- <scale android:fromXScale="1.0"
- android:toXScale="0.0"
- android:fromYScale="1.0"
- android:toYScale="0.0"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="2000" />
-
- </set>
translate.xml
点击(此处)折叠或打开
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator">
-
- <translate
- android:fromXDelta="50%"
- android:toXDelta="100%"
- android:fromYDelta="0%"
- android:toYDelta="100%"
- android:duration="2000" />
-
- </set>
4、调用:
点击(此处)折叠或打开
- package mars.animations02;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.AnimationSet;
- import android.view.animation.AnimationUtils;
- import android.view.animation.RotateAnimation;
- import android.view.animation.ScaleAnimation;
- import android.view.animation.TranslateAnimation;
- import android.widget.Button;
- import android.widget.ImageView;
-
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- private ImageView imageView = null;
- private Button rotateButton = null;
- private Button scaleButton = null;
- private Button alphaButton = null;
- private Button translateButton = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- imageView = (ImageView) findViewById(R.id.imageViewId);
-
- rotateButton = (Button) findViewById(R.id.rotateButtonId);
- rotateButton.setOnClickListener(new RotateButtonListener());
-
- scaleButton = (Button) findViewById(R.id.scaleButtonId);
- scaleButton.setOnClickListener(new ScaleButtonListener());
-
- alphaButton = (Button) findViewById(R.id.alphaButtonId);
- alphaButton.setOnClickListener(new AlphaButtonListener());
-
- translateButton = (Button) findViewById(R.id.translateButtonId);
- translateButton.setOnClickListener(new TranslateButtonListener());
- }
-
- private class RotateButtonListener implements OnClickListener {
-
- @Override
- public void onClick(View view) {
- Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
- imageView.startAnimation(animation);
- }
- }
-
- private class ScaleButtonListener implements OnClickListener {
-
- @Override
- public void onClick(View view) {
- Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.sacle);
- imageView.startAnimation(animation);
- }
-
- }
-
- private class AlphaButtonListener implements OnClickListener {
-
- @Override
- public void onClick(View view) {
- //使用AnimationUtils装载动画设置文件
- Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
- imageView.startAnimation(animation);
- }
-
- }
-
- private class TranslateButtonListener implements OnClickListener {
-
- @Override
- public void onClick(View view) {
- Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
- imageView.startAnimation(animation);
- }
-
- }
- }
02_09.zip 原文链接:https://www.f2er.com/xml/297494.html