【问题标题】:Listview Items not getting displayed when converting XML Layout to bitmap image将 XML 布局转换为位图图像时未显示 Listview 项目
【发布时间】:2017-03-15 13:14:23
【问题描述】:

我一直在尝试将 XML 布局转换为位图图像。当我单击转换按钮时,正在生成图像。但我的 ListView 项目没有得到显示。我怎样才能做到这一点? 以下是我的代码

mView = findViewById(R.id.f_view);
        mButton = (Button) findViewById(R.id.button1);
        mButton.setOnClickListener(this);

        mView.setDrawingCacheEnabled(true);
        mView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
        mView.buildDrawingCache(true);
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.button1) {
            Bitmap b = Bitmap.createBitmap(mView.getDrawingCache());
            mView.setDrawingCacheEnabled(false);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

            File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Expense Calculator" + File.separator + "Expense.jpg");
            try {
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
                Toast.makeText(null, getApplicationContext()+ "Image Generated", Toast.LENGTH_SHORT).show();

                fo.close();
            } catch (Exception e) {
            }
            //finish();
        }
    }

而我的xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <include android:id="@+id/f_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout="@layout/view_expense"
        android:layout_above="@id/button1"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="54dp"
        android:background="@drawable/cimage" />

</RelativeLayout>

我应该怎么做才能显示我的列表视图项目?有人能帮助我吗? 谢谢

【问题讨论】:

    标签: android xml listview bitmap


    【解决方案1】:

    仅当您想通过单击获取位图时才启用绘图缓存。

      mView.buildDrawingCache(true);
    

    删除onCreate()方法上面的行。

    您需要在从 View 生成 Bitmap 后立即禁用 DrawingCache 如下代码:

    mView.destroyDrawingCache();
    

    试试下面的onClick代码:

     @Override
        public void onClick(View v) {
    
            if (v.getId() == R.id.button1) {
                mView.buildDrawingCache(true);
                Bitmap b = Bitmap.createBitmap(mView.getDrawingCache());
                mView.setDrawingCacheEnabled(false);
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    
                File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Expense Calculator" + File.separator + "Expense.jpg");
                try {
                    f.createNewFile();
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                    Toast.makeText(null, getApplicationContext()+ "Image Generated", Toast.LENGTH_SHORT).show();
    
                    fo.close();
                } catch (Exception e) {
                }
                //finish();
            }
    

    使用以下方法从视图中获取位图,您可以在此方法中传递视图:

    public Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),   view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
    view.draw(canvas);
    return returnedBitmap;
    }
    

    【讨论】:

    • 为什么要创建另一个位图而不是压缩 getDrawingCache() 返回的位图?
    • Jobish 想要将该位图存储到 Storage 中。
    • 我的意思是,你不能对从视图返回的位图调用压缩吗?
    • 我试过这个。但我没有得到 Listview。
    • 你想获取整个 Listview 或其 Child 的位图吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    相关资源
    最近更新 更多