package com.sheep.jiuyan.samllsheep.page.view;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  ScrollerLine</p>
 * @ <p>Description: 滚动下滑线</p>
 * @ date:  2015/09/16 15:08
 * @ QQ:    315096953
 */
public class ScrollerLine extends View {
    private int mAllWidth;
    private int mWidth = 0;


    public ScrollerLine(Context context) {
        super(context);
        init();
    }

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

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

    /**
     * 初始化
     */
    private void init() {
        Point point = new Point();
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getSize(point);
        mAllWidth = point.x;
    }


    /**
     * @param viewGroup
     * @param degree
     * @param from
     * @param averagecount 总的有几个滑动范围
     */
    public void moveDegree(ViewGroup viewGroup, float degree, int from, int averagecount) {
        if (degree < 0.0001)
            return;
        int aver = mAllWidth / averagecount;
        int distance = (int) (aver * degree);
        distance += from * aver + (aver - getRightWidth()) / 2;
        viewGroup.setPadding(distance, 0, 0, 0);
    }


    /**
     * 取宽。
     *
     * @return
     */
    private int getRightWidth() {
        int width = getWidth();
        if (width == 0) {
            width = getMeasuredWidth();
        }
        if (width == 0) {
            width = getLayoutParams().width;
        }
        return width;
    }

}
