重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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,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;//停止线程
}
}
复制代码
android中使用xml布局自定义SurfaceView模板