anim_xph_animationlist.xml文件中设置animation-list <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/anim_xph_1" android:duration="300"> <item android:drawable="@drawable/anim_xph_2" android:duration="300"> <item android:drawable="@drawable/anim_xph_3" android:duration="300"> </animation-list> —控制帧动画显示与停止的代码 LinearLayout linearLY = new LinearLayout(context);//创建一个LinearLayout布局对象 linearLY.setOrientation(LinearLayout.VERTICAL);//设置LinearLayout为垂直布局 ImageView moveIV = new ImageView(context);//创建ImageView //用XML文件中的animation-list给帧动画设置图片资源 moveIV.setBackgroundResource(R.drawable.anim_xph_animationlist); //获取帧动画AnimationDrawable的对象 moveIVCartoonAnim = (AnimationDrawable)moveIV.getBackground(); linearLY.addView(moveIV);//把ImageView添加到LinearLayout布局对象中 Button cartoonBtn = new Button(context); cartoonBtn.setText("cartoonBtn走着"); cartoonBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (moveIVCartoonAnim != null) { if (moveIVCartoonAnim.isRunning()) { moveIVCartoonAnim.stop();//停止帧动画 } else { moveIVCartoonAnim.start();//开始帧动画 } } } }); linearLY.addView(cartoonBtn);//把Button添加到LinearLayout布局对象中 this.addView(linearLY);//把LinearLayout布局对象添加到主视图中 —从资源文件中取出一张具有四个动作小图片的大图片,并显示其动态效果[工作小结] Bitmap roleBitmap;//roleBitmap是一张具有四个动作小图片的大图片--此变量应该是已知的 int roleImgW;//动作小图片的宽--此变量应该是已知的 int roleImgH;//动作小图片的高--此变量应该是已知的 moveIVCartoonAnim = new AnimationDrawable(); for(int i=0; i<4; i++){ Bitmap newbitmap= Bitmap.createBitmap(roleBitmap, roleImgW*i, 0, roleImgW, roleImgH); BitmapDrawable bmppd=new BitmapDrawable(newbitmap); moveIVCartoonAnim.addFrame(bmppd, 300); } moveIV.setBackgroundDrawable(moveIVCartoonAnim);//将动画设置为ImageView背景 moveIVCartoonAnim.start();