如何将低分辨率图像放大并适合安卓屏幕?
问题描述:
当我创建一个活动来显示图像时,分辨率低的图像只是简单地占用它所需的空间,而不仅仅适合屏幕。这是我的活动: 如何将低分辨率图像放大并适合安卓屏幕?
活动的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>
Galary应用程序中的图像的分辨率比我在活动中使用的图像小!那么这是如何完成的?
答
使用不同的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));
}
}
我已经使用了每个缩放选项。没有用!是像素化是一个问题,但我希望这个功能在我的应用程序。因为用户可能会选择一个像素化图像。 –
@FebinMathew检查我的编辑。您是对的,缩放类型不足以将高宽比缩放为带有wrap_content高度的match_parent。上面的代码应该可以工作 –