android xml布局模板,xml布局自定义SurfaceView模板

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;//停止线程

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值