java - SurfaceHolder导致闪烁

标签 java android bitmap surfaceview surfaceholder

我正在尝试使用透明的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/

相关文章:

java - 如何仅使用 apache camel 从 http 读取一次 csv 文件?

c++ - IN_ADDR 结构在 android 中奇怪地定义

android - 加载位图时出现 "BitmapFactory.decodeResource"应用程序崩溃

仅在 X 上的 Android 位图平铺

haskell - 我将如何想象 Haskell 中基于像素的渲染?

java - 无法序列化

java - 在 Google 应用引擎上部署失败

java - 尝试添加过滤器以 hibernate 自引用OneToMany连接

android - 抽屉导航滞后于 ImageView

java - eclipse是否每个项目都使用proguard?