STT*_*LCU 10 android imageview
我有一个字节数组,其中包含从网络中获取的图像.我懒得在我的UI活动上加载它们(或者我试图至少:D)使用Bitmapfactory,BitmapDrawable和setImageDrawable.这是我的代码:
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
Run Code Online (Sandbox Code Playgroud)
这是涉及的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>
Run Code Online (Sandbox Code Playgroud)
我添加了"android:src ..."只是为了检查imageview是否可见,它是.唯一的问题是我无法改变它!我尝试了setImageBitmap,只使用我创建的位图,我尝试了setimageDrawable创建一个BitmapDrawable,但根本没有效果.没有错误,没有任何错误.拜托,哪里出错了?谢谢
Lun*_*ong 12
这是一个内存不足的问题,您可以使用下面的方法修复它,"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;
}
Run Code Online (Sandbox Code Playgroud)
我的猜测是你没有在正确的线程中这样做.您可以通过调用此代码然后旋转屏幕(或打开物理键盘,如果您有其中一部手机)来测试它.这将导致UI刷新.
无论如何,你在说什么上下文?背景线程?您需要更新UI线程中的UI,或者您必须在视图上调用类似"invalidate"的内容以强制重新绘制.
| 归档时间: |
|
| 查看次数: |
14029 次 |
| 最近记录: |