您当前的位置: 首页 >  android

Kevin-Dev

暂无认证

  • 2浏览

    0关注

    544博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android 自定义 View】-->单列时间轴(二)

Kevin-Dev 发布时间:2020-01-10 22:41:14 ,浏览量:2

不断学习,做更好的自己!💪

视频号CSDN简书欢迎打开微信,关注我的视频号:KevinDev点我点我 实例一

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 initTimeInfo(){
        List 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 extends BaseAdapter {

    public RecyclerAdapter() {
    }

    public RecyclerAdapter(AdapterListener adapterListener) {
        super(adapterListener);
    }

    public RecyclerAdapter(List mDataList, AdapterListener 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 extends BaseAdapter.ViewHolder{
        public Unbinder unbinder;

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

}

第四步:创建时间轴

public class SingleTimeLineActivity extends BaseActivity {
    @BindView(R.id.rv_content)
    RecyclerView mRecyclerView;

    private RecyclerAdapter 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() {
            @Override
            public ViewHolder 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 timeItems = TimeItem.initTimeInfo();
        mAdapter.addAllData(timeItems);

        TimeLine timeLine = provideTimeLine(timeItems);
        mRecyclerView.addItemDecoration(timeLine);
    }



    private TimeLine provideTimeLine(List 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 {

        @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)            
关注
打赏
1658837700
查看更多评论
0.0460s