setImageBitmap没有可见的影响setImageBitmap

2023-09-08 09:58:23 作者:无眠

我有一个字节数组,其中包含从网上获取的图像。我懒洋洋地加载它们在我的UI活动(或我试图至少为:D)采用Bitmapfactory,BitmapDrawable和setImageDrawable。这里是我的code:

I have a byte array which contains an image fetched from the net. I am lazily loading them on my UI activity (or i am trying to at least :D ) using Bitmapfactory, BitmapDrawable and setImageDrawable. here is my code:

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

here is the XML involved

<?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>

我已经加入机器人:SRC ......只是为了检查是否ImageView的是可见的,并且它是。唯一的问题是,我不能改变它!我曾尝试setImageBitmap,只用我创建位图,我想setimageDrawable创建BitmapDrawable,但都没有效果。没有错误,什么都没有。请,哪里是错?谢谢

I have added "android:src..." just to check if the imageview is visible, and it is. The only problem is that i cannot change it! I have tried setImageBitmap, using only the bitmap i created, i tried setimageDrawable creating a BitmapDrawable, but no effects at all. no errors, no nothing. Please, where is the mistake? thank you

推荐答案

我的猜测是你没有在正确的线程这样做。您可以通过调用这个code,然后旋转屏幕测试了这一点(或打开物理键盘,如果你有这些手机之一)。这将导致UI刷新。

My guess is you're not doing this in the right thread. You could test this out by calling this code, then rotating the screen (or opening the physical keyboard, if you have one of those phones). This will cause the UI to refresh.

总之,什么情况下你在调用这个?后台线程?你想要在UI线程更新UI,或者你必须呼吁观像无效,以让它强制重新绘制。

Anyway, what context are you calling this in? Background thread? You'd want to update the UI in the UI thread, or you'll have to call something like 'invalidate' on the View to get it to force a re-draw.