package com.rv.home.rv.until;

import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.request.target.ImageViewTarget;

public class TransformationUtils extends ImageViewTarget<Bitmap> {

    private ImageView target;
    private TransforationListener listener;

    public TransformationUtils(ImageView target, TransforationListener listener) {
        super(target);
        this.target = target;
        this.listener = listener;
    }

    @Override
    protected void setResource(Bitmap resource) {
        target.setImageBitmap(resource);

        if (resource == null) return;
        //获取原图的宽高
        int width = resource.getWidth();
        int height = resource.getHeight();

        //获取imageView的宽
        int imageViewWidth = target.getWidth();

        //计算缩放比例
        float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1);
        if (listener != null) listener.getSy(sy);

        //计算图片等比例放大后的高
        int imageViewHeight = (int) (height * sy);
        ViewGroup.LayoutParams params = target.getLayoutParams();
        params.height = imageViewHeight;
        target.setLayoutParams(params);
    }

    public interface TransforationListener {
        void getSy(float sy);
    }
}