我的应用程序具有导出功能,可导出ScrollView的副本。 Scrollview(包括背景图像)是通过编程设置的,但是当我导出它的副本时,背景显示为黑色。
我调用了两个函数:takeScreenshot()和saveBitmap()
如何添加背景图片?
private android.graphics.Bitmap takeScreenshot(View scroll) {
ScrollView iv = findViewById(R.id.scroll);
Bitmap bitmap = Bitmap.createBitmap(
iv.getChildAt(0).getWidth(),
iv.getChildAt(0).getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
iv.getChildAt(0).draw(c);
return bitmap;
}
public void saveBitmap(Bitmap bitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root+"/NPCdata");
boolean test = newDir.mkdirs();
if (test){
String photoname = myNPC.getNpcMap().get("Name");
assert photoname != null;
photoname.replaceAll("\\s+", "");
String fotoname = photoname+".jpg";
File file = new File(newDir, fotoname);
while (file.exists()){
fotoname = photoname+"a"+".jpg";
file = new File(newDir, fotoname);
}
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(),
"Saved in folder: 'NPCdata'", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
答案 0 :(得分:0)
我通过在ScrollView内的LinearLayout中添加背景来解决了这个问题
scroll = new ScrollView(this);
scroll.setBackgroundResource(R.drawable.back);
scroll.setFillViewport(true);
contentView = new LinearLayout(this);
contentView.setBackgroundResource(R.drawable.back);// <-- this makes it work