我正在尝试将ScrollView
转换为Bitmap
,但位图返回null。我不知道如何管理,请给我一个解决方案来实现这个目标
我想将整个ScrollView内容作为位图,以便之后创建PDF
遵循我的准则:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_first__resume__screen);
Rlv = (RelativeLayout) findViewById(R.id.Rltv_Layout);
Rlt_layoutWhole = (RelativeLayout) findViewById(R.id.Rlv_whole);
Srvw = (ScrollView) findViewById(R.id.scrollView1);
// View u = ((Activity) mContext).findViewById(R.id.scrollView1);
Random rnd = new Random();
int prevTextViewId = 0;
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 15; i++) {
final TextView textView = new TextView(this);
textView.setText("Text " + i);
textView.setTextColor(rnd.nextInt() | 0xff000000);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
params.setMargins(10, 0, 0, 0);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
Rlv.addView(textView, params);
}
// Rlt_layoutWhole.setDrawingCacheEnabled(true);
// Rlt_layoutWhole.buildDrawingCache();
// Bitmap bm = Rlt_layoutWhole.getDrawingCache();
Rlt_layoutWhole.setDrawingCacheEnabled(true);
Rlt_layoutWhole.refreshDrawableState();
Bitmap bitmap = Rlt_layoutWhole.getDrawingCache();
Rlt_layoutWhole.setDrawingCacheEnabled(false);
// Bitmap bitmap = Bitmap.createBitmap(Rlt_layoutWhole.getWidth(),
// Rlt_layoutWhole.getHeight(), Bitmap.Config.ARGB_8888);
// Canvas canvas = new Canvas(bitmap);
// Rlt_layoutWhole.draw(canvas);
// createPDF(bitmap);
//
//****************************************************************88
// int totalHeight = Srvw.getChildAt(0).getHeight();
// int totalWidth = Srvw.getChildAt(0).getWidth();
//
// Bitmap b = getBitmapFromView(Srvw,1200,300);
//
// //Save bitmap
// String extr = Environment.getExternalStorageDirectory()+"/Folder/";
// String fileName = "report.jpg";
// File myPath = new File(extr, fileName);
// FileOutputStream fos = null;
// try {
// fos = new FileOutputStream(myPath);
// b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// fos.flush();
// fos.close();
// MediaStore.Images.Media.insertImage(mContext.getContentResolver(), b, "Screen", "screen");
// }catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//********************************************************************************************8
int height = Math.min(500, 500);
// float percent = height / (float)totalHeight;
Bitmap canvasBitmap = Bitmap.createBitmap((int)(1500),(int)(1500), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBitmap);
Drawable bgDrawable = Srvw.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.BLACK);
canvas.save();
//canvas.scale(100, percent);
Srvw.draw(canvas);
canvas.restore();
createPDF(canvasBitmap);
}
public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {
Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight , Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
public Bitmap getBitmapFromView(View view) {
view.setDrawingCacheEnabled(true);
view.refreshDrawableState();
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
return bitmap;
}