初学Android,图形图像之混合使用逐帧动画和补间动画(三十七)
2012-08-31 15:25
573 查看
下面例子混合使用了逐帧动画和补间动画,还有一个缺点,就是画面闪烁,一直没有找到解决办法
上面点击ImageView,上面的人物就开始走路跟移动
定义动画文件
主界面代码
上面点击ImageView,上面的人物就开始走路跟移动
定义动画文件
<?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/one" android:duration="10" /> <item android:drawable="@drawable/two" android:duration="10" /> <item android:drawable="@drawable/three" android:duration="10" /> <item android:drawable="@drawable/four" android:duration="10" /> <item android:drawable="@drawable/five" android:duration="10" /> <item android:drawable="@drawable/six" android:duration="10" /> <item android:drawable="@drawable/seven" android:duration="10" /> <item android:drawable="@drawable/eight" android:duration="10" /> <item android:drawable="@drawable/nine" android:duration="10" /> <item android:drawable="@drawable/ten" android:duration="10" /> <item android:drawable="@drawable/eleven" android:duration="10" /> <item android:drawable="@drawable/twelve" android:duration="10" /> <item android:drawable="@drawable/thirteen" android:duration="10" /> <item android:drawable="@drawable/fourteen" android:duration="10" /> <item android:drawable="@drawable/fifteen" android:duration="10" /> <item android:drawable="@drawable/sixteen" android:duration="10" /> <item android:drawable="@drawable/seventeen" android:duration="10" /> <item android:drawable="@drawable/eighteen" android:duration="10" /> <item android:drawable="@drawable/nineteen" android:duration="10" /> <item android:drawable="@drawable/twenty" android:duration="10" /> <item android:drawable="@drawable/twentyone" android:duration="10" /> <item android:drawable="@drawable/twentytwo" android:duration="10" /> <item android:drawable="@drawable/twentythree" android:duration="10" /> <item android:drawable="@drawable/twentyfour" android:duration="10" /> <item android:drawable="@drawable/twentyfive" android:duration="10" /> <item android:drawable="@drawable/twentysix" android:duration="10" /> <item android:drawable="@drawable/twentyseven" android:duration="10" /> <item android:drawable="@drawable/twentyeight" android:duration="10" /> <item android:drawable="@drawable/twentynine" android:duration="10" /> <item android:drawable="@drawable/thirty" android:duration="10" /> <item android:drawable="@drawable/thirtyone" android:duration="10" /> <item android:drawable="@drawable/thirtytwo" android:duration="10" /> <item android:drawable="@drawable/thirtythree" android:duration="10" /> <item android:drawable="@drawable/thirtyfour" android:duration="10" /> <item android:drawable="@drawable/thirtyfive" android:duration="10" /> <item android:drawable="@drawable/thirtysix" android:duration="10" /> </animation-list>主界面引用动画
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#fff"> <ImageView android:id="@+id/walk" android:layout_width="150dp" android:layout_height="150dp" android:background ="@anim/walk" /> </LinearLayout>
主界面代码
package WangLi.Graphics.Walk; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; public class Walk extends Activity { //记录行走人ImageView当前的位置 private float curX = 0; private float curY = 30; //记录行走人ImageView下一个位置的坐标 float nextX = 0; float nextY = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取显示行走人的ImageView组件 final ImageView imageView = (ImageView)findViewById(R.id.walk); final Handler handler = new Handler() { public void handleMessage(Message msg) { if(msg.what == 0x123) { //横向上一直向右走 if(nextX > 320) { curX = nextX = 0; } else { nextX += 8; } //纵向上随机上下走 nextY = curY + (float)(Math.random() * 10 - 5); //设置显示行走人的ImageView位置发生位移改变 TranslateAnimation anim = new TranslateAnimation(curX, nextX, curY, nextY); curX = nextX; curY = nextY; anim.setDuration(200); anim.setFillEnabled(true); //开始位移动画 imageView.startAnimation(anim); } } }; final AnimationDrawable walk = (AnimationDrawable)imageView.getBackground(); imageView.setOnClickListener(new OnClickListener() { public void onClick(View v) { //开始播放人行走的逐帧动画 walk.start(); //通过定制器控制每0.2秒运行一次TanslateAnimation动画 new Timer().schedule(new TimerTask() { public void run() { handler.sendEmptyMessage(0x123); } }, 0, 200); } }); } }
相关文章推荐
- 初学Android,图形图像之自定义补间动画(三十八)
- 初学Android,图形图像之补间动画(三十六)
- 初学Android,图形图像之使用逐帧动画(三十四)
- 初学Android,图形图像之使用双缓冲画图(二十七)
- Android图形与图像处理-补间动画
- 初学Android,图形图像之使用SurfaceView(三十九)
- 初学Android,图形图像之使用drawBitmapMesh扭曲图像(三十二)
- 初学Android,图形图像之在指定点(坐标)播放动画(三十五)
- Android图形与图像处理-补间动画
- 初学Android,图形图像之使用Path类
- 初学Android,图形图像之使用SurfaceView(四十)
- 初学Android,图形图像之使用Bitmap和BitmapFactory(二十四)
- (转)初学Android,图形图像之使用Bitmap和BitmapFactory(二十四)
- 初学Android,图形图像之使用Shader填充图像(三十三)
- 初学Android,图形图像之使用Path类(二十六)
- 初学Android,图形图像之使用Path类(二十六)
- 初学Android,图形图像之使用Bitmap和BitmapFactory(二十四)
- 初学Android,图形图像之使用Canvas,Paint绘图(二十五)
- 初学Android,图形图像之使用双缓冲画图(二十七)
- Android动画的使用(一):补间动画与逐帧动画