新建工程 myFrameAnimation
修改mainActivity.java的代码
参考资料:
在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这个动画将循环播放 -->
参考资料:
289

被折叠的 条评论
为什么被折叠?



