我正在创建一个从网址下载图像的应用程序,将它们保存在设备上,之后必须将它们加载到具有固定大小的ImageView中 . 对于下载和保存文件我没有问题但是当我尝试在ImageView中设置图像时我有一个致命的错误,因为我的ImageView图像很大(我认为......) .
这是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff9f9f"
android:layout_margin="2dip"
android:padding="2dip">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff9f9f"
android:layout_margin="2dip"
android:padding="2dip">
<TextView
android:id="@+id/notizieTitolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/immagineNotizie"
android:text="Titolo"
android:textColor="#6b71f1"
android:textSize="20dip"
android:layout_margin="2dip" />
<TextView
android:id="@+id/notizieSottoTitolo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/immagineNotizie"
android:text="SottoTitolo"
android:textSize="15dip"
android:layout_margin="2dip"/>
<ImageView
android:background="@drawable/icona"
android:id="@+id/immagineNotizie"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_margin="2dip" />
</RelativeLayout>
</ScrollView>
是一个带有ImageView的简单布局,一个位于ImageView右侧的TextView和一个TextView
重要的是ImageView with和height . 设置为100dip(如果我计算2dip为保证金) .
这个类的代码,图像保存在Bitmap中 .
public class Notizia {
String url;
String titolo;
String sottoTitolo;
String nomeImmaginSalvata;
Bitmap immagine;
public Notizia(String tit, String sottoTit, Bitmap imm, String lk){
titolo = tit;
sottoTitolo = sottoTit;
immagine = imm;
url = lk;
nomeImmaginSalvata = nomeImmSalvata;
}
}
在这种情况下,我将在下载后立即使用该图像而不将其保存到磁盘,因为它不是必需的保存 .
这是使用xml布局创建动态布局的代码
LayoutInflater layoutInflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.ll_content);
List views = new ArrayList();
Iterator<Notizia> it = (Dati.listaNotizie).iterator();
while (it.hasNext()){
Notizia not = it.next();
View view = layoutInflator.inflate(R.layout.layout_notizie, null);
TextView textView = (TextView) view.findViewById(R.id.notizieTitolo);
Bitmap yourBitmap = not.getImmagine();
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, 10, 10, true);
TextView textView1 = (TextView) view.findViewById(R.id.notizieSottoTitolo);
ImageView img = (ImageView) findViewById(R.id.immagineNotizie);
img.setImageBitmap(resized);
textView.setText(not.getTitolo());
textView1.setText(not.getSottoTitolo());
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
insertPoint.addView((View) views.get(i));
}
我的问题是:如何在100dip(或98)中调整图像宽度和高度?
我试过了
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, width, heigh, true);
但没什么 .
3 回答
您需要将dp转换为px,因为
createScaledBitmap
使用px . 使用这个功能:不要缩放图像....
删除这个..
我认为它会起作用......
对于调整ImageView的大小,请使用以下类 .
ResizableImageView.java
和ImageView如下:用您的包名替换com.demo.demoproject .
对于加载大图像,我建议使用Aquery方法,您可以使用所需的分辨率图像对大图像进行下采样 . https://code.google.com/p/android-query/wiki/ImageLoading