如何使低分辨率图像放大并适合Android中的屏幕?

时间:2017-09-24 06:17:56

标签: android android-layout user-interface imageview

当我创建一个用于显示图像的活动时,具有低分辨率的图像只是创建它所需的空间而不仅仅适合屏幕。这是我的活动: The Activity that I created

活动的XML代码是:

<LinearLayout
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
        <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/nick"/>
</LinearLayout>

我希望ImageView能够使用所选图像的比例将图像调整到屏幕 Galary Activity

Galary App中的图像分辨率小于我在Activity中使用的图像!那怎么办呢?

1 个答案:

答案 0 :(得分:0)

使用不同的scaleType,您可以将其放大。但这可能会导致像素化和其他问题。最好使用更大的图像并缩小(或两个图像,全尺寸和缩略图),而不是缩放大多数图像。

编辑:好的,在重新阅读您的问题时,缩放类型是不够的。尝试将其用作自定义视图:

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;



public class HeightScaleImageView extends ImageView {

    public HeightScaleImageView(Context context) {
        super(context);
    }

    public HeightScaleImageView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public HeightScaleImageView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = 0;
        int height = 0;
        //Scale to parent width
        width = MeasureSpec.getSize(widthMeasureSpec);
        Drawable drawable = getDrawable();
        if (drawable != null) {
            height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
        }
        super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }
}