新建工程 myFrameAnimation
在main.xml布局中添加view子类,调整一下,效果如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:prientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:prientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/bt_group_back" android:layout_marginTop="10px">
<Button android:text="播放动画" android:layout_width="100px"
android:id="@+id/Button_start" android:layout_height="fill_parent"></Button>
<Button android:layout_width="100px" android:text="停止动画"
android:id="@+id/Button_stop" android:layout_height="fill_parent"></Button>
<CheckBox android:text="动画重复" android:layout_width="100px"
android:id="@+id/CheckBox_ifCycle_orNot" style="?android:attr/starStyle"
android:layout_height="fill_parent"></CheckBox>
</LinearLayout>
<ImageView android:id="@+id/rocket_image"
android:layout_width="80px" android:layout_height="80px"
android:background="@drawable/android_large"
android:layout_marginLeft="100dp" android:layout_marginTop="100dp"></ImageView>
</LinearLayout>
找几个动态图片,把它分成单个图。(主要是为了讲解/是用FrameAnimation效果,AnimationDrawable)
修改mainActivity.java的代码
package zyf.my.frame.animation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;
public class myFrameAnimatino extends Activity implements Button.OnClickListener {
/** Called when the activity is first created. */
AnimationDrawable frameAnimation;
/*
* 声明AnimationDrawable 可绘制动画 对象frameAnimation
*/
ImageView myImage;
/*
* 图片View ImageView
*/
Button start,stop;
CheckBox Cycle;
boolean isChecked_cycle=true;
/*
* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start=(Button) findViewById(R.id.Button_start);
stop=(Button) findViewById(R.id.Button_stop);
Cycle=(CheckBox) findViewById(R.id.CheckBox_ifCycle_orNot);
/*
* findViewById()从XML中获取 Button CheckBox
*/
myImage = (ImageView) findViewById(R.id.rocket_image);
/*
* findViewById()从XML中获取ImageView 对象myImage
*/
myImage.setBackgroundResource(R.anim.myframeanimation);
/*
* ImageView.setBackgroundResource()设置 图片View的背景图片
* 这里是把帧动画 myframeanimation加到 图片View的背景中
*/
frameAnimation = (AnimationDrawable) myImage.getBackground();
/*
* myImage.getBackground()获得背景的Drawable的对象,转换成AnimationDrawable
*/
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
frameAnimation.setOneShot(isChecked_cycle);
/*
* 添加触摸事件处理方法
*/
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_DOWN){
/*
* MotionEvent.getAction()获取事件动作
* MotionEvent.ACTION_DOWN 向下的手势动作
*/
/*event.getAction() 返回正被执行的动作种类:
* 是 ACTION_DOWN, ACTION_MOVE, ACTION_UP, 或 ACTION_CANCEL中的一个.
*/
frameAnimation.start();
/*
* 启动帧动画效果
*/
return true;
}
return super.onTouchEvent(event);
}
/*
* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View button) {
// TODO Auto-generated method stub
switch (button.getId()) {
case R.id.Button_start:{
if(Cycle.isChecked()){
Toast.makeText(this, "动画重复", Toast.LENGTH_LONG).show();
isChecked_cycle=false;
}else{
Toast.makeText(this, "不重复", Toast.LENGTH_LONG).show();
isChecked_cycle=true;
}
/*
* 复选按钮选中, 动画重复播放, AnimationDrawable.setOneShot(false)
* 复选按钮未选中,动画不重复播放,AnimationDrawable.setOneShot(true)
*/
frameAnimation.setOneShot(isChecked_cycle);
/*
* 设置重复与否
*/
frameAnimation.start();
/*
*启动帧动画效果
*/
}
break;
case R.id.Button_stop:{
if(frameAnimation.isRunning()){
/*
* AnimationDrawable.isRunning(),判断帧动画是否在运行,true---运行中
* 如果动画正在运行,可以停止
*/
frameAnimation.stop();
/*
*停止帧动画效果
*/
}
}
break;
default:
break;
}
}
}
myframeanimation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/rkof" android:duration="200" ></item>
<item android:drawable="@drawable/qkof" android:duration="200" />
<item android:drawable="@drawable/pkof" android:duration="200" />
<item android:drawable="@drawable/okof" android:duration="200" />
<item android:drawable="@drawable/nkof" android:duration="200" />
<item android:drawable="@drawable/mkof" android:duration="200" />
<item android:drawable="@drawable/lkof" android:duration="200" />
<item android:drawable="@drawable/kkof" android:duration="200" />
<item android:drawable="@drawable/jkof" android:duration="200" />
<item android:drawable="@drawable/ikof" android:duration="200" />
<item android:drawable="@drawable/hkof" android:duration="200" />
<item android:drawable="@drawable/gkof" android:duration="200" />
<item android:drawable="@drawable/fkof" android:duration="200" />
<item android:drawable="@drawable/ekof" android:duration="200" />
<item android:drawable="@drawable/dkof" android:duration="200" />
<item android:drawable="@drawable/ckof" android:duration="200" />
<item android:drawable="@drawable/bkof" android:duration="200" />
<item android:drawable="@drawable/akof" android:duration="200" />
</animation-list>
<!-- android:oneshot属性为true,它将会在最后一帧停下来,如果设置
为false这个动画将循环播放 -->
参考资料:
Android 动画效果二 Frame Animation 动画专题研究 二
分享到:
相关推荐
我的博客《Android动画之二:View Animation》附带源码
比较全的android动画例子,包括基础的tween animation,这个就不必说了,还有比较新的属性动画:property animation
我的博客《Android动画之一:Drawable Animation》的附带源码
上次提供了Animation动画其一Tween补间动画的源代码,这次提供其二Frame动画的源代码(以直接编码的形式实现Frame动画的Demo,笔者资源中另有以xml形式实现的Demo)。
Android frame by frame animation动画显示源代码
基本动画案例:Tween Animation, Frame Animation,介绍参考:http://blog.csdn.net/forwardyzk/article/details/42739281
我的博客《Android动画之一:Drawable Animation》附带源码
包含AlphaAnimation渐变效果 TranslateAnimation:位移渐变 ScaleAnimation:缩放渐变 RotateAnimation:旋转渐变 AnimationSet:组合渐变... frameAnimation:帧动画的xml和代码实现方式,非常适合新手朋友参考。
在代码中定义逐帧动画。WIFI信号显示。适用于初学者。
Android 动画 Animation Demo
上次提供了Animation动画其一Tween补间动画的源代码,这次提供其二Frame动画的源代码(以xml形式实现的Demo。笔者资源中另有以直接编码的形式实现Frame动画的Demo)。
Android Animation Detailed Tutorial / Android 动画详尽教程 中文讲解(README)请直接点击对应标题 English explanation(or readme), Do not click on the title, please click on the tip ...
Animation 动画详解 Animation 动画详解 Animation 动画详解Animation 动画详解Animation 动画详解Animation 动画详解
Android动画之Animation的使用。包括变换动画Tween Animation,帧动画Frame Animation,布局动画Layout Animation,属性动画Property Animation
Android常见的补间Animation、Android动画特效,及Activity切换动画。
android Animation动画实现loading效果
Android Animation动画资源文件
FrameAnimation 用TextureView或SurfaceView 高性能播放帧动画,避免在很多帧的情况下使用AnimationDrawable带来的OOM和卡顿问题。 华为 mate 20X 1920×1080 24bit color JPG 201frames 24fps 测试效果 download ...
微信小程序实现animation动画,具体内容如下 1. 创建动画实例 wx.createAnimation(OBJECT) 创建一个动画实例animation。调用实例的方法来描述动画。最后通过动画实例的export方法导出动画数据传递给组件的...
博客讲解地址http://blog.csdn.net/wingichoy/article/details/47122619