Android getDrawingCache() 不包含来自 TextView/TextClock 的实际值
Android the getDrawingCache() not contain actual values from TextView/TextClock
我想得到两个屏幕位图。问题是,TextView
或 TextClock
包含第一次调用 getDrawingCache()
的值。第二次调用 getDrawingCache()
.
后,TextView
或 TextClock
的值不会在新的 Bitmap
中刷新
在 Nexus 5 上工作(第二个位图包含来自 TextClock
的正确时间)。
在许多其他设备上不起作用。例:华为荣耀4C.
minSdkVersion 21
targetSdkVersion 25
compileSdkVersion 25
TextClock
自动更改值。即使我在 TextView
.
上调用 setText()
方法也无法正常工作
问题出在哪里?
测试:
创建简单布局:TextClock
和 运行 此代码在 UI 线程:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("TAG", Thread.currentThread().getName());
try {
Bitmap bitmapScreen = getBitmapOfScreen();
//File f = new File("/storage/sdcard1", "tmp.jpg"); //Huawei
File f = new File("/storage/emulated/0", "tmp.jpg"); //Nexus 5
FileOutputStream fis = new FileOutputStream(f);
bitmapScreen.compress(Bitmap.CompressFormat.JPEG, 100, fis);
fis.flush();
fis.close();
bitmapScreen.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 5000);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("TAG", Thread.currentThread().getName());
try {
Bitmap bitmapScreen = getBitmapOfScreen();
//File f = new File("/storage/sdcard1", "tmp2.jpg"); //Huawei
File f = new File("/storage/emulated/0", "tmp2.jpg"); //Nexus 5
FileOutputStream fis = new FileOutputStream(f);
bitmapScreen.compress(Bitmap.CompressFormat.JPEG, 100, fis);
fis.flush();
fis.close();
bitmapScreen.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 10000);
/** Must be called in UI thread.*/
public Bitmap getBitmapOfScreen() throws Exception {
final View v1 = getWindow().getDecorView().getRootView();
v1.destroyDrawingCache();
v1.setDrawingCacheEnabled(true);
v1.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
bitmap.setHasAlpha(true); //important for PNG
v1.setDrawingCacheEnabled(false);
return bitmap;
}
已解决。
未显示呼叫 invalidate()
。现在,当我在 getBitmapOfScreen()
调用之前在 TextClock
上调用 invalidate()
时,位图中的值是正确的。
我想得到两个屏幕位图。问题是,TextView
或 TextClock
包含第一次调用 getDrawingCache()
的值。第二次调用 getDrawingCache()
.
TextView
或 TextClock
的值不会在新的 Bitmap
中刷新
在 Nexus 5 上工作(第二个位图包含来自 TextClock
的正确时间)。
在许多其他设备上不起作用。例:华为荣耀4C.
minSdkVersion 21
targetSdkVersion 25
compileSdkVersion 25
TextClock
自动更改值。即使我在 TextView
.
上调用 setText()
方法也无法正常工作
问题出在哪里?
测试:
创建简单布局:TextClock
和 运行 此代码在 UI 线程:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("TAG", Thread.currentThread().getName());
try {
Bitmap bitmapScreen = getBitmapOfScreen();
//File f = new File("/storage/sdcard1", "tmp.jpg"); //Huawei
File f = new File("/storage/emulated/0", "tmp.jpg"); //Nexus 5
FileOutputStream fis = new FileOutputStream(f);
bitmapScreen.compress(Bitmap.CompressFormat.JPEG, 100, fis);
fis.flush();
fis.close();
bitmapScreen.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 5000);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.i("TAG", Thread.currentThread().getName());
try {
Bitmap bitmapScreen = getBitmapOfScreen();
//File f = new File("/storage/sdcard1", "tmp2.jpg"); //Huawei
File f = new File("/storage/emulated/0", "tmp2.jpg"); //Nexus 5
FileOutputStream fis = new FileOutputStream(f);
bitmapScreen.compress(Bitmap.CompressFormat.JPEG, 100, fis);
fis.flush();
fis.close();
bitmapScreen.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 10000);
/** Must be called in UI thread.*/
public Bitmap getBitmapOfScreen() throws Exception {
final View v1 = getWindow().getDecorView().getRootView();
v1.destroyDrawingCache();
v1.setDrawingCacheEnabled(true);
v1.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
bitmap.setHasAlpha(true); //important for PNG
v1.setDrawingCacheEnabled(false);
return bitmap;
}
已解决。
未显示呼叫 invalidate()
。现在,当我在 getBitmapOfScreen()
调用之前在 TextClock
上调用 invalidate()
时,位图中的值是正确的。