小编Eva*_*ira的帖子

在 Android 中在屏幕外将视图渲染为位图

我想将视图渲染到位图并保存位图。但我需要在屏幕外完成这一切。

我试过这个:

    LayoutInflater inflater = getLayoutInflater();
    View linearview = (View) findViewById(R.id.linearview);
    linearview = inflater.inflate(R.layout.intro, null);


Bitmap bm = Bitmap.createBitmap( linearview.getMeasuredWidth(), linearview.getMeasuredHeight(), Bitmap.Config.ARGB_8888);                
Canvas c = new Canvas(bm);
linearview.layout(0, 0, linearview.getLayoutParams().width, linearview.getLayoutParams().height);
linearview.draw(c);

    String extStorageDirectory = Environment.getExternalStorageState().toString();
    extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;
    File file = new File(extStorageDirectory, "screen1.PNG");

    try {
        outStream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch …
Run Code Online (Sandbox Code Playgroud)

java android

5
推荐指数
1
解决办法
3893
查看次数

标签 统计

android ×1

java ×1