【问题标题】:Android is not creating bitmap image from viewAndroid 没有从视图中创建位图图像
【发布时间】:2015-12-19 20:32:21
【问题描述】:

我正在开发一个 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);

谁能告诉我哪里有问题?我需要更改什么才能从布局视图获取高度、宽度和创建图像。

【问题讨论】:

    标签: android view bitmap android-bitmap


    【解决方案1】:

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

     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);
    

    【讨论】:

    • 我会试试这个,但我不想截屏,因为视图将在后台生成并带有详细信息。不会显示。
    • 没关系。然后不要使用第一个示例,而是使用我给你的第二个示例并将其设置为您想要制作位图的视图。
    • 它不工作,同样的问题。它返回仅具有背景颜色的空白图像。高度和宽度也有同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    相关资源
    最近更新 更多