封面图像裁剪Android

时间:2017-07-13 06:39:20

标签: android image image-processing

我想知道封面图片裁剪的确切尺寸。我的应用程序的UI设计如下。我如何裁剪图像以适合所有版本的Android手机。 如果我采用位图图像的确切长度和​​宽度。后台线程需要很多时间来加载图像。 enter image description here

下面给出了我的图像裁剪代码,但这只适用于边缘会导致大量空白区域的图像大小。

public class ProportionalImageView extends AppCompatImageView {

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

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

public ProportionalImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable d = getDrawable();
    if (d != null) {
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
        setMeasuredDimension(w, h);
    }
    else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

提前致谢

1 个答案:

答案 0 :(得分:1)

Drawable d = getDrawable();

给定内部位图大小与原始位图高度和宽度不同。 为此,您需要使用设备屏幕高度和宽度并根据您的ImageView高度和宽度计算高度和宽度。

此外,如果您正在使用位图进行任何裁剪操作,那么需要了解图像的宽高比和建议的宽高比。因此,当您实际剪切图像时,图像质量不会降低。