首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SurfaceHolder导致闪烁

SurfaceHolder导致闪烁
EN

Stack Overflow用户
提问于 2015-09-18 05:13:29
回答 1查看 121关注 0票数 1

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

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

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

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

代码语言:javascript
复制
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();
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-09-18 17:20:32

好了,我找到了一个解决方案,我会把它贴在这里,以帮助其他人。This is the link帮助了我,这就是我需要的代码样本。(很难找到)。

代码语言:javascript
复制
  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);
    }
  }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32640071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档