2016-02-27 231 views
0

每当我点击一次takescreenshot按钮它卡住,并不断采取截图。屏幕截图按住屏幕截图时卡住

停止按钮

这个按钮的功能是停止线程,并从截屏停止代码。

Thread th = new Thread(new Runnable() { 
    @Override 
    public void run() { 
     StopThread.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       thread.interrupt(); 
       check=false; 
       //textView.setText(TotalShots); 
      } 
     }); 
    } 
}); 
th.start(); 

抓图按钮

这个按钮的功能是启动线程,并开始采取截图。

public void screenShot(View view) throws IOException { 
     thread = new Thread(new Runnable() { 
      @Override 
      public void run() { 
        runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
          if (!Thread.interrupted()){ 

           while (check) 
           { 
            captureScreenShot = (Button) findViewById(R.id.capture_screen_shot); 
            c = Calendar.getInstance(); 
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
            name = simpleDateFormat.format(c.getTime()); 
            mbitmap = getBitmapOFRootView(captureScreenShot); 
            imageView.setImageBitmap(mbitmap); 
            try { 
             createImage(mbitmap); 
            } catch (IOException e) { 
             e.printStackTrace(); 
            } 


           } 
          } 

         } 
        }); 
      } 
     }); 
     thread.start(); 
    } 

点击查看UI of App

回答

0

而无需启动线程或喜欢,你可以按一下按钮简单的调用方法。而不是设置比位图内的位图或任何你想要使用该位图。

volatile boolean flag = true;

public void run() 
{ 
while(flag) 
    {  
    // Do your task of calling method of screenshot 
    try{ 
     Thread.Sleep(interval); 
    } catch(Exception e){ 

     } 

    } 
} 

当您单击停止按钮时设置标志为false。

public Bitmap takeScreenShot(View view) { 

// configuramos para que la view almacene la cache en una imagen 
view.setDrawingCacheEnabled(true); 
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW); 
view.buildDrawingCache(); 

if(view.getDrawingCache() == null) return null; // Verificamos antes de que no sea null 

// utilizamos esa cache, para crear el bitmap que tendra la imagen de la view actual 
Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache()); 
view.setDrawingCacheEnabled(false); 
view.destroyDrawingCache(); 

return snapshot; 
} 
+0

我想要代码继续截图,直到按下停止按钮。 –

+0

仍然存在问题。它不允许我按停止按钮。 –