Android:调整图像的宽度和高度

Android:调整图像的宽度和高度,android,image,bitmap,resize,Android,Image,Bitmap,Resize,我正在创建一个应用程序,从url下载图像,保存在设备上,之后必须将其加载到固定大小的ImageView中。 对于文件的下载和保存,我没有问题,但是当我尝试在ImageView中设置图像时,我有一个致命的错误,因为图像对于我的ImageView来说太大了(我想…) 这是xml文件: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res

我正在创建一个应用程序,从url下载图像,保存在设备上,之后必须将其加载到固定大小的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>
在这种情况下,我将在下载后立即使用该映像,而不将其保存到磁盘,因为不需要保存

这是使用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));
    }

但是什么都没有。

您需要从dp转换到px,因为
createScaledBitmap
使用px。使用以下功能:

private float pxFromDp(float dp)
{
    return dp * getResources().getDisplayMetrics().density;
}

private float dpFromPx(float px)
{
    return px / getResources().getDisplayMetrics().density;
}

您需要从dp转换为px,因为
createScaledBitmap
使用px。使用以下功能:

private float pxFromDp(float dp)
{
    return dp * getResources().getDisplayMetrics().density;
}

private float dpFromPx(float px)
{
    return px / getResources().getDisplayMetrics().density;
}

不要缩放图像

删除此

  Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, 10, 10, true);

我想那会有用的…

不要缩放图像

删除此

  Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, 10, 10, true);

我想它会起作用的…

使用下面的类来调整ImageView的大小

resizebleimageview.java

public class ResizableImageView extends ImageView {

public ResizableImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable d = getDrawable();

    if (d != null) {
        // ceil not round - avoid thin vertical gaps along the left/right
        // edges
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = (int) Math.ceil((float) width* (float) d.getIntrinsicHeight()/     (float) d.getIntrinsicWidth());
        // int height=10;
        setMeasuredDimension(width, height);
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
}
和具有以下内容的ImageView: 用包名替换com.demo.demoproject

<com.demo.demoproject.ResizableImageView
    android:id="@+id/immagineNotizie"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/icona"/>

对于加载大图像,我建议使用Aquery方法,在这种方法中,您可以对具有所需分辨率的大图像进行降采样。

要调整ImageView的大小,请使用以下类

resizebleimageview.java

public class ResizableImageView extends ImageView {

public ResizableImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable d = getDrawable();

    if (d != null) {
        // ceil not round - avoid thin vertical gaps along the left/right
        // edges
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = (int) Math.ceil((float) width* (float) d.getIntrinsicHeight()/     (float) d.getIntrinsicWidth());
        // int height=10;
        setMeasuredDimension(width, height);
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
}
和具有以下内容的ImageView: 用包名替换com.demo.demoproject

<com.demo.demoproject.ResizableImageView
    android:id="@+id/immagineNotizie"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/icona"/>

对于加载大图像,我建议使用Aquery方法,在这种方法中,您可以对具有所需分辨率的大图像进行降采样。