【问题标题】:setImageBitmap has no visible effectsetImageBitmap 没有可见效果
【发布时间】:2017-07-24 13:07:50
【问题描述】:

我有一个字节数组,其中包含从网上获取的图像。我正在使用 Bitmapfactory、BitmapDrawable 和 setImageDrawable 将它们懒惰地加载到我的 UI 活动中(或者我正在尝试至少:D)。这是我的代码:

RelativeLayout r =(RelativeLayout) adap.getGroupView(Integer.parseInt(groupPos), false, null, null);
ImageView iv = (ImageView) r.findViewById(R.id.imgGruppo);
Log.w("",""+raw_img.length);
Bitmap bm = BitmapFactory.decodeByteArray(raw_img, 0, raw_img.length);
Drawable drawable = new BitmapDrawable(bm);
Log.i("","pre setimage"); 
iv.setImageDrawable(drawable);
//added for testing only, with no effects.
//((ELA) Activity_Titoli_2.this.getExpandableListAdapter()).notifyDataSetChanged();
//ELA is my expandable list adapter
Log.i("","post setimage"+bm.getRowBytes()); //just to see that i have actual data in raw_img and such

这里是涉及的 XML

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

    <TextView
        android:id="@+id/textNomeGruppo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Large Text"
        android:textColor="#FF0000"
        android:padding="14dp"
        android:textAppearance="?android:attr/textAppearanceLarge"  />

    <TextView
        android:id="@+id/textNoteGruppo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textNomeGruppo"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:paddingBottom="7dp"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/imgGruppo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/icon"
        />

</RelativeLayout>

我添加了“android:src...”只是为了检查 imageview 是否可见,并且确实如此。唯一的问题是我无法改变它! 我试过 setImageBitmap,只使用我创建的位图,我尝试 setimageDrawable 创建一个 BitmapDrawable,但根本没有效果。没有错误,没有什么。 请问,错在哪里?谢谢

【问题讨论】:

  • 在 iv.setImageDrawable(drawable) 之后调用 iv.invalidate() 有帮助吗?
  • 如果我将 invalidate() 添加到代码中,没有任何变化
  • 另外,感谢那个在没有说任何建设性的情况下投反对票的人。干杯,伙计。
  • 我也遇到了同样的问题,你有没有发现是什么原因造成的?
  • 不,抱歉。它发生在很多年前,在一个较旧的 android 版本中,我根本不记得了。即使我记得,我想它也不会再适用了。

标签: android imageview


【解决方案1】:

这是一个内存不足的问题,您可以使用以下方法修复它, “inSampleSize”选项将返回较小的图像并节省内存。你可以在 ImageView.setImageBitmap 中调用它

    private Bitmap loadImage(String imgPath) {
    BitmapFactory.Options options;
    try {
        options = new BitmapFactory.Options();
        options.inSampleSize = 4;// 1/4 of origin image size from width and height
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

【讨论】:

  • 我已经有一段时间没有管理这个项目了,所以我无法测试你的建议。无论如何,我会为这项努力投票,我希望这可以帮助将来的人。谢谢。
【解决方案2】:

我的猜测是您没有在正确的线程中执行此操作。您可以通过调用此代码,然后旋转屏幕(或打开物理键盘,如果您有其中一部手机)来测试这一点。这将导致 UI 刷新。

无论如何,你在什么上下文中调用它?后台线程?您可能希望在 UI 线程中更新 UI,或者您必须在 View 上调用类似“invalidate”之类的方法来强制重新绘制。

【讨论】:

  • 这是 UI“主”线程的一部分。准确的说,就是将Handler对象定义为activity类的内部成员。原始图像字节通过 asynctask 获取,并通过我实现的回调方法传递给 UI 线程(之所以有效,是因为它已在应用程序的其他地方成功使用)
  • 无论如何,我尝试旋转屏幕,但图像也没有刷新。仍显示默认图像。
【解决方案3】:

以下解决方案对我有用。我使用了 Picasa 库。

private Uri getImageUri(Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(activity.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public void bitmapIntoImageView(ImageView imageView, Bitmap bitmap, Activity activity){
    Uri imageUri = getImageUri(bitmap);
    Picasso.with(activity).load(imageUri).into(imageView);
}

public void imagePathIntoImageView(ImageView imageView, String imagePath, Activity activity) {
    Uri imageUri = Uri.fromFile(new File(imagePath));
    Picasso.with(activity).load(imageUri).into(imageView);
}

Picasa:http://square.github.io/picasso/

调用下面的函数将位图设置为ImageView

bitmapIntoImageView(imageView, bitmap, MainActivity.this)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2013-08-15
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多