Animation 动画分为三种
代码如下:直接粘贴复制即可
帧动画简单的由N种动画收集起来,通过控制依次显示这些图片,Android中实现帧动画,一般我们会用到AnimationDrawable 先编写好Drawable,然后代码中调用start()以及stop()开始或停止播放动画
补间动画 补间动画相当于有五种类型,或者是有四种吧,第五种类型是自己设置的,
属性动画 属性动画是3.0以后引入的新特效
帧动画
首先编写我们的动画文件,先在res下创建一个anim目录,接着在anim下创建 动画文件:miao.xml这样的格式:
这里的android:oneshot是设置动画是否只是播放一次,true只播放一次,false循环播放!
属性动画
可以改变文件的属性 位移 旋转 渐变 缩放
补间动画
不可以改变文件的属性 位移 旋转 渐变 缩放
anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="@drawable/ic_launcher"
android:duration="150">
</item>
<item
android:drawable="@drawable/introduce_01"
android:duration="150">
</item>
<item
android:drawable="@drawable/introduce_02"
android:duration="150">
</item>
</animation-list>
主界面
package com.example.animation_demo;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
private ImageView mImg;
private Button mBtn1,mBtn2;
private boolean isFlag=true;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mImg=(ImageView) findViewById(R.id.mImg);
mBtn1=(Button) findViewById(R.id.mBtn1);
mBtn2=(Button) findViewById(R.id.mBtn2);
mBtn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mImg.setImageResource(R.drawable.anim);
animationDrawable=(AnimationDrawable) mImg.getDrawable();
animationDrawable.start();
}
});
mBtn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
animationDrawable=(AnimationDrawable) mImg.getDrawable();
animationDrawable.stop();
}
});
}
}
补间动画
AlphaAnimation:透明度渐变效果,创建时许指定开始以及结束透明度,还有动画的持续 时间,透明度的变化范围(0,1),0是完全透明,1是完全不透明;对应<alpha/>标签!
ScaleAnimation:缩放渐变效果,创建时需指定开始以及结束的缩放比,以及缩放参考点, 还有动画的持续时间;对应<scale/>标签!
TranslateAnimation:位移渐变效果,创建时指定起始以及结束位置,并指定动画的持续 时间即可;对应<translate/>标签!
RotateAnimation:旋转渐变效果,创建时指定动画起始以及结束的旋转角度,以及动画 持续时间和旋转的轴心;对应<rotate/>标签
AnimationSet:组合渐变,就是前面多种渐变的组合,对应<set/>标签
/*
一下代码属于res下的anim文件夹
*/
XML
alpha_anim
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromAlpha="1"
android:toAlpha="0"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="2"
>
</alpha>
rotate_anim
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="1"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="2"
>
</rotate>
scale_anim
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:fromXScale="1"
android:toXScale="3"
android:fromYScale="1"
android:toYScale="3"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="2"
>
</scale>
translate_anim
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="1"
android:fromYDelta="1"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="2"
android:toXDelta="100"
android:toYDelta="100" >
</translate>
set_anim
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:interpolator="@android:anim/linear_interpolator"
>
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0"
/>
<translate
android:duration="3000"
android:fromXDelta="0"
android:toXDelta="100"
/>
<rotate
android:duration="3000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
/>
</set>
activity_main.xml
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/mBtn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="渐变"
/>
<Button
android:id="@+id/mBtn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="伸缩"
/>
<Button
android:id="@+id/mBtn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="位移"
/>
<Button
android:id="@+id/mBtn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="旋转"
/>
<Button
android:id="@+id/mBtn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="set"
/>
<ImageView
android:id="@+id/mImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</LinearLayout>
主界面
MainActivity
package cn.bgs.anim_xml;
import java.nio.channels.AlreadyConnectedException;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
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 implements OnClickListener {
private Button mBtn1,mBtn2,mBtn3,mBtn4,mBtn5;
private ImageView mImg;
private AlphaAnimation alpha;
private ScaleAnimation scale;
private TranslateAnimation translat;
private RotateAnimation rotate;
private AnimationUtils utils;//动画工具类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
utils=new AnimationUtils();
initView();
}
private void initView() {
mBtn1=(Button) findViewById(R.id.mBtn1);
mBtn2=(Button) findViewById(R.id.mBtn2);
mBtn3=(Button) findViewById(R.id.mBtn3);
mBtn4=(Button) findViewById(R.id.mBtn4);
mBtn5=(Button) findViewById(R.id.mBtn5);
mImg=(ImageView) findViewById(R.id.mImg);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
mBtn4.setOnClickListener(this);
mBtn5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int ID=v.getId();
switch (ID) {
case R.id.mBtn1:
alpha=(AlphaAnimation) utils.loadAnimation(this, R.anim.alpha_anim);
mImg.startAnimation(alpha);
break;
case R.id.mBtn2:
scale=(ScaleAnimation) utils.loadAnimation(this, R.anim.scale_anim);
mImg.startAnimation(scale);
break;
case R.id.mBtn3:
translat=(TranslateAnimation) utils.loadAnimation(this, R.anim.translate_anim);
mImg.startAnimation(translat);
break;
case R.id.mBtn4:
rotate=(RotateAnimation) utils.loadAnimation(this, R.anim.rotate_anim);
mImg.startAnimation(rotate);
break;
case R.id.mBtn5:
AnimationSet set=(AnimationSet) utils.loadAnimation(this, R.anim.set_anim);
mImg.startAnimation(set);
break;
}
}
}