gpt4 book ai didi

android - 将 XML 布局转换为位图图像时, ListView 项目未显示

转载 作者:行者123 更新时间:2023-11-30 00:39:14 24 4
gpt4 key购买 nike

我一直在尝试将 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>

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

最佳答案

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

  mView.buildDrawingCache(true);

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

您需要在从 View 生成位图后立即禁用 DrawingCache像下面的代码:

mView.destroyDrawingCache();

尝试下面的点击代码:

 @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();
}

使用下面的方法从 View 中获取位图你可以在这个方法中传递 View :

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

关于android - 将 XML 布局转换为位图图像时, ListView 项目未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42810892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com