每当我点击“截屏”按钮时,它就会卡住并继续拍摄截图。
停止按钮
此按钮的功能是停止线程并停止代码截取屏幕截图。
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();
选择ScreenShot按钮
此按钮的功能是启动线程并开始截屏。
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 :(得分:0)
无需启动线程或类似按钮单击即可轻松调用方法。而不是设置比imageview内的位图或任何你想要使用的位图。
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;
}