package
com.example.p031_mokuaihua_viewpager_fragment.demo3.utils;
import
android.content.Context;
import
android.content.res.TypedArray;
import
android.graphics.Canvas;
import
android.graphics.Color;
import
android.graphics.Paint;
import
android.graphics.Rect;
import
android.os.Bundle;
import
android.os.Parcelable;
import
android.support.v4.view.ViewPager;
import
android.text.TextUtils;
import
android.util.AttributeSet;
import
android.view.View;
import
android.widget.LinearLayout;
import
com.example.p031_mokuaihua_viewpager_fragment.R;
/**
* 首页viewpager指示符<br/>
* Created by geek on 2016/7/29.
*/
public
class
IndexPagerIndicator
extends
LinearLayout {
private
int
mItemWidth;
private
int
mItemSelectedHeight;
private
int
mItemInterval;
private
int
mItemColor;
private
int
mStartX;
private
int
mCurrentX;
private
Paint mSelectedPaint;
private
IStyleDrawer mStyleDrawer;
public
IndexPagerIndicator(Context context) {
this
(context,
null
,
0
);
}
public
IndexPagerIndicator(Context context, AttributeSet attrs) {
this
(context, attrs,
0
);
}
public
IndexPagerIndicator(Context context, AttributeSet attrs,
int
defStyleAttr) {
super
(context, attrs, defStyleAttr);
setBackgroundColor(Color.TRANSPARENT);
setOrientation(LinearLayout.HORIZONTAL);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IndexPagerIndicator, defStyleAttr,
0
);
mItemWidth = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_width,
10
);
int
itemHeight = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_height,
2
);
mItemSelectedHeight = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_select_height,
0
);
if
(mItemSelectedHeight ==
0
) { mItemSelectedHeight = itemHeight;}
mItemInterval = ta.getDimensionPixelSize(R.styleable.IndexPagerIndicator_indicator_interval,
0
);
mItemColor = ta.getColor(R.styleable.IndexPagerIndicator_indicator_item_color, Color.BLACK);
int
selectedColor = ta.getColor(R.styleable.IndexPagerIndicator_indicator_select_color, Color.WHITE);
String styleDrawer = ta.getString(R.styleable.IndexPagerIndicator_indicator_style_drawer);
try
{
if
(TextUtils.isEmpty(styleDrawer)) {
throw
new
Exception();
}
mStyleDrawer = (IStyleDrawer) Class.forName(styleDrawer).newInstance();
}
catch
(Exception e) {
e.printStackTrace();
mStyleDrawer =
new
DefaultStyleDrawer();
}
ta.recycle();
mStyleDrawer.prepare(mItemWidth, itemHeight, mItemSelectedHeight);
mSelectedPaint =
new
Paint(Paint.ANTI_ALIAS_FLAG);
mSelectedPaint.setColor(selectedColor);
}
@Override
protected
void
onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec) {
super
.onMeasure(widthMeasureSpec, heightMeasureSpec);
if
(mItemSelectedHeight > getMeasuredHeight()) {
setMeasuredDimension(getMeasuredWidth(), mItemSelectedHeight);
}
mStartX = (getMeasuredWidth() - (getChildCount() * mItemWidth