【问题标题】:Android ImageView scale bitmap to the width and crop the heightAndroid ImageView 将位图缩放到宽度并裁剪高度
【发布时间】:2015-06-26 17:17:37
【问题描述】:

我有一个固定大小的ImageView。我想实现这个:

  1. 如果Bitmap 大于或小于Imageview 宽度,则始终将Bitmap 缩放到宽度。
  2. 如果高度高于ImageView,则裁剪高度,否则将其缩放到高度。

我想要类似 answer 的东西,但反过来 (FitXCropY)。我已尝试更改此答案但没有成功。

谢谢。

【问题讨论】:

    标签: android bitmap imageview


    【解决方案1】:

    基于这个答案ImageView to scale to fixed height, but crop excess width

    public class FitXCropYImageView extends ImageView {
    boolean done = false;
    
    @SuppressWarnings("UnusedDeclaration")
    public FitXCropYImageView(Context context) {
        super(context);
        setScaleType(ScaleType.MATRIX);
    }
    
    @SuppressWarnings("UnusedDeclaration")
    public FitYCropXImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setScaleType(ScaleType.MATRIX);
    }
    
    @SuppressWarnings("UnusedDeclaration")
    public FitXCropYImageView(Context context, AttributeSet attrs, int defStyle)      {
        super(context, attrs, defStyle);
        setScaleType(ScaleType.MATRIX);
    }
    
    private final RectF drawableRect = new RectF(0, 0, 0,0);
    private final RectF viewRect = new RectF(0, 0, 0,0);
    private final Matrix m = new Matrix();
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (done) {
            return;//Already fixed drawable scale
        }
        final Drawable d = getDrawable();
        if (d == null) {
            return;//No drawable to correct for
        }
        int viewHeight = getMeasuredHeight();
        int viewWidth = getMeasuredWidth();
        int drawableWidth = d.getIntrinsicWidth();
        int drawableHeight = d.getIntrinsicHeight();
        drawableRect.set(0, 0, drawableWidth, drawableHeight);//Represents the original image
        //Compute the left and right bounds for the scaled image
        float viewHalfHeight = viewHeight / 2;
        float scale = (float) viewWidth / (float) drawableWidth;
        float scaledHeight = drawableHeight * scale;
        float scaledHalfHeight = scaledHeight / 2;
        viewRect.set(0, viewHalfHeight-scaledHalfHeight,viewWidth, viewHalfHeight+scaledHalfHeight);
    
        m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER /* This constant doesn't matter? */);
        setImageMatrix(m);
    
        done = true;
    
        requestLayout();
    }
    }
    

    【讨论】:

    • 不起作用。较小的图像被裁剪和缩放以适合屏幕。我希望仅当高度大于 imageview 高度时才裁剪图像。在所有其他情况下,图像应按比例放大或缩小。
    • 如果图像高度小于图像视图的高度,则可以使用 Matrix.ScaleToFit.FILL,如果大于图像视图的高度,则使用 Matrix.ScaleToFit.CENTER。
    • 适用于我裁剪较大图像的用例
    【解决方案2】:

    这样试试,

    在 Oncreate 中添加

    viewImage = (ImageView) findViewById(R.id.pictureImageview123);
    viewImage.setScaleType(ScaleType.FIT_XY);
    

    【讨论】:

    • 谢谢,但我认为如果 FIT_XY 高于 ImageView 高度,它会缩小位图高度,但我希望它被裁剪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多