在Android中更新特定布局的DrawingCache
问题描述:
我在android中获取布局的正确“ScreenShot”时遇到了问题。该布局包含EditTexts和TextViews。在Android中更新特定布局的DrawingCache
下面的代码只给出了布局的“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;
}
}
答
解决了,我不应该使用
LayoutInflater
LayoutInflater只是实例化一个布局XML文件到其相应的机器人。 view.View对象,但不是当前图形 see here
只是用findViewbyId得到视图对象 然后
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap drawingCache = view.getDrawingCache();
的作品!
尝试添加ll.destroyDrawingCache();之前ll.setDrawingCacheEnabled(true); –
我试过 ll.destroyDrawingCache(); ll.invalidate(); 它不为我工作 @DivyeshPatel – Lee
尝试为无效和的EditText TextView的 –