您的位置:首页 > 其它

022_01Animation简单应用

2015-06-04 20:25 393 查看
Android提供了2种动画:

  一. Frame动画,即顺序播放事先做好的图像,跟放胶片电影类似。

    开发步骤:1,把准备好的图片放进项目res/ drawable下

         2,定义动画XML文件。当然也可以采用编码方式定义动画效果(使用AnimationDrawable类)

         3,为View控件绑定动画效果。 iv.setImageResource(R.drawable.myframeanim)

  二. Tween动画,通过对 View 的内容进行一系列的图形变换 (包括平移、缩放、旋转、改变透明度)来实现动画效    果。动画效果的定义可以采用XML来做也可以采用编码来做。Tween动画有4种类型:



overridePendingTransition简介

   Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画。

  它包括两个部分:一部分是第一个activity退出时的动画;

          另外一部分时第二个activity进入时的动画;

  可以用overridePendingTransition(int enterAnim, int exitAnim)来实现

  //实现淡入浅出的效果

    overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);

  // 由左向右滑入的效果
    overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);

  // 实现zoommin 和 zoomout (自定义的动画)

    overridePendingTransition(R.anim.zoomin, R.anim.zoomout);

Frame动画开发过程:

   1,把准备好的图片放进项目res/ drawable下(使用在线分解GIF动态图片工具将GIF分解成多张图片,比如
http://www.360doc.com/content/13/0314/18/699582_271506280.shtml)


  2,定义动画xml文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:drawable="@drawable/a1"
android:duration="100"/>
<item
android:drawable="@drawable/a2"
android:duration="100"/>
<item
android:drawable="@drawable/a3"
android:duration="100"/>
<item
android:drawable="@drawable/a4"
android:duration="100"/>
<item
android:drawable="@drawable/a5"
android:duration="100"/>
<item
android:drawable="@drawable/a6"
android:duration="100"/>
<item
android:drawable="@drawable/a7"
android:duration="100"/>
<item
android:drawable="@drawable/a8"
android:duration="100"/>
<item
android:drawable="@drawable/a9"
android:duration="100"/>
<item
android:drawable="@drawable/a10"
android:duration="100"/>
<item
android:drawable="@drawable/a11"
android:duration="100"/>
<item
android:drawable="@drawable/a12"
android:duration="100"/>
<item
android:drawable="@drawable/a13"
android:duration="100"/>
<item
android:drawable="@drawable/a14"
android:duration="100"/>
<item
android:drawable="@drawable/a11"
android:duration="100"/>
<item
android:drawable="@drawable/a15"
android:duration="100"/>
<item
android:drawable="@drawable/a11"
android:duration="100"/>
<item
android:drawable="@drawable/a16"
android:duration="100"/>
<item
android:drawable="@drawable/a11"
android:duration="100"/>
<item
android:drawable="@drawable/a17"
android:duration="100"/>
<item
android:drawable="@drawable/a11"
android:duration="100"/>
<item
android:drawable="@drawable/a18"
android:duration="100"/>

</animation-list>


  

3,为View控件绑定动画效果。

iv.setImageResource(R.drawable.animation);


源代码

package com.example.day22_01animationdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv_anim);
}

public void playanimation(View v){
iv.setImageResource(R.drawable.animation);
}
/**
* @param v
* 透明渐变效果动画
*  1.0 means fully opaque 完全不透明
*  0.0 means fully transparent. 完全透明
*/
public void alpha(View v){
AlphaAnimation aa =  new AlphaAnimation(1, 0);
aa.setDuration(2000);  //设置每一次播放的时间 第一帧到最后一帧
aa.setFillAfter(true); //设置播放完毕之后 停留在哪一个帧 true表示最后一帧
aa.setRepeatCount(2); //不包含你的第一次播放 ,表示再重复的次数
aa.setRepeatMode(Animation.REVERSE); //表示你需要重复的第二次 是重新开始,还是从上一次的结束帧开始
iv.setAnimation(aa);
}

/**
* @param v
* 缩放动画
* 步骤 1.把需要的动画效果 自己设定好
*    2.将该动画效果使用 setAnimation 设置到需要应用该动画的imageview上
* Animation.ABSOLUTE, 绝对的  x坐标就需要填像素 0 0
* Animation.RELATIVE_TO_SELF,  相对于本身
* Animation.RELATIVE_TO_PARENT.
*/
public void scale(View v){
ScaleAnimation  sa = new ScaleAnimation(1, 2, 1, 2,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
sa.setFillAfter(true);
sa.setRepeatCount(1);
sa.setRepeatMode(Animation.REVERSE);
sa.setDuration(2000);
iv.setAnimation(sa);

}

public void transfrom(View v){
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);

ta.setFillAfter(true);
ta.setDuration(2000);
ta.setRepeatCount(1);
ta.setRepeatMode(Animation.REVERSE);
iv.setAnimation(ta);
}

public void rotate(View v){
RotateAnimation ra = new RotateAnimation(
0, 3600,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
ra.setDuration(5000);
iv.setAnimation(ra);
}
}




<LinearLayout 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="com.example.day22_01animationdemo.MainActivity"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放动画"
android:onClick="playanimation" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明渐变动画"
android:onClick="alpha" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放渐变动画"
android:onClick="scale" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移动渐变动画"
android:onClick="transfrom" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转渐变动画"
android:onClick="rotate" />

<ImageView
android:id="@+id/iv_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a"/>
</LinearLayout>


两种方式播放合成效果的动画以及Activity切换时的效果代码:

  建立动态播放格式的xml文件。一种是建立两个XML文件用于设定不同效果,一种是将两种效果的设定放在同一个xml文件中。

/Day22_01AnimationDemo/res/anim/alpha_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="500"
android:fillAfter="true"  >
</alpha>


/Day22_01AnimationDemo/res/anim/scale_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:toXScale="2"
android:fromYScale="1"
android:toYScale="2"
android:pivotX="0.5"
android:pivotY="0.5"
android:duration="500" >
</scale>


/Day22_01AnimationDemo/res/anim/myanimation.xml

<?xml version="1.0" encoding="utf-8"?>
<set>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromAlpha="1"
android:repeatCount="2"
android:repeatMode="reverse"
android:toAlpha="0" >
</alpha>

<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="0.5"
android:pivotY="0.5"
android:repeatCount="2"
android:toXScale="2"
android:toYScale="2" >
</scale>
</set>


package com.example.day22_01animationdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv_anim);
}

public void animset(View v){
/*AnimationSet as = new AnimationSet(true);
AlphaAnimation aa =     (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
ScaleAnimation sa =     (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.scale_anim);
as.addAnimation(aa);
as.addAnimation(sa);
iv.setAnimation(as);*/

AnimationSet as = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.myanimation);
iv.setAnimation(as);
}

public void jump(View v){
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);

/*enterAnim A resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnim  A resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.*/
//overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
}




<LinearLayout 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="com.example.day22_01animationdemo.MainActivity222"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动画合成效果"
android:onClick="animset" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换到下一个Activity"
android:onClick="jump" />
<ImageView
android:id="@+id/iv_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a"/>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: