画布上显示的控件

时间:2015-03-15 03:15:07
【文件属性】:

文件名称:画布上显示的控件

文件大小:51KB

文件格式:RAR

更新时间:2015-03-15 03:15:07

Canvas,widget

package com.vai; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; 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 { public MySurfaceView(Context context, AttributeSet attrs) { super(context, attrs); sfh = this.getHolder(); sfh.addCallback(this); paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); setFocusable(true); } private SurfaceHolder sfh; private Paint paint; private Thread th; private boolean flag; private Canvas canvas; private int screenW, screenH; // /** * SurfaceView初始化函数 */ /** * SurfaceView视图创建,响应此函数 */ @Override public 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.BLACK); paint.setTextSize(15); canvas.drawText("这里是游戏视图-SurfaceView", 40, 40, paint); } } catch (Exception e) { // TODO: handle exception } finally { if (canvas != null) sfh.unlockCanvasAndPost(canvas); } } /** * 触屏事件监听 */ @Override public boolean onTouchEvent(MotionEvent event) { return true; } /** * 按键事件监听 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } /** * 游戏逻辑 */ private void logic() { } @Override public 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视图状态发生改变,响应此函数 */ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } /** * SurfaceView视图消亡时,响应此函数 */ @Override public void surfaceDestroyed(SurfaceHolder holder) { flag = false; } }


【文件预览】:
ViewAndItem
----.project(847B)
----project.properties(360B)
----proguard.cfg(1KB)
----src()
--------com()
----AndroidManifest.xml(631B)
----res()
--------drawable-ldpi()
--------drawable-hdpi()
--------drawable-mdpi()
--------layout()
--------values()
----assets()
----gen()
--------com()
----.classpath(288B)
----bin()
--------resources.ap_(11KB)
--------classes()
--------res()
--------classes.dex(5KB)
--------ViewAndItem.apk(15KB)

网友评论

  • 很好的资源,长知识了,谢谢提供!
  • 有问题,还需进一步调试
  • 调试可用。。。还不错