本文主要是介绍帧图画图动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
帧图画图动画
package com.example.test;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;/*** * @author Himi**/
public class MySurfaceView extends SurfaceView implements Callback, Runnable {private SurfaceHolder sfh;private Paint paint;private Thread th;private boolean flag;private Canvas canvas;private int screenW, screenH;//首先声明十个容量的位图数组private Bitmap fishBmp[] = new Bitmap[10];//记录当前播放帧private int currentFrame;///*** SurfaceView初始化函数*/public MySurfaceView(Context context) {super(context);sfh = this.getHolder();setBackgroundResource(R.drawable.water);setZOrderOnTop(true);getHolder().setFormat(PixelFormat.TRANSLUCENT);sfh.addCallback(this);paint = new Paint();paint.setColor(Color.WHITE);paint.setAntiAlias(true);setFocusable(true);//将每张小鱼帧图生成位图存入小鱼帧数组中for (int i = 0; i < fishBmp.length; i++) {fishBmp[i] = BitmapFactory.decodeResource(this.getResources(), R.drawable.fish0 + i);}}/*** SurfaceView视图创建,响应此函数*/@Overridepublic void surfaceCreated(SurfaceHolder holder) {screenW = this.getWidth();screenH = this.getHeight();flag = true;//实例线程th = new Thread(this);//启动线程th.start();}/*** 游戏绘图*/public void myDraw() {try {canvas = sfh.lockCanvas();if (canvas != null) {canvas.drawColor(Color.TRANSPARENT);canvas.drawBitmap(fishBmp[currentFrame], this.getWidth()/2, this.getHeight()/2, paint);}} catch (Exception e) {// TODO: handle exception} finally {if (canvas != null)sfh.unlockCanvasAndPost(canvas);}}/*** 触屏事件监听*/@Overridepublic boolean onTouchEvent(MotionEvent event) {return true;}/*** 按键事件监听*/@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {return super.onKeyDown(keyCode, event);}/*** 游戏逻辑*/private void logic() {currentFrame++;if (currentFrame >= fishBmp.length) {currentFrame = 0;}}@Overridepublic void run() {while (flag) {long start = System.currentTimeMillis();myDraw();logic();long end = System.currentTimeMillis();try {if (end - start < 50) {Thread.sleep(50 - (end - start));}} catch (InterruptedException e) {e.printStackTrace();}}}/*** SurfaceView视图状态发生改变,响应此函数*/@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}/*** SurfaceView视图消亡时,响应此函数*/@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {flag = false;}
}
package com.example.test;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(new MySurfaceView(this));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
这篇关于帧图画图动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!