package com.gh.gamecenter.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.gamecenter.common.R;

/**
 * @author CsHeng
 * @Date 17/06/2017
 * @Time 2:46 PM
 */

public class ScaleSimpleDraweeView extends SimpleDraweeView {

    private boolean mMeasureByHeight = false; // default width

    private float mFraction = 1f;// default square

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

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

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

    public ScaleSimpleDraweeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScaleView);
        int enumV = a.getInt(R.styleable.ScaleView_measureBy, 0);
        mFraction = a.getFloat(R.styleable.ScaleView_fractionValue, mFraction);
        if (enumV != 0) {
            mMeasureByHeight = true;
        }
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (mMeasureByHeight) {
            setMeasuredDimension((int) (height * mFraction), height);
        } else {
            setMeasuredDimension(width, (int) (width * mFraction));
        }
    }


}
