android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(2)
這篇做了一個(gè)簡(jiǎn)單的時(shí)間軸控件。右側(cè)的數(shù)據(jù)就是一個(gè)簡(jiǎn)單的字符串。問題還是有的,當(dāng)右側(cè)的文字長度不一樣的時(shí)候就會(huì)有問題了?,F(xiàn)在可以修改一下適配右側(cè)的文字。
效果如下:
代碼:
private Paint bgPaint, linePaint, borderPaint,textPaint; private Rect bgRect, textRect; //基本屬性 private int mTextSize; private int mTextColor; private String mTextTitle="默認(rèn)文本內(nèi)容"; private int lineColr = Color.parseColor("#AAAAAA"); private int borderColor = Color.parseColor("#AAAAAA"); private int bgColor = Color.parseColor("#138DDD"); private int mBorderColor=0xFFDDDDDD; private int mBorderWidth = 10; private int mLineColor=Color.parseColor("#ff000000"); private int mLineWidth = 2; private int mLineHeight; private int mBgColor; private ?int mWidth =0; private ?int mHeight =300;//整個(gè)控件的寬和高 //line繪制 private int lineLocation = -1;//0 ?上方 1 ?下方 2 ?上下兩個(gè) private int mRadius = 90;//直徑,最終會(huì)被寬高限制 //設(shè)置line的位置 0 ?上方 1 ?下方 2 ?上下兩個(gè) public void setLineLocation(int lineLocation) { ? ? ? ? this.lineLocation = lineLocation; ? ? } //設(shè)置純色的整圓形,包括背景 public void setBgAndBorderColor(int color) { ? ? ? ? this.mBgColor = color; ? ? } ? ? public void setmHeight(int mHeight) { ? ? ? ? this.mHeight = mHeight; ? ? } ? ? public void setmBorderColor(int mBorderColor) { ? ? ? ? this.mBorderColor = mBorderColor; ? ? } ? ? public void setmTextTitle(String mTextTitle) { ? ? ? ? this.mTextTitle = mTextTitle; ? ? } ? ? public void setmRadius(int mRadius) { ? ? ? ? this.mRadius = mRadius; ? ? } ? ? public void setmLineHeight(int mLineHeight) { ? ? ? ? this.mLineHeight = mLineHeight; ? ? } ? ? public TimeLineSingleView(Context context) { ? ? ? ? this(context,null); ? ? } ? ? public TimeLineSingleView(Context context, AttributeSet attrs) { ? ? ? ? this(context, attrs,0); ? ? } ? ? public TimeLineSingleView(Context context, AttributeSet attrs, int defStyleAttr) { ? ? ? ? super(context, attrs, defStyleAttr); ? ? ? ? TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCicleView, defStyleAttr, 0); ? ? ? ? int n = a.getIndexCount(); ? ? ? ? for (int i = 0; i < n; i++) { ? ? ? ? ? ? int attr = a.getIndex(i); ? ? ? ? ? ? switch (attr) { ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_textSize: ? ? ? ? ? ? ? ? ? ? // 默認(rèn)設(shè)置為16sp,TypeValue也可以把sp轉(zhuǎn)化為px ? ? ? ? ? ? ? ? ? ? mTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( ? ? ? ? ? ? ? ? ? ? ? ? ? ? TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics())); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_textColor: ? ? ? ? ? ? ? ? ? ? mTextColor = a.getColor(attr, Color.BLACK); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_textTitle: ? ? ? ? ? ? ? ? ? ? mTextTitle = a.getString(attr); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_lineWidth: ? ? ? ? ? ? ? ? ? ? mLineWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( ? ? ? ? ? ? ? ? ? ? ? ? ? ? TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics())); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_lineColor: ? ? ? ? ? ? ? ? ? ? mLineColor = a.getColor(attr, lineColr); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_mRadius: ? ? ? ? ? ? ? ? ? ? mRadius=a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( ? ? ? ? ? ? ? ? ? ? ? ? ? ? TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics())); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_borderWidth: ? ? ? ? ? ? ? ? ? ? mBorderWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( ? ? ? ? ? ? ? ? ? ? ? ? ? ? TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics())); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_borderColor: ? ? ? ? ? ? ? ? ? ? mBorderColor = a.getColor(attr, borderColor); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case R.styleable.CustomCicleView_bgColor: ? ? ? ? ? ? ? ? ? ? mBgColor = a.getColor(attr, bgColor); ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? a.recycle(); ? ? ? ? bgPaint = new Paint(); ? ? ? ? borderPaint = new Paint(); ? ? ? ? linePaint = new Paint(); ? ? ? ? textPaint = new Paint(); ? ? ? ? textRect = new Rect(); ? ? ? ? textPaint.setTextSize(mTextSize); ? ? } ? ? //EXACTLY :在準(zhǔn)確的數(shù)值和Match_parent的狀態(tài)是這個(gè)值 列表中,wrap_content的狀態(tài)下必須計(jì)算一個(gè)合適的值 ? ? @Override ? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec); ? ? ? ? int w = 0; ? ? ? ? int h =0; ? ? ? ? int widthMode=MeasureSpec.getMode(widthMeasureSpec); ? ? ? ? int heightMode=MeasureSpec.getMode(heightMeasureSpec); ? ? ? ? int widthSize = MeasureSpec.getSize(widthMeasureSpec); ? ? ? ? int heightSize = MeasureSpec.getSize(heightMeasureSpec); ? ? ? ? if(widthMode==MeasureSpec.EXACTLY){ ? ? ? ? ? ? w=widthSize; ? ? ? ? }else{ ? ? ? ? ? ? w=Math.max(mHeight,mRadius+getPaddingRight()+getPaddingLeft()); ? ? ? ? } ? ? ? ? if(heightMode==MeasureSpec.EXACTLY){ ? ? ? ? ? ? h=heightSize; ? ? ? ? }else{ ? ? ? ? ? ? h=Math.max(mHeight,mRadius+getPaddingTop()+getPaddingBottom()); ? ? ? ? } ? ? ? ? setMeasuredDimension(w,h); ? ? ? ? } ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? int centreX = getWidth()/ 2; // 獲取圓心的x坐標(biāo) ? ? ? ? int centreY=getHeight()/2; ? ? ? ? //半徑比較 ? ? ? ? mBorderWidth =(mBorderWidth>=mRadius/10)?(mRadius/10):mBorderWidth;//半徑的1/5 ? ? ? ? int radius = mRadius/2 - mBorderWidth / 2;// 半徑 ? ? ? ? if(mLineHeight<=0){ ? ? ? ? ? ? mLineHeight=Math.abs(getHeight()/2-radius);//這個(gè)地方要判斷設(shè)置正負(fù) ? ? ? ? } ? ? ? ? //繪制圓 ? ? ? ? bgPaint.setAntiAlias(true); // 消除鋸齒 ? ? ? ? bgPaint.setColor(mBgColor); ? ? ? ? bgPaint.setStyle(Paint.Style.FILL); // 設(shè)置實(shí)心 ? ? ? ? canvas.drawCircle(centreX, centreY, radius, bgPaint); ? ? ? ? //繪制圓環(huán) ? ? ? ? borderPaint.setStrokeWidth(mBorderWidth); // 設(shè)置圓環(huán)的寬度 ? ? ? ? borderPaint.setAntiAlias(true); // 消除鋸齒 ? ? ? ? if (mBorderColor != 0) { ? ? ? ? ? ? borderPaint.setColor(mBorderColor); ? ? ? ? } else { ? ? ? ? ? ? borderPaint.setColor(borderColor); ? ? ? ? } ? ? ? ? borderPaint.setStyle(Paint.Style.STROKE); // 設(shè)置實(shí)心 ? ? ? ? canvas.drawCircle(centreX,centreY, radius - mBorderWidth / 2+mLineWidth/2, borderPaint); ? ? ? ? //繪制文本 ? ? ? ? textPaint.setTextSize(mTextSize); ? ? ? ? textPaint.setColor(mTextColor); ? ? ? ? textPaint.getTextBounds(mTextTitle, 0, mTextTitle.length(), textRect); ? ? ? ? canvas.drawText(mTextTitle, centreX -textRect.width()/2-mBorderWidth/2, centreY ?+ textRect.height() / 2, textPaint); ? ? ? ? //繪制線條 ? ? ? ? drawLineAll(canvas,centreX,centreY,radius); ? ? } ? ? //上下都繪制不用 ? ? //1 ?上方 0 ?下方 2 ?上下兩個(gè) ? ? private void drawLineAll(Canvas canvas, ?float centreX, float centreY,int radius) { ? ? ? ? if(lineLocation==-1){ ? ? ? ? ? ? linePaint.setColor(borderColor); ? ? ? ? ? ? linePaint.setStrokeWidth(mLineWidth); ? ? ? ? ? ? canvas.drawLine(centreX, 0, centreX, centreY-radius, linePaint);//上方的 ? ? ? ? ? ? canvas.drawLine(centreX, centreY+radius, centreX, getHeight(), linePaint);//下方的 ? ? ? ? }else{ ? ? ? ? ? ? //這個(gè)可以繪制不同的line ? ? ? ? ? ? linePaint.setColor(lineColr); ? ? ? ? ? ? linePaint.setStrokeWidth(mLineWidth); ? ? ? ? ? ? if (lineLocation == 0) { ? ? ? ? ? ? ? ? canvas.drawLine(centreX, centreY+radius, centreX, getHeight(), linePaint); ? ? ? ? ? ? } else if (lineLocation == 1) { ? ? ? ? ? ? ? ? canvas.drawLine(centreX, 0, centreX, centreY -radius , linePaint); ? ? ? ? ? ? } else if (lineLocation == 2) { ? ? ? ? ? ? ? ? canvas.drawLine(centreX, centreY+radius, centreX, getHeight(), linePaint); ? ? ? ? ? ? ? ? canvas.drawLine(centreX, 0, centreX, centreY -radius , linePaint); ? ? ? ? ? ? } ? ? ? ? } ? ? }
其他代碼和之前文章一樣就不貼了,但是還有一個(gè)問題就是,這個(gè)控件是放在一個(gè)列表里面的,你在適配器中使用的時(shí)候布局要是wrap的狀態(tài)下要計(jì)算一個(gè)合適的高度,比如listView 的Item的高度。這里我沒有實(shí)現(xiàn),還是在Match和固定高度中,后期更改。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android TextView和ImageView簡(jiǎn)單說明
Android TextView和ImageView簡(jiǎn)單說明,需要的朋友可以參考一下2013-03-03Android基于OpenCV實(shí)現(xiàn)霍夫直線檢測(cè)
霍夫變換利用點(diǎn)與線之間的對(duì)偶性,將圖像空間中直線上離散的像素點(diǎn)通過參數(shù)方程映射為霍夫空間中的曲線,并將霍夫空間中多條曲線的交點(diǎn)作為直線方程的參數(shù)映射為圖像空間中的直線。給定直線的參數(shù)方程,可以利用霍夫變換來檢測(cè)圖像中的直線。本文簡(jiǎn)單講解Android的實(shí)現(xiàn)2021-06-06Kotlin協(xié)程操作之創(chuàng)建啟動(dòng)掛起恢復(fù)詳解
本文的定位是協(xié)程的創(chuàng)建、啟動(dòng)、掛起、恢復(fù),也會(huì)示例一些簡(jiǎn)單的使用,這里不對(duì)suspend講解,,也不對(duì)協(xié)程的高級(jí)用法做闡述(熱數(shù)據(jù)通道Channel、冷數(shù)據(jù)流Flow...),本文主要講協(xié)程稍微深入的全面知識(shí)2022-08-08Android Loader的使用以及手機(jī)通訊錄的獲取方法
下面小編就為大家分享一篇Android Loader的使用以及手機(jī)通訊錄的獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android編程實(shí)現(xiàn)音量按鈕添加監(jiān)聽事件的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)音量按鈕添加監(jiān)聽事件的方法,結(jié)合實(shí)例形式分析了Android事件監(jiān)聽實(shí)現(xiàn)音量控制的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06Android編程之滑動(dòng)按鈕事件實(shí)例詳解
這篇文章主要介紹了Android編程之滑動(dòng)按鈕事件,結(jié)合具體實(shí)例形式分析了Android滑動(dòng)按鈕功能的具體實(shí)現(xiàn)步驟、布局及功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2017-03-03Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能
這篇文章主要介紹了Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01