我有代码以编程方式创建视图,
splash.post(new Runnable() {
@Override
public void run() {
processFromDatabase();
}
});
splash是我的父视图,这是processFromDatabase代码
private void processFromDatabase() {
createLayoutBook("food_name", "80000", "food_des","", 2);
Bitmap bmp = merge();
saveToLocal2(bmp);
}
这是我的createLayoutBook代码,
private void createLayoutBook(String food_name, String food_price,String food_des, String path, int tag) {
Log.e("create layout book", "Create layout book");
RelativeLayout root_2book = new RelativeLayout(c);
RelativeLayout.LayoutParams rlp_a = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, ((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 120, c.getResources()
.getDisplayMetrics())));
rlp_a.setMargins(0, 10, 0, 0);
root_2book.setLayoutParams(rlp_a);
root_2book.setBackgroundColor(c.getResources().getColor(R.color.krem));
RelativeLayout layout_detail_2book = new RelativeLayout(c);
RelativeLayout.LayoutParams rdet = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rdet.addRule(RelativeLayout.RIGHT_OF, layout_a_2book.getId());
rdet.setMargins(20, 3, 0, 0);
layout_detail_2book.setLayoutParams(rdet);
TextView tv_nama_2book = new TextView(c);// nama
tv_nama_2book.setId(id_book);
id_book = id_book + 1;
RelativeLayout.LayoutParams tv_nama = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv_nama_2book.setLayoutParams(tv_nama);
tv_nama_2book.setTextSize(20);
tv_nama_2book.setTypeface(Typeface.DEFAULT_BOLD);
tv_nama_2book.setText(food_name);
Log.e("food name", food_name);
getSharedPreferences("myprefs", 0).edit().putString("food_name", food_name).commit();
TextView tv_des_2book = new TextView(c); // des
RelativeLayout.LayoutParams tv_des = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv_des.addRule(RelativeLayout.BELOW, tv_nama_2book.getId());
tv_des.setMargins(0, 3, 5, 0);
tv_des_2book.setLayoutParams(tv_des);
tv_des_2book.setTextSize(12);
tv_des_2book.setTextColor(c.getResources().getColor(R.color.hitam));
tv_des_2book.setText(food_des);
Log.e("food des", food_des);
getSharedPreferences("myprefs", 0).edit().putString("food_des", food_des).commit();
layout_detail_2book.addView(tv_nama_2book);
layout_detail_2book.addView(tv_des_2book);
root_2book.addView(layout_detail_2book);
splash.addView(root_2book);
这是将此布局更改为位图
的代码public Bitmap merge() {
View v1 = splash;
v1.setDrawingCacheEnabled(true);
Bitmap merge = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
return merge;
}
但为什么,它会以黑屏返回,就像我之前创建的布局一样,不保存或丢失,任何解决方案?感谢
答案 0 :(得分:0)
我相信你想要的东西:
public Bitmap merge() {
Bitmap bitmap = Bitmap.createBitmap(...);
Canvas canvas = new Canvas(bitmap);
splash.draw(canvas);
return bitmap;
}