gpt4 book ai didi

java - SurfaceHolder导致闪烁

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:07 26 4
gpt4 key购买 nike

我正在尝试使用透明的surefaceView和由自定义位图制成的画笔创建特定的绘画程序。我在该网站上寻找答案,但找不到我需要的内容。

由于run方法的while循环,我在屏幕上绘制的位图不停地闪烁。看起来程序正在无限地绘制相同的位图,这导致了这种闪烁。

我正在考虑将 event.getAction()==MotionEvent.ACTION_UP 之前的每个笔触保存为单独的位图。但我在做这件事时遇到了麻烦。

这是我的代码,如果您能帮助解决此闪烁问题,我将不胜感激!

public class SurfaceMyPaint extends SurfaceView implements Runnable {

Thread t;
SurfaceHolder holder;
Bitmap brush;
boolean isItOk = false;

public SurfaceMyPaint(Context context)
{
super(context);
holder = getHolder();
initial();
}
public SurfaceMyPaint(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
holder = getHolder();
initial();
// TODO Auto-generated constructor stub
}

public SurfaceMyPaint(Context context, AttributeSet attrs)
{
super(context, attrs);
holder = getHolder();

initial();
// TODO Auto-generated constructor stub
}

public void initial()
{
brush= BitmapFactory.decodeResource(getResources(), R.drawable.brush2);
}

public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction()== MotionEvent.ACTION_DOWN)
{
x= event.getX();
y= event.getY();
}

if(event.getAction()== MotionEvent.ACTION_MOVE)
{
x = event.getX();
y= event.getY();
Canvas c= holder.lockCanvas();
c.drawBitmap(brush,x- (brush.getWidth() / 2),y- (brush.getWidth() / 2),null);
holder.unlockCanvasAndPost(c);
}
return true;
}


public void run()
{
while (isItOk == true)
{
if (!holder.getSurface().isValid())
continue;
}
}

public void pause(){
isItOk = false;
while (true){
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){
isItOk = true;
t = new Thread(this);
t.start();
}
}

最佳答案

好的,我找到了解决方案,我会将其发布在这里以帮助其他人。 This is the link这对我有帮助,这是我需要的代码示例。 (很难找到)。

public void run() {
Canvas canvas = null;
while (_run){
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);;
}
final Canvas c = new Canvas (mBitmap);
c.drawColor(0, PorterDuff.Mode.CLEAR);
commandManager.executeAll(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}

关于java - SurfaceHolder导致闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32640071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com