主要核心代码
package com.example.netprobe.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import com.example.netprobe.R;
/**
*
* @ClassName: TimeLineMarker
* @Description: 工作笔记页面时间轴控件
*
*/
public class TimeLineMarker extends View {
//线的宽度
private int mLineSize;
//标记的大小
private int mMarkerSize;
//开始的线
private Drawable mBeginLine;
//结束的线
private Drawable mEndLine;
//标记的图片
private Drawable mMarkerDrawable;
//时光轴线与图片间的间隙
private int drawablePadding;
public TimeLineMarker(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
private void init(AttributeSet attrs) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TimeLineMarker);
mMarkerSize = typedArray.getDimensionPixelSize(R.styleable.TimeLineMarker_markerSize, 26);
mLineSize = typedArray.getDimensionPixelSize(R.styleable.TimeLineMarker_lineSize, 16);
mBeginLine = typedArray.getDrawable(R.styleable.TimeLineMarker_beginLine);
mEndLine = typedArray.getDrawable(R.styleable.TimeLineMarker_endLine);
mMarkerDrawable = typedArray.getDrawable(R.styleable.TimeLineMarker_marker);
drawablePadding = typedArray.getDimensionPixelOffset(R.styleable.TimeLineMarker_drawablePadding, 10);
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int w = mMarkerSize + getPaddingLeft() + getPaddingRight();
int h = mMarkerSize + getPaddingTop() + getPaddingBottom();
int widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);
int heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);
setMeasuredDimension(widthSize, heightSize);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
initDrawable();
}
int x;
int y;
int contentWidth;
int contentHeight;
private void initDrawable() {
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int width = getWidth();
int height = getHeight();
contentWidth = width - paddingLeft - paddingRight;
contentHeight = height - paddingTop - paddingBottom;
x = width >> 1;
y = height >> 1;
int ms;
if (mMarkerDrawable != null) {
int markerSize = Math.min(mMarkerSize, Math.min(contentWidth, contentHeight));
ms = markerSize >> 1;
mMarkerDrawable.setBounds(x - ms, y - ms, x + ms, y + ms);
}
int lineLeft = 0;
lineLeft = x - (mLineSize >> 1);
if (mBeginLine != null) {
mBeginLine.setBounds(lineLeft, 0, lineLeft + mLineSize, mMarkerDrawable == null ? y : y - drawablePadding);
} else {
}
if (mEndLine != null) {
mEndLine.setBounds(lineLeft, mMarkerDrawable == null ? y : y + drawablePadding, lineLeft + mLineSize, height);
} else {
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBeginLine != null) {
mBeginLine.draw(canvas);
}
if (mEndLine != null) {
mEndLine.draw(canvas);
}
if (mMarkerDrawable != null) {
mMarkerDrawable.draw(canvas);
}
}
public void setMarkerDrawable(Drawable markerDrawable) {
this.mMarkerDrawable = markerDrawable;
initDrawable();
invalidate();
}
}
attrs属性
<!-- 时光轴 浏览历史 -->
<declare-styleable name="TimeLineMarker">
<attr name="markerSize" format="dimension" />
<attr name="marker" format="color|reference" />
<attr name="beginLine" format="color|reference" />
<attr name="endLine" format="color|reference" />
<attr name="lineSize" format="dimension" />
<attr name="drawablePadding" format="dimension" />
</declare-styleable>