实例一
02.jpg第一步: 继承 SingleTimeLineDecoration
/**
* Created on 2021/7/16 15:44
*
* @author Gong Youqiang
*/
public class WeekPlanSTL extends SingleTimeLineDecoration {
public WeekPlanSTL(Config config) {
super(config);
}
@Override
protected void onDrawTitleItem(Canvas canvas, int left, int top, int right, int bottom, int pos) {
ITimeItem item = timeItems.get(pos);
String title = item.getTitle();
if (TextUtils.isEmpty(title))
return;
Rect mRect = new Rect();
mTextPaint.setColor(item.getColor());
mTextPaint.getTextBounds(title, 0, title.length(), mRect);
int centerX = (left + right) / 2;
int centerY = (bottom + top) / 2;
int x, y;
x = centerX - mRect.width() / 2;
y = centerY + mRect.height() / 2;
canvas.drawText(title, x, y, mTextPaint);
}
@Override
protected void onDrawDotResItem(Canvas canvas, int cx, int cy, int radius, Drawable drawable, int pos) {
super.onDrawDotResItem(canvas, cx, cy, radius, drawable, pos);
if (drawable != null) {
int height = drawable.getIntrinsicHeight();
int width = drawable.getIntrinsicWidth();
int left = cx - width / 2;
int top = cy - height / 2;
int right = cx + width / 2;
int bottom = cy + height / 2;
drawable.setBounds(left, top, right, bottom);
drawable.draw(canvas);
mDotPaint.setStyle(Paint.Style.STROKE);
mDotPaint.setColor(Color.parseColor("#ffffff"));
mDotPaint.setStrokeWidth(DisplayUtils.dip2px(2));
canvas.drawCircle(cx, cy, width / 2 - DisplayUtils.dip2px(3), mDotPaint);
}
}
}
第二步: 创建数据,实现 ITimeItem 接口
/**
* Created on 2021/7/16 15:07
*
* @author Gong Youqiang
*/
public class TimeItem implements ITimeItem {
private String name;
private String title;
private String detail;
private int color;
private int res;
public TimeItem(String name, String title, String detail, int color, int res) {
this.name = name;
this.title = title;
this.detail = detail;
this.color = color;
this.res = res;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setTitle(String title) {
this.title = title;
}
public void setColor(int color) {
this.color = color;
}
public int getRes() {
return res;
}
public void setRes(int res) {
this.res = res;
}
@Override
public String getTitle() {
return title;
}
@Override
public int getColor() {
return color;
}
@Override
public int getResource() {
return res;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public static List<TimeItem> initTimeInfo(){
List<TimeItem> items = new ArrayList<>();
items.add(new TimeItem("喝茶", "10-01,周二", "第一天养养生吧~", Color.parseColor("#f36c60"), R.drawable.timeline_ic_tea));
items.add(new TimeItem("喝酒", "06-12,周三", "今天找老徐吃烧烤", Color.parseColor("#ab47bc"), R.drawable.timeline_ic_drink));
items.add(new TimeItem("画画", "07-07,周四", "去鼋头渚写生", Color.parseColor("#aed581"), R.drawable.timeline_ic_draw));
items.add(new TimeItem("高尔夫", "08-20,周五", "约个高尔夫", Color.parseColor("#5FB29F"), R.drawable.timeline_ic_golf));
items.add(new TimeItem("游泳", "09-16,周六", "今天来洗个澡", Color.parseColor("#ec407a"), R.drawable.timeline_ic_bath));
items.add(new TimeItem("温泉", "10-01,周日", "快上班了好好休息", Color.parseColor("#ffd54f"), R.drawable.timeline_ic_footer));
return items;
}
}
第三步:创建适配器
/**
* Created on 2021/7/16 15:25
*
* @author Gong Youqiang
*/
public abstract class RecyclerAdapter<Data> extends BaseAdapter<Data> {
public RecyclerAdapter() {
}
public RecyclerAdapter(AdapterListener<Data> adapterListener) {
super(adapterListener);
}
public RecyclerAdapter(List<Data> mDataList, AdapterListener<Data> adapterListener) {
super(mDataList, adapterListener);
}
@Override
protected void doWithRoot(BaseAdapter.ViewHolder viewHolder, View root) {
super.doWithRoot(viewHolder, root);
((RecyclerAdapter.ViewHolder)viewHolder).unbinder = ButterKnife.bind(viewHolder,root);
}
/**
* 自定义的ViewHolder
*/
public static abstract class ViewHolder<Data> extends BaseAdapter.ViewHolder<Data>{
public Unbinder unbinder;
public ViewHolder(View itemView) {
super(itemView);
}
}
}
第四步:创建时间轴
public class SingleTimeLineActivity extends BaseActivity {
@BindView(R.id.rv_content)
RecyclerView mRecyclerView;
private RecyclerAdapter<TimeItem> mAdapter;
@Override
public int getLayoutId() {
return R.layout.activity_single_time_line;
}
@Override
public void initView() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter = new RecyclerAdapter<TimeItem>() {
@Override
public ViewHolder<TimeItem> onCreateViewHolder(View root, int viewType) {
return new WeekPlanViewHolder(root);
}
@Override
public int getItemLayout(TimeItem s, int position) {
return R.layout.two_side_left_recycle_item;
}
});
List<TimeItem> timeItems = TimeItem.initTimeInfo();
mAdapter.addAllData(timeItems);
TimeLine timeLine = provideTimeLine(timeItems);
mRecyclerView.addItemDecoration(timeLine);
}
private TimeLine provideTimeLine(List<TimeItem> timeItems) {
return new TimeLine.Builder(this, timeItems)
.setTitle(Color.parseColor("#8d9ca9"), 14)
.setTitleStyle(TimeLine.FLAG_TITLE_TYPE_LEFT, 100)
.setLine(TimeLine.FLAG_LINE_BEGIN_TO_END, 40, Color.parseColor("#757575"),1)
.setDot(TimeLine.FLAG_DOT_RES)
.build(WeekPlanSTL.class);
}
class WeekPlanViewHolder extends RecyclerAdapter.ViewHolder<TimeItem> {
@BindView(R.id.tv_name)
TextView mNameTv;
@BindView(R.id.tv_detail)
TextView mDetailTv;
@BindView(R.id.btn_go)
TextView mGoBtn;
@BindView(R.id.btn_write)
TextView mWriteBtn;
WeekPlanViewHolder(View itemView) {
super(itemView);
}
@Override
protected void onBind(TimeItem timeItem) {
mNameTv.setText(timeItem.getName());
mDetailTv.setText(timeItem.getDetail());
setColor(timeItem.getColor());
}
private void setColor(int color){
mGoBtn.setBackgroundColor(color);
mWriteBtn.setBackgroundColor(color);
}
}
}
实例二
03.jpg第一步: 继承 SingleTimeLineDecoration
/**
* Created on 2021/7/16 15:59
*
* @author Gong Youqiang
*/
public class NoteInfoSTL extends SingleTimeLineDecoration {
private Paint strTextPaint;
private Paint dayTextPaint;
private int space;
public NoteInfoSTL(Config config) {
super(config);
strTextPaint = new Paint();
strTextPaint.setTextSize(DisplayUtils.sp2px(mContext,12));
strTextPaint.setColor(Color.parseColor("#8d6e63"));
dayTextPaint = new Paint();
dayTextPaint.setTextSize(DisplayUtils.sp2px(mContext,20));
dayTextPaint.setColor(Color.parseColor("#8d6e63"));
dayTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
space = DisplayUtils.dip2px(8);
}
@Override
protected void onDrawTitleItem(Canvas canvas, int left, int top, int right, int bottom, int pos) {
DateInfo timeItem = (DateInfo) timeItems.get(pos);
Date date = timeItem.getDate();
if(date != null){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String year = Integer.toString(calendar.get(Calendar.YEAR));
String mon = calendar.get(Calendar.MONTH)<9?"0"+(calendar.get(Calendar.MONTH)+1)
:Integer.toString(calendar.get(Calendar.MONTH+1));
String str = year + "-" +mon;
int n = calendar.get(Calendar.DAY_OF_MONTH);
String day = n<10?"0"+n:Integer.toString(n);
Rect monRect = new Rect();
strTextPaint.getTextBounds(str,0,str.length(),monRect);
Rect dayRect = new Rect();
dayTextPaint.getTextBounds(day,0,day.length(),dayRect);
int monWidth = monRect.width();
int monHeight = monRect.height();
int dayWidth = dayRect.width();
int dayHeight = dayRect.height();
int cy = (top + bottom)/2;
int cx = (left + right)/2;
int beginY = cy - space/2;
canvas.drawText(day,cx-dayWidth/2,beginY,dayTextPaint);
beginY = cy + space/2 + monHeight;
canvas.drawText(str,cx-monWidth/2,beginY,strTextPaint);
}
}
@Override
protected void onDrawDotItem(Canvas canvas, int cx, int cy, int radius, int pos) {
// 不绘制
}
}
第二步: 创建数据,实现 ITimeItem 接口
/**
* Created on 2021/7/16 15:56
*
* @author Gong Youqiang
*/
public class DateInfo implements ITimeItem {
private String name;
private String detail;
private Date date;
private int color;
public DateInfo(String name, String detail, Date date, int color) {
this.name = name;
this.detail = detail;
this.date = date;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public void setColor(int color) {
this.color = color;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String getTitle() {
return null;
}
@Override
public int getColor() {
return color;
}
@Override
public int getResource() {
return 0;
}
public static List<DateInfo> initDateInfo() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
List<DateInfo> items = new ArrayList<>();
items.add(new DateInfo("喝茶", "第一天养养生吧~", calendar.getTime(), Color.parseColor("#f36c60")));
calendar.add(Calendar.DAY_OF_MONTH, 1);
items.add(new DateInfo("喝酒", "今天找老徐吃烧烤", calendar.getTime(), Color.parseColor("#ab47bc")));
calendar.add(Calendar.DAY_OF_MONTH, 1);
items.add(new DateInfo("画画", "去鼋头渚写生", calendar.getTime(), Color.parseColor("#aed581")));
calendar.add(Calendar.DAY_OF_MONTH, 1);
items.add(new DateInfo("高尔夫", "约个高尔夫", calendar.getTime(), Color.parseColor("#5FB29F")));
calendar.add(Calendar.DAY_OF_MONTH, 1);
items.add(new DateInfo("游泳", "今天来洗个澡", calendar.getTime(), Color.parseColor("#ec407a")));
calendar.add(Calendar.DAY_OF_MONTH, 1);
items.add(new DateInfo("温泉", "快上班了好好休息", calendar.getTime(), Color.parseColor("#ffd54f")));
return items;
}
}
/**
* Created on 2021/7/16 15:54
*
* @author Gong Youqiang
*/
public class NoteInfo extends DateInfo{
public static final int NOTE_IMG = 1;
public static final int NOTE_TEXT = 2;
private int type;
public NoteInfo(String name, String detail, Date date, int color, int type) {
super(name, detail, date, color);
this.type = type;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String getTitle() {
return DisplayUtils.date2SDayFormat(getDate());
}
public static List<NoteInfo> initNoteInfo(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
List<NoteInfo> items = new ArrayList<>();
items.add(new NoteInfo("喝茶", "第一天养养生吧~", calendar.getTime(), Color.parseColor("#f36c60"), NOTE_IMG));
items.add(new NoteInfo("喝酒", "今天找老徐吃烧烤", calendar.getTime(), Color.parseColor("#ab47bc"), NOTE_TEXT));
items.add(new NoteInfo("画画", "去鼋头渚写生", calendar.getTime(), Color.parseColor("#aed581"), NOTE_TEXT));
calendar.add(Calendar.DAY_OF_MONTH, 1);
items.add(new NoteInfo("高尔夫", "约个高尔夫", calendar.getTime(), Color.parseColor("#5FB29F"),NOTE_IMG ));
calendar.add(Calendar.DAY_OF_MONTH, 1);
items.add(new NoteInfo("游泳", "今天来洗个澡", calendar.getTime(), Color.parseColor("#ec407a"), NOTE_TEXT));
items.add(new NoteInfo("温泉", "快上班了好好休息", calendar.getTime(), Color.parseColor("#0D47A1"), NOTE_TEXT));
return items;
}
}
第三步:创建适配器
/**
* Created on 2021/7/16 15:25
*
* @author Gong Youqiang
*/
public abstract class RecyclerAdapter<Data> extends BaseAdapter<Data> {
public RecyclerAdapter() {
}
public RecyclerAdapter(AdapterListener<Data> adapterListener) {
super(adapterListener);
}
public RecyclerAdapter(List<Data> mDataList, AdapterListener<Data> adapterListener) {
super(mDataList, adapterListener);
}
@Override
protected void doWithRoot(BaseAdapter.ViewHolder viewHolder, View root) {
super.doWithRoot(viewHolder, root);
((RecyclerAdapter.ViewHolder)viewHolder).unbinder = ButterKnife.bind(viewHolder,root);
}
/**
* 自定义的ViewHolder
*/
public static abstract class ViewHolder<Data> extends BaseAdapter.ViewHolder<Data>{
public Unbinder unbinder;
public ViewHolder(View itemView) {
super(itemView);
}
}
}
第四步:创建时间轴
public class SingleInfoActivity extends BaseActivity {
@BindView(R.id.rv_content)
RecyclerView mRecyclerView;
private RecyclerAdapter<NoteInfo> mAdapter;
@Override
public int getLayoutId() {
return R.layout.activity_single_info;
}
@Override
public void initView() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter = new RecyclerAdapter<NoteInfo>() {
@Override
public ViewHolder<NoteInfo> onCreateViewHolder(View root, int viewType) {
return new DateInfoHolder(root);
}
@Override
public int getItemLayout(NoteInfo s, int position) {
if (s.getType() == NoteInfo.NOTE_IMG) {
return R.layout.note_info_img_recycle_item;
}
return R.layout.note_info_text_recycle_item;
}
});
List<NoteInfo> timeItems = NoteInfo.initNoteInfo();
mAdapter.addAllData(timeItems);
TimeLine timeLine = provideTimeLine(timeItems);
mRecyclerView.addItemDecoration(timeLine);
}
private TimeLine provideTimeLine(List<NoteInfo> timeItems) {
return new TimeLine.Builder(this, timeItems)
.setTitle(Color.parseColor("#ffffff"), 20)
.setTitleStyle(SingleTimeLineDecoration.FLAG_TITLE_TYPE_LEFT, 80)
.setLine(SingleTimeLineDecoration.FLAG_LINE_CONSISTENT, 0, Color.parseColor("#00000000"))
.setDot(SingleTimeLineDecoration.FLAG_DOT_DRAW)
.setSameTitleHide()
.build(NoteInfoSTL.class);
}
class DateInfoHolder extends RecyclerAdapter.ViewHolder<NoteInfo> {
@BindView(R.id.tv_name)
TextView mNameTv;
@BindView(R.id.tv_detail)
TextView mDetailTv;
DateInfoHolder(View itemView) {
super(itemView);
}
@Override
protected void onBind(NoteInfo timeItem) {
mNameTv.setText(timeItem.getName());
mDetailTv.setText(timeItem.getDetail());
}
}
}
实例三
02.jpg第一步: 继承 SingleTimeLineDecoration
/**
* Created on 2021/7/16 15:44
*
* @author Gong Youqiang
*/
/**
* Created on 2021/7/16 16:20
*
* @author Gong Youqiang
*/
public class SocialMediaSTL extends SingleTimeLineDecoration {
private static final String[] MONTHS = new String[]{
"/1月", "/2月", "/2月", "/4月", "/5月", "/6月", "/7月", "/8月", "/9月", "/10月", "/11月", "/12月"
};
private int r;
private Paint monTextPaint;
private Paint dayTextPaint;
private int space;
public SocialMediaSTL(Config config) {
super(config);
r = DisplayUtils.dip2px(24);
monTextPaint = new Paint();
monTextPaint.setTextSize(DisplayUtils.sp2px(mContext, 10));
monTextPaint.setColor(Color.parseColor("#F5F5F5"));
dayTextPaint = new Paint();
dayTextPaint.setTextSize(DisplayUtils.sp2px(mContext, 18));
dayTextPaint.setColor(Color.parseColor("#ffffff"));
space = DisplayUtils.dip2px(2);
mDotPaint.setMaskFilter(new BlurMaskFilter(6, BlurMaskFilter.Blur.SOLID));
mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
}
@Override
protected void onDrawTitleItem(Canvas canvas, int left, int top, int right, int bottom, int pos) {
ITimeItem item = timeItems.get(pos);
int rectWidth = DisplayUtils.dip2px(20);
int mid = (bottom + top) / 2;
String title = item.getTitle();
if (TextUtils.isEmpty(title))
return;
Rect mRect = new Rect();
mTextPaint.getTextBounds(title, 0, title.length(), mRect);
int x = left + rectWidth;
//int x = left + UIUtils.dip2px(20);
int y = mid + mRect.height() / 2;
canvas.drawText(title, x, y, mTextPaint);
}
@Override
protected void onDrawDotItem(Canvas canvas, int cx, int cy, int radius, int pos) {
super.onDrawDotItem(canvas, cx, cy, radius, pos);
DateInfo timeItem = (DateInfo) timeItems.get(pos);
Date date = timeItem.getDate();
mDotPaint.setColor(timeItem.getColor());
canvas.drawRoundRect(cx - r, cy - r, cx + r, cy + r
, DisplayUtils.dip2px(2),DisplayUtils.dip2px(2),mDotPaint);
if (date != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String mon = MONTHS[calendar.get(Calendar.MONTH)];
int n = calendar.get(Calendar.DAY_OF_MONTH);
String day = n < 10 ? "0" + n : Integer.toString(n);
Rect monRect = new Rect();
monTextPaint.getTextBounds(mon, 0, mon.length(), monRect);
Rect dayRect = new Rect();
dayTextPaint.getTextBounds(day, 0, day.length(), dayRect);
int monWidth = monRect.width();
int dayWidth = dayRect.width();
int dayHeight = dayRect.height();
int beginY = cy + r - (r * 2 -dayHeight) / 2;
int beginX = cx - r +(r *2 - monWidth -space - dayWidth)/2;
canvas.drawText(day, beginX, beginY, dayTextPaint);
beginX += space+dayWidth;
canvas.drawText(mon, beginX, beginY, monTextPaint);
}
}
}
第二步: 创建数据,实现 ITimeItem 接口
/**
* Created on 2021/7/16 16:18
*
* @author Gong Youqiang
*/
public class SocialMediaInfo extends DateInfo {
private int res;
public SocialMediaInfo(String name, String detail, Date date, int color,int res) {
super(name, detail, date, color);
this.res = res;
}
@Override
public String getTitle() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getDate());
return Integer.toString(calendar.get(Calendar.YEAR));
}
public int getRes() {
return res;
}
public void setRes(int res) {
this.res = res;
}
public static List<SocialMediaInfo> initSocialMediaInfo(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
List<SocialMediaInfo> items = new ArrayList<>();
for (int i = 0; i < 2; i++) {
items.add(new SocialMediaInfo("傍晚的太湖", "", calendar.getTime(), Color.parseColor("#f36c60"), R.mipmap.image2));
calendar.add(Calendar.DAY_OF_MONTH, -1);
items.add(new SocialMediaInfo("最近真的是种了简书和掘金的毒,偷偷学习了它们的风格", "共2张"
, calendar.getTime(), Color.parseColor("#ab47bc"), R.mipmap.image1));
calendar.add(Calendar.DAY_OF_MONTH, -1);
items.add(new SocialMediaInfo("出差啦~", "共3张", calendar.getTime(), Color.parseColor("#aed581"), R.mipmap.image3));
calendar.add(Calendar.YEAR, -1);
calendar.add(Calendar.DAY_OF_MONTH, -1);
items.add(new SocialMediaInfo("Hey,故宫", "", calendar.getTime(), Color.parseColor("#5FB29F"), R.mipmap.image1));
calendar.add(Calendar.DAY_OF_MONTH, -1);
items.add(new SocialMediaInfo("吃个布丁", "", calendar.getTime(), Color.parseColor("#ec407a"), R.mipmap.image4));
calendar.add(Calendar.DAY_OF_MONTH, -1);
items.add(new SocialMediaInfo("夏天到啦啦啦,2333333", "共2张", calendar.getTime(), Color.parseColor("#0D47A1"), R.mipmap.image1));
}
return items;
}
}
第三步:创建适配器
/**
* Created on 2021/7/16 15:25
*
* @author Gong Youqiang
*/
public abstract class RecyclerAdapter<Data> extends BaseAdapter<Data> {
public RecyclerAdapter() {
}
public RecyclerAdapter(AdapterListener<Data> adapterListener) {
super(adapterListener);
}
public RecyclerAdapter(List<Data> mDataList, AdapterListener<Data> adapterListener) {
super(mDataList, adapterListener);
}
@Override
protected void doWithRoot(BaseAdapter.ViewHolder viewHolder, View root) {
super.doWithRoot(viewHolder, root);
((RecyclerAdapter.ViewHolder)viewHolder).unbinder = ButterKnife.bind(viewHolder,root);
}
/**
* 自定义的ViewHolder
*/
public static abstract class ViewHolder<Data> extends BaseAdapter.ViewHolder<Data>{
public Unbinder unbinder;
public ViewHolder(View itemView) {
super(itemView);
}
}
}
第四步:创建时间轴
public class SingleInfoActivity extends BaseActivity {
@BindView(R.id.rv_content)
RecyclerView mRecyclerView;
private RecyclerAdapter<SocialMediaInfo> mAdapter;
@Override
public int getLayoutId() {
return R.layout.activity_single_info;
}
@Override
public void initView() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter = new RecyclerAdapter<SocialMediaInfo>() {
@Override
public ViewHolder<SocialMediaInfo> onCreateViewHolder(View root, int viewType) {
return new DateInfoHolder(root);
}
@Override
public int getItemLayout(SocialMediaInfo s, int position) {
return R.layout.social_meida_recycle_item;
}
});
List<SocialMediaInfo> timeItems = SocialMediaInfo.initSocialMediaInfo();
mAdapter.addAllData(timeItems);
TimeLine timeLine = provideTimeLine(timeItems);
mRecyclerView.addItemDecoration(timeLine);
}
private TimeLine provideTimeLine(List<SocialMediaInfo> timeItems) {
return new TimeLine.Builder(this, timeItems)
.setTitleStyle(TimeLine.FLAG_TITLE_TYPE_TOP, 52)
.setTitle(Color.parseColor("#000000"), 22)
.setLine(TimeLine.FLAG_LINE_DIVIDE, 80, Color.parseColor("#757575"), 1)
.setDot(TimeLine.FLAG_DOT_DRAW)
.setSameTitleHide()
.build(SocialMediaSTL.class);
}
class DateInfoHolder extends RecyclerAdapter.ViewHolder<SocialMediaInfo> {
@BindView(R.id.tv_name)
TextView mNameTv;
@BindView(R.id.tv_detail)
TextView mDetailTv;
@BindView(R.id.iv_content)
ImageView mContentIv;
DateInfoHolder(View itemView) {
super(itemView);
}
@Override
protected void onBind(SocialMediaInfo timeItem) {
mNameTv.setText(timeItem.getName());
mDetailTv.setText(timeItem.getDetail());
mContentIv.setImageResource(timeItem.getRes());
}
}
}
网友评论