package com.app;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ViewMain extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder surfaceHolder;
private ThreadInitApp threadInitApp;
private boolean isInitOK = false;
private ThreadDrawUI threadDrawUI;
private boolean isDrawUI = true;
private ThreadLogic threadLogic;
private boolean isLogic = true;
private Bitmap bmIcon;
private int y;
public ViewMain(Context context, AttributeSet attrs) {
super(context, attrs);
surfaceHolder = this.getHolder();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
threadInitApp = new ThreadInitApp();
threadDrawUI = new ThreadDrawUI();
threadLogic = new ThreadLogic();
threadInitApp.start();
threadDrawUI.start();
threadLogic.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
this.stop();
}
/**
* 显示
*/
public void display() {
surfaceHolder.addCallback(this);
isInitOK = false;
isDrawUI = true;
isLogic = true;
}
/**
* 停止
*/
public void stop() {
isDrawUI = false;
isLogic = false;
threadLogic.stop();
threadDrawUI.stop();
threadLogic = null;
threadDrawUI = null;
}
/**加载资源线程*/
class ThreadInitApp extends Thread {
@Override
public void run() {
while(isInitOK==false) {
bmIcon = BitmapFactory.decodeResource(getResources(), R.drawable.test);
isInitOK = true;
this.stop();
threadInitApp = null;
}
}
}
/**绘图线程*/
class ThreadDrawUI extends Thread {
@Override
public void run() {
Canvas canvas;
while(isDrawUI) {
canvas = null;
try {
// 锁定整个画布,在内存要求比较高的情况下,建议参数不要为null
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {//同步
canvas.drawColor(Color.WHITE);
if(isInitOK) {
canvas.drawBitmap(bmIcon, 200, y, null);
}
}
} finally {//使用finally语句保证下面的代码一定会被执行
if(canvas != null) {
//更新屏幕显示内容
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
/**逻辑线程*/
class ThreadLogic extends Thread {
@Override
public void run() {
while(isLogic) {
y += 1;
if(y>800) {
y=0;
}
System.out.println("y-->" + y);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SurfaceView屏幕抖动问题原文件
共28个文件
class:11个
png:4个
xml:3个
4星 · 超过85%的资源 需积分: 43 114 下载量 18 浏览量
2011-05-24
17:27:19
上传
评论
收藏 56KB RAR 举报
温馨提示
发帖提问题使用,android原项目文件,SurfaceView屏幕抖动问题
资源推荐
资源详情
资源评论
收起资源包目录
Test.rar (28个子文件)
Test
.project 840B
default.properties 362B
bin
Test.apk 18KB
com
app
R$string.class 394B
R$layout.class 361B
ViewMain.class 3KB
R$drawable.class 395B
R.class 454B
ViewMain$ThreadDrawUI.class 1KB
ActivityMain.class 847B
ViewMain$ThreadInitApp.class 1KB
R$attr.class 307B
ViewMain$ThreadLogic.class 1KB
R$id.class 353B
resources.ap_ 13KB
classes.dex 7KB
proguard.cfg 1KB
AndroidManifest.xml 674B
src
com
app
ViewMain.java 3KB
ActivityMain.java 809B
res
values
strings.xml 175B
layout
main.xml 384B
drawable-ldpi
icon.png 2KB
drawable-hdpi
test.png 6KB
icon.png 4KB
drawable-mdpi
icon.png 3KB
.classpath 280B
gen
com
app
R.java 776B
assets
共 28 条
- 1
麦田捕手
- 粉丝: 134
- 资源: 42
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页