FitWidth ImageView和TopCrop ImageView

FitWidth ImageView: 宽度自适应

  <com.kk.drama.view.widget.FitWidthImageView
            android:id="@+id/show_images"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/default_picture" />



public class FitWidthImageView extends ImageView
{
   
    public FitWidthImageView(Context context) {
        super(context);
        setup();
    }
   
    public FitWidthImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup();
    }
   
    public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setup();
    }
   
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
        setMeasuredDimension(width, height);
    }
   
    private void setup() {
        setScaleType(ScaleType.CENTER_CROP);
    }

}



TopCrop ImageView : 从头部Crop而不是center

 
<com.kk.drama.view.widget.FitWidthImageView
            android:id="@+id/show_images"
            android:layout_width="match_parent"
            android:layout_height="227dp"
            android:src="@drawable/default_picture" />

自己改名
public class FitWidthImageView extends ImageView
{
   
    public FitWidthImageView(Context context) {
        super(context);
        setup();
    }
   
    public FitWidthImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup();
    }
   
    public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setup();
    }
   
    private void setup() {
        setScaleType(ScaleType.CENTER_CROP);
        setScaleType(ScaleType.MATRIX);
    }
   
    @Override
    protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) {
        float frameWidth = frameRight - frameLeft;
        float frameHeight = frameBottom - frameTop;
       
        float originalImageWidth = (float) getDrawable().getIntrinsicWidth();
        float originalImageHeight = (float) getDrawable().getIntrinsicHeight();
       
        float usedScaleFactor = 1;
       
        if ((frameWidth > originalImageWidth) || (frameHeight > originalImageHeight)) {
            // If frame is bigger than image
            // => Crop it, keep aspect ratio and position it at the bottom and center horizontally
           
            float fitHorizontallyScaleFactor = frameWidth / originalImageWidth;
            float fitVerticallyScaleFactor = frameHeight / originalImageHeight;
           
            usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor);
        }
       
        float newImageWidth = originalImageWidth * usedScaleFactor;
        float newImageHeight = originalImageHeight * usedScaleFactor;
       
        Matrix matrix = getImageMatrix();
        matrix.setScale(usedScaleFactor, usedScaleFactor, 0, 0); // Replaces the old matrix completly
        // matrix.postTranslate((frameWidth - newImageWidth) / 2, frameHeight - newImageHeight);//BottomCrop
        matrix.postTranslate((frameWidth - newImageWidth) / 2, 0);//Top Crop
        setImageMatrix(matrix);
        return super.setFrame(frameLeft, frameTop, frameRight, frameBottom);
    }
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值