如何设置ImageView的scaletype到topCrop [英] How set imageview scaletype to topCrop

查看:711
本文介绍了如何设置ImageView的scaletype到topCrop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码Android和我有一个ImageView的。我想设置这样scaletype到topcrop。
我能找到的选项centercrop,但它不是我的要求。我该怎么办?

I'm coding android and I have a imageview. I want to set scaletype of this to topcrop. I could find centercrop in options, but it's not my request. How do I do?

推荐答案

定制的Andr​​oid的ImageView所包含绘制的顶部作物比例。

Custom Android ImageView for top-crop scaling of the contained drawable.

import android.content.Context;
import android.graphics.Matrix;
import android.widget.ImageView;

/**
* ImageView to display top-crop scale of an image view.
*
* @author Chris Arriola
*/
public class TopCropImageView extends ImageView {

public TopCropImageView(Context context) {
    super(context);
    setScaleType(ScaleType.MATRIX);
}

@Override
protected boolean setFrame(int l, int t, int r, int b) {
    final Matrix matrix = getImageMatrix();

    float scale;
    final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
    final int drawableWidth = getDrawable().getIntrinsicWidth();
    final int drawableHeight = getDrawable().getIntrinsicHeight();

    if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
        scale = (float) viewHeight / (float) drawableHeight;
    } else {
        scale = (float) viewWidth / (float) drawableWidth;
    }

    matrix.setScale(scale, scale);
    setImageMatrix(matrix);

    return super.setFrame(l, t, r, b);
}        
}

https://gist.github.com/arriolac/3843346

这篇关于如何设置ImageView的scaletype到topCrop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆