私は2つのビットマップを取得したいと思います。問題は、TextView
またはTextClock
には、最初の呼び出しの値がgetDrawingCache()
から含まれています。 TextView
またはTextClock
の値は、getDrawingCache()
の2番目のコールの後に新しいBitmap
にリフレッシュされません。AndroidのgetDrawingCache()にTextView/TextClockの実際の値が含まれていません
Nexus 5で作業します(2番目のビットマップには、正確な時刻がTextClock
から含まれています)。
他の多くのデバイスでは動作しません。例:Huawei Honor 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;
}
投稿getBitmapOfScreen()メソッド –
あります。下へスクロール。 – t0m
getdrawingcacheからビットマップを作成する前にv1.buildDrawingCache()を追加しようとします。 –