Android不是从视图创建位图图像

时间:2015-12-19 20:32:22

标签: android view bitmap android-bitmap

我正在开发一个Android应用程序,我正在尝试从视图创建位图图像。我的视图创建代码是,

private View createInflatedViewFromLayout(Context context, String question, String answer, ViewGroup containerView){

        View view;

        //Creating view from layout inflater
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.image_export_view, (ViewGroup) containerView.getParent(), false);

        //Getting layout and text views
        LinearLayout layout = (LinearLayout)view.findViewById(R.id.imageExportLayout);

        TextView pre_text_one = (TextView)view.findViewById(R.id.imageLayout_pre_text_one);
        TextView pre_text_two = (TextView)view.findViewById(R.id.imageLayout_pre_text_two);
        TextView pre_text_three = (TextView)view.findViewById(R.id.imageLayout_pre_text_three);

        TextView post_text_one = (TextView)view.findViewById(R.id.imageLayout_post_text_one);
        TextView post_text_two = (TextView)view.findViewById(R.id.imageLayout_post_text_two);

        //Setting up typeface
        pre_text_one.setTypeface(custFont);
        pre_text_two.setTypeface(custFont);
        pre_text_three.setTypeface(custFont);

        post_text_one.setTypeface(custFont);
        post_text_two.setTypeface(custFont);


        //Setting up text and answer in text views
        post_text_one.setText(question);
        post_text_two.setText(answer);

        //To get random number so we can get color from colors array
        Random r = new Random();
        int Low = 0;
        int High = 5;
        int Result = r.nextInt(High-Low) + Low;

        layout.setBackgroundColor(Color.parseColor(colors[Result][0]));
        post_text_one.setTextColor(Color.parseColor(colors[Result][1]));
        post_text_two.setTextColor(Color.parseColor(colors[Result][1]));


        return view;
    }

所以这个功能正常工作并创建视图。我已通过以下代码验证了它,

final LinearLayout testLo = (LinearLayout)convertView.findViewById(R.id.test_lo);

if(testLo.getChildCount() > 0)
                        testLo.removeAllViewsInLayout();

                    View lv = createInflatedViewFromLayout(context,
                            questions[(Integer)v.getTag()],
                            answers[(Integer)v.getTag()],
                            finalConvertView);

                    testLo.addView(lv, 0);

我的位图图像创建功能

public static Bitmap loadBitmapFromView(View v, Context ctx) {

    Bitmap b = Bitmap.createBitmap(
            2000, 2000, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, 2000, 2000);
    v.draw(c);
    return b;
}

当我用这些代码检查图像时,

Bitmap bmp = loadBitmapFromView(testLo.findViewById(0), context);
ImageView image = new ImageView(context);
image.setImageBitmap(bmp);
testLo.addView(image);

我得到的只是彩色背景,但没有文字来自视图。我面临的位图创建功能问题是我必须手动给出高度和宽度。

我的意思是如果我使用这些代码,

Bitmap b = Bitmap.createBitmap(
                v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.layout(0, 0, v.getWidth(), v.getHeight());

或这些代码,

Bitmap b = Bitmap.createBitmap(
                v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);

通过抛出高度和宽度需要大于0的错误来崩溃应用程序。它返回-1,-2等。

要创建位图图像,我还使用了以下代码行,

Bitmap bmp = loadBitmapFromView(lv, context);

谁能告诉我哪里有问题?从布局视图中我需要更改以获得高度,宽度和创建图像。

1 个答案:

答案 0 :(得分:0)

我不确定,但我使用此样板代码从我的屏幕创建位图 - 换句话说,下面的代码实际上会截取您手机的屏幕截图:

 final View v = getWindow().getDecorView().getRootView();
 v.setDrawingCacheEnabled(true);
 Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
 v.setDrawingCacheEnabled(false);

如果您只想获取特定视图的位图,例如TextView或ImageView,您可以这样做:

 TextView v = (TextView) findViewById(R.id.example);
 v.setDrawingCacheEnabled(true);
 Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
 v.setDrawingCacheEnabled(false);