android动画

来源:互联网 发布:快手特效软件大全 编辑:程序博客网 时间:2024/05/14 21:17

Android提供了三类动画,视图动画,Drawale动画,属性动画。

视图动画就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。

相关类:
Animation   动画
AlphaAnimation 渐变透明度
RotateAnimation 画面旋转
ScaleAnimation 渐变尺寸缩放
TranslateAnimation 位置移动
AnimationSet 动画集

一、视图动画

参数详细说明:

表二

XML节点功能说明alpha渐变透明度动画效果<alpha
android:fromAlpha=”0.1″
android:toAlpha=”1.0″
android:duration=”3000″ />fromAlpha

属性为动画起始时透明度

0.0表示完全透明
1.0表示完全不透明
以上值取0.0-1.0之间的float数据类型的数字toAlpha属性为动画结束时透明度

表三

scale渐变尺寸伸缩动画效果<scale
android:interpolator= “@android:anim/accelerate_decelerate_interpolator”
android:fromXScale=”0.0″
android:toXScale=”1.4″
android:fromYScale=”0.0″
android:toYScale=”1.4″
android:pivotX=”50%”
android:pivotY=”50%”
android:fillAfter=”false”
android:startOffset=“700”
android:duration=”700″
android:repeatCount=”10″ />fromXScale[float] fromYScale[float]为动画起始时,X、Y坐标上的伸缩尺寸0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大toXScale [float]
toYScale[float]为动画结束时,X、Y坐标上的伸缩尺寸pivotX[float]
pivotY[float]为动画相对于物件的X、Y坐标的开始位置属性值说明:从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置    

表四

translate画面转换位置移动动画效果<translate
android:fromXDelta=”30″
android:toXDelta=”-80″
android:fromYDelta=”30″
android:toYDelta=”300″
android:duration=”2000″ />fromXDelta
toXDelta为动画、结束起始时 X坐标上的位置 fromYDelta
toYDelta为动画、结束起始时 Y坐标上的位置     

表五

rotate画面转移旋转动画效果<rotate
android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
android:fromDegrees=”0″
android:toDegrees=”+350″
android:pivotX=”50%”
android:pivotY=”50%”
android:duration=”3000″ />fromDegrees为动画起始时物件的角度说明
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)toDegrees属性为动画结束时物件旋转的角度 可以大于360度pivotX
pivotY为动画相对于物件的X、Y坐标的开始位说明:以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置    

 

二、Drawale动画

Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似

实例:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <LinearLayout        android:id="@+id/linear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical" >        <LinearLayout            android:id="@+id/linear"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal" >            <Button                android:id="@+id/btn_alpha"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/alpha" />            <Button                android:id="@+id/btn_rotate"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/rotate" />            <Button                android:id="@+id/btn_scale"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/scale" />            <Button                android:id="@+id/btn_translate"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/translate" />            <Button                android:id="@+id/btn_set"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/set" />            <Button                android:id="@+id/btn_drawable_animation"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/animation_xml" />            <CheckBox                android:id="@+id/cb"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="20dip"                android:text="@string/animation_xml" />        </LinearLayout>        <LinearLayout            android:id="@+id/linear22"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="20dip"            android:orientation="horizontal" >            <ImageView                android:id="@+id/img"                android:layout_width="wrap_content"                android:layout_height="wrap_content" >            </ImageView>        </LinearLayout>    </LinearLayout></RelativeLayout>


MainActivity.java

package com.example.animation_test;import android.os.Bundle;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;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.CheckBox;import android.widget.ImageView;import android.widget.LinearLayout;public class MainActivity extends Activity implements OnClickListener {private Button alpha = null;private Button rotate = null;private Button scale = null;private Button translate = null;private Button set = null;private Button drawable_animation = null;private LinearLayout ll = null;private CheckBox cb = null;ImageView iv = null;private Animation alphaAnimation = null;private Animation rotateAnimation = null;private Animation scaleAnimation = null;private Animation translateAnimation = null;private Animation setAnimation = null;private AnimationDrawable drawableAnimation = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);alpha = (Button) findViewById(R.id.btn_alpha);alpha.setOnClickListener(this);rotate = (Button) findViewById(R.id.btn_rotate);rotate.setOnClickListener(this);scale = (Button) findViewById(R.id.btn_scale);scale.setOnClickListener(this);translate = (Button) findViewById(R.id.btn_translate);translate.setOnClickListener(this);set = (Button) findViewById(R.id.btn_set);set.setOnClickListener(this);iv = (ImageView) findViewById(R.id.img);iv.setBackground(getResources().getDrawable(R.drawable.test));drawable_animation = (Button) findViewById(R.id.btn_drawable_animation);drawable_animation.setOnClickListener(this);cb = (CheckBox) findViewById(R.id.cb);ll = (LinearLayout) findViewById(R.id.linear22);//ll.addView(rv);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (arg0.getId()) {case R.id.btn_alpha:if (!cb.isChecked()) {alphaAnimation = new AlphaAnimation(0.0f, 1.0f); //alpha动画 不可见-->可见alphaAnimation.setDuration(3000);} else {alphaAnimation = AnimationUtils.loadAnimation(this,R.anim.alpha);}iv.startAnimation(alphaAnimation);break;case R.id.btn_rotate:if (!cb.isChecked()) {rotateAnimation = new RotateAnimation(0f, 90f);//旋转动画  旋转90度rotateAnimation.setDuration(3000);} else {rotateAnimation = AnimationUtils.loadAnimation(this,R.anim.rotate);}iv.startAnimation(rotateAnimation);break;case R.id.btn_scale:if (!cb.isChecked()) {scaleAnimation = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.1f);//缩放动画  y缩放到0.1scaleAnimation.setDuration(3000);} else {scaleAnimation = AnimationUtils.loadAnimation(this,R.anim.scale);}iv.startAnimation(scaleAnimation);break;case R.id.btn_translate:if (!cb.isChecked()) {translateAnimation = new TranslateAnimation(20.0f, 100.0f,//平移动画  从(20,20)移动到(100, 100)20.0f, 100.0f);translateAnimation.setDuration(3000);} else {translateAnimation = AnimationUtils.loadAnimation(this,R.anim.translate);}iv.startAnimation(translateAnimation);break;case R.id.btn_set:AnimationSet set = new AnimationSet(true);if (!cb.isChecked()) {scaleAnimation = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.5f);//缩放动画// rotateAnimation = new RotateAnimation(0f, 90f);alphaAnimation = new AlphaAnimation(0.0f, 1.0f);//alpha动画set.addAnimation(scaleAnimation);// set.addAnimation(rotateAnimation);set.addAnimation(alphaAnimation);set.setDuration(3000);}else{set = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.set);}iv.startAnimation(set);break;case R.id.btn_drawable_animation:drawableAnimation = new AnimationDrawable();  /*for (int i = 0; i < 10; i++) {              int id = getResources().getIdentifier("dd_" + (i+1), "drawable", getPackageName());              drawableAnimation.addFrame(getResources().getDrawable(id), 100);          }  *///ll.removeView(rv);//ImageView iv = new ImageView(this);//ll.addView(iv, LayoutParams.WRAP_CONTENT);drawableAnimation.addFrame(getResources().getDrawable(R.drawable.dd_1), 100);  //添加要播放的动画drawableAnimation.addFrame(getResources().getDrawable(R.drawable.dd_2), 100);  drawableAnimation.addFrame(getResources().getDrawable(R.drawable.dd_3), 100);  drawableAnimation.addFrame(getResources().getDrawable(R.drawable.dd_4), 100);  drawableAnimation.addFrame(getResources().getDrawable(R.drawable.dd_5), 100);  drawableAnimation.addFrame(getResources().getDrawable(R.drawable.dd_6), 100);  drawableAnimation.setOneShot(false);//设置循环播放   false表示循环  true表示不循环,仅播放一次  iv.setBackground(drawableAnimation);drawableAnimation.start();break;}}}

相应的动画xml文件:

alpha.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="1000"        android:fromAlpha="0.1"        android:toAlpha="1.0" /></set>
rotate.xml


<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <rotate        android:duration="500"        android:fromDegrees="0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="50%"         android:pivotY="50%"        android:toDegrees="360" /></set>

scale.xml

<?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android">      <scale          android:interpolator="@android:anim/accelerate_decelerate_interpolator"          android:fromXScale="0.0"          android:toXScale="1.0"          android:fromYScale="1.0"          android:toYScale="1.0"          android:pivotX="50%"          android:pivotY="50%"          android:fillAfter="false"          android:duration="2000"      />     </set> 

translate.xml

<?xml version="1.0" encoding="utf-8"?>  <set xmlns:android="http://schemas.android.com/apk/res/android">      <translate          android:fromXDelta="10"          android:toXDelta="100"          android:fromYDelta="10"          android:toYDelta="100"          android:duration="2000"      />  </set>  

set.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <scale        android:duration="8000"        android:fillAfter="false"        android:fromXScale="0.0"        android:fromYScale="1.0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.0"        android:toYScale="1.0" />    <rotate        android:duration="8000"        android:fromDegrees="0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:startOffset="16000"        android:toDegrees="360" /></set>

效果图:

code:

http://download.csdn.net/detail/new_abc/7971265

参考:

http://blog.csdn.net/feng88724/article/details/6320507

http://blog.csdn.net/feng88724/article/details/6318430


0 0
原创粉丝点击