我在获取 android 布局的正确“ScreenShot”时遇到问题。布局包含 EditTexts 和 TextViews。
下面的代码只给出了布局的“ScreenShot”,但是当我在 EditText 或 TextView 中更改字符串时,“ScreenShot”没有更新!
如何确保“ScreenShot”可以更新并与屏幕上显示的相同?
private Bitmap getBitmap() {
Bitmap bitmap = null;
LayoutInflater factorys = LayoutInflater.from(this);
final View textEntryView = factorys.inflate(R.layout.activity_main, null);
View ll = textEntryView.findViewById(R.id.ll_layout);
ll.setDrawingCacheEnabled(true);
ll.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
ll.layout(0, 0, ll.getMeasuredWidth(), ll.getMeasuredHeight());
ll.buildDrawingCache();
bitmap = Bitmap.createBitmap(ll.getDrawingCache());
//bitmap=getCacheBitmapFromView(ll);
ll.setDrawingCacheEnabled(false);
return bitmap;
}
这是整个代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(mycl);
}
OnClickListener mycl = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bitmap mybpic = null;
mybpic = getBitmap();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(mybpic);
}
};
private Bitmap getBitmap() {
Bitmap bitmap = null;
LayoutInflater factorys = LayoutInflater.from(this);
final View textEntryView = factorys.inflate(R.layout.activity_main,
null);
View ll = textEntryView.findViewById(R.id.ll_layout);
ll.destroyDrawingCache();
ll.invalidate();
ll.setDrawingCacheEnabled(true);
ll.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
ll.layout(0, 0, ll.getMeasuredWidth(), ll.getMeasuredHeight());
ll.buildDrawingCache();
bitmap = Bitmap.createBitmap(ll.getDrawingCache());
// bitmap=getCacheBitmapFromView(ll);
ll.setDrawingCacheEnabled(false);
return bitmap;
}
}