-
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;//停止線程
-
}
-
-
- }
xml佈局自定義SurfaceView模板
android中使用xml佈局自定義SurfaceView模板
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
Qt/C++音視頻開發73-高效濾鏡/文字水印/圖形水印/圖片水印/yolo運算後的結果顯示到畫面中
飛揚青雲
2024-05-10 14:33:24
藍橋15屆stema編程題密碼鎖-動態規劃 C++和Python最後一道題
Danlis
2024-05-10 14:32:23
Pywinauto的幾個小坑
Believer007
2024-05-10 14:25:53
gin-admin-cli配置記錄
水車
2024-05-10 14:24:02
C#/.NET/.NET Core優秀項目和框架2024年4月簡報
追逐時光
2024-05-10 14:23:32
LLM生態下爬蟲程序的現狀與未來
JadePeng
2024-05-10 14:20:52
Java線程池核心知識詳解
uper超人
2024-05-10 14:17:01
一個簡單的MD5加鹽
uper超人
2024-05-10 14:17:01
《最新出爐》系列入門篇-Python+Playwright自動化測試-45-鼠標操作-下篇
北京-宏哥
2024-05-10 14:16:11
前端技術選型時有用的網站
dy-junior
2024-05-10 14:16:11
apisix~jwt-auth插件
張佔嶺
2024-05-10 14:14:11
apisix~helm方式的部署到k8s
張佔嶺
2024-05-10 14:14:11
2021看雪SDC議題回顧 | SaTC:一種全新的物聯網設備漏洞自動化挖掘方法
朝暉
2024-05-10 14:07:40
firmeye - IoT固件漏洞挖掘工具
朝暉
2024-05-10 14:07:40