package com.dream.apm;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by HuangZhiLong on 2015/1/22.
*/
public class MySurfaceViewone extends SurfaceView implements SurfaceHolder.Callback,Runnable {
private Thread th;
private SurfaceHolder sfh;
private Canvas canvas;
private Paint paint;
private boolean flag;
public int screenW=0,screenH=0;
/**
* SurfaceView初始化函数
*/
public MySurfaceViewone(Context context,AttributeSet attrs) {
super(context,attrs);
this.setKeepScreenOn(true);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setAntiAlias(true);//消除锯齿
//paint.setTypeface(Typeface.DEFAULT_BOLD);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
}
/**
* SurfaceView视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
screenW = this.getWidth();
screenH = this.getHeight();
myDraw();
//tt=new Rect(0,this.getWidth(),this.getHeight());
flag = true;
th = new Thread(this);
th.start();
}
/**
* 游戏绘图
*/
public void myDraw() {
try {
canvas = sfh.lockCanvas();
canvas.drawColor(Color.rgb(90,151,161));//背景
paint.setStrokeWidth(0);
paint.setColor(Color.BLACK);
//paint.setTextSize(size);
//绘制表格
} catch (Exception e) {
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
}
/**
* 图片的线程运行
*/
public void run() {
while (flag) {
myDraw();
try {
Thread.sleep(40);
} catch (Exception ex) {
Log.e("ERROR","Thread is Error!");
}
}
}
/**
* 触屏事件监听
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction())
{
//移动
case MotionEvent.ACTION_MOVE:
break;
//按下
case MotionEvent.ACTION_DOWN:
break;
//放开
case MotionEvent.ACTION_UP:
break;
}
return true;
}
/**
* SurfaceView视图状态发生改变,响应此函数
*/
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {
System.out.println("ImageSurfaceView is surfaceChanged");
}
/**
* SurfaceView视图消亡时,响应此函数
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceDestroyed");
flag = false;//停止线程
}
}
复制代码
原文链接:https://www.f2er.com/xml/296725.html