获得从布局位图 [英] Get bitmap from layout

查看:204
本文介绍了获得从布局位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图夸大的布局,并用它来对图像视图设置一个位图。然后,我添加了ImageView的线性布局和显示的线性布局。下面是我尝试过:

I'm trying to inflate a layout and use that to set a bitmap on an image view. Then, I'm adding that imageview to a linear layout and displaying the linear layout. Here's what I've tried:

public class TestActivity extends Activity {
    private static Bitmap bitMap;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout l = new LinearLayout(this);

        bitMap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(), 
                getWindowManager().getDefaultDisplay().getHeight(), 
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitMap);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v1 = inflater.inflate(R.layout.main, null);
        v1.layout(0, 0, getWindowManager().getDefaultDisplay().getWidth(), 
                getWindowManager().getDefaultDisplay().getHeight());
        v1.draw(canvas);

        ImageView i1 = new ImageView(this);
        i1.setImageBitmap(bitMap);
        i1.setAdjustViewBounds(true);
        i1.setLayoutParams(new FrameLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth(), 
                getWindowManager().getDefaultDisplay().getHeight()));

        l.addView(i1);
        setContentView(l);
    }
}

不幸的是,位图没有被正确创建。难道有什么我做错了什么?

Unfortunately, the bitMap is not being created properly. Is there anything I'm doing wrong?

推荐答案

转换布局为位图。

FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();

这篇关于获得从布局位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆