Android自定義View實(shí)現(xiàn)折線圖效果
下面就是結(jié)果圖(每種狀態(tài)用一個(gè)表情圖片表示):
一、主頁面的布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/res/ting.example.linecharview"> <ting.example.linecharview.LineCharView android:id="@+id/test" android:layout_width="match_parent" android:layout_height="match_parent" app:xytextcolor="@color/bg" app:xytextsize="20sp" app:interval="80dp" /> </RelativeLayout>
其中linecharview
就是自定義的View
,而app:xx
就是這個(gè)View
的各種屬性。
二、在values
的attrs文件中加入如下xml,來定義linecharview
的各種屬性:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="LineChar"> <attr name="xylinecolor" format="color"/><!-- xy坐標(biāo)軸顏色 --> <attr name="xylinewidth" format="dimension"/><!-- xy坐標(biāo)軸寬度 --> <attr name="xytextcolor" format="color"/><!-- xy坐標(biāo)軸文字顏色 --> <attr name="xytextsize" format="dimension"/><!-- xy坐標(biāo)軸文字大小 --> <attr name="linecolor" format="color"/><!-- 折線圖中折線的顏色 --> <attr name="interval" format="dimension"/><!-- x軸各個(gè)坐標(biāo)點(diǎn)水平間距 --> <attr name="bgcolor" format="color"/><!-- 背景顏色 --> </declare-styleable> </resources>
三、接下來建個(gè)類LineCharView
繼承View
,并申明如下變量:
<span style="white-space:pre"> </span>private int xori;//圓點(diǎn)x坐標(biāo) private int yori;//圓點(diǎn)y坐標(biāo) private int xinit;//第一個(gè)點(diǎn)x坐標(biāo) private int minXinit;//在移動(dòng)時(shí),第一個(gè)點(diǎn)允許最小的x坐標(biāo) private int maxXinit;//在移動(dòng)時(shí),第一個(gè)點(diǎn)允許允許最大的x坐標(biāo) private int xylinecolor;//xy坐標(biāo)軸顏色 private int xylinewidth;//xy坐標(biāo)軸大小 private int xytextcolor;//xy坐標(biāo)軸文字顏色 private int xytextsize;//xy坐標(biāo)軸文字大小 private int linecolor;//折線的顏色 private int interval;//坐標(biāo)間的間隔 private int bgColor;//背景顏色 private List<String> x_coords;//x坐標(biāo)點(diǎn)的值 private List<String> x_coord_values;//每個(gè)點(diǎn)狀態(tài)值 private int width;//控件寬度 private int heigth;//控件高度 private int imageWidth;//表情的寬度 private float textwidth;//y軸文字的寬度 float startX=0;//滑動(dòng)時(shí)候,上一次手指的x坐標(biāo)
在構(gòu)造函數(shù)中讀取各個(gè)屬性值:
public LineCharView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray= context.obtainStyledAttributes(attrs, R.styleable.LineChar); xylinecolor=typedArray.getColor(R.styleable.LineChar_xylinecolor, Color.GRAY); xylinewidth=typedArray.getInt(R.styleable.LineChar_xylinewidth, 5); xytextcolor=typedArray.getColor(R.styleable.LineChar_xytextcolor, Color.BLACK); xytextsize=typedArray.getLayoutDimension(R.styleable.LineChar_xytextsize, 20); linecolor=typedArray.getColor(R.styleable.LineChar_linecolor, Color.GRAY); interval=typedArray.getLayoutDimension(R.styleable.LineChar_interval, 100); bgColor=typedArray.getColor(R.styleable.LineChar_bgcolor, Color.WHITE); typedArray.recycle(); x_coords=new ArrayList<String>(); x_coord_values=new ArrayList<String>(); }
四、接下來可以重寫onLayout
方法,來計(jì)算控件寬高和坐標(biāo)軸的原點(diǎn)坐標(biāo),坐標(biāo)軸原點(diǎn)的x坐標(biāo)可以通過y軸文字的寬度,y軸寬度,和距離y的水平距離進(jìn)行計(jì)算,這里y軸文字只有4種狀態(tài)(A、B、C、D),可以使用下面方法來計(jì)算出原點(diǎn)的x坐標(biāo):
Paint paint=new Paint(); paint.setTextSize(xytextsize); textwidth= paint.measureText("A"); xori=(int) (textwidth+6+2*xylinewidth);//6 為與y軸的間隔
原點(diǎn)的y坐標(biāo)也可以用類似的方法計(jì)算出來:
yori=heigth-xytextsize-2*xylinewidth-3; //3為x軸的間隔,heigth為控件高度。
當(dāng)需要展示的數(shù)據(jù)量多時(shí)候,無法全部展示時(shí)候,需要通過滑動(dòng)折線圖進(jìn)行展示,我們只需要控制第一點(diǎn)x坐標(biāo),就可以通過interval這個(gè)屬性計(jì)算出后面每個(gè)點(diǎn)的坐標(biāo),但是為了防止將所有的數(shù)據(jù)滑動(dòng)出界面外,需要在滑動(dòng)時(shí)進(jìn)行控制,其實(shí)就是控制第一個(gè)點(diǎn)x坐標(biāo)的范圍,第一個(gè)點(diǎn)的x坐標(biāo)的最小值可以通過控件的寬度減去原點(diǎn)x坐標(biāo)再減去所有折線圖的水平距離,代碼如下:
minXinit=width-xori-x_coords.size()*interval;
控件在默認(rèn)第一個(gè)展示時(shí),第一個(gè)點(diǎn)與y軸的水平距離等于interval
的一半,在滑動(dòng)時(shí)候如果第一個(gè)點(diǎn)出現(xiàn)在這個(gè)位置了,就不允許再繼續(xù)向右滑動(dòng),所以第一個(gè)點(diǎn)x坐標(biāo)的最大值就等這個(gè)起始x坐標(biāo)。
xinit=interval/2+xori; maxXinit=xinit;
重寫onLayout
方法的代碼如下:
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { if(changed){ width=getWidth(); heigth=getHeight(); Paint paint=new Paint(); paint.setTextSize(xytextsize); textwidth= paint.measureText("A"); xori=(int) (textwidth+6+2*xylinewidth);//6 為與y軸的間隔 yori=heigth-xytextsize-2*xylinewidth-3;//3為x軸的間隔 xinit=interval/2+xori; imageWidth= BitmapFactory.decodeResource(getResources(), R.drawable.facea).getWidth(); minXinit=width-xori-x_coords.size()*interval; maxXinit=xinit; setBackgroundColor(bgColor); } super.onLayout(changed, left, top, right, bottom); }
五、接下來就可以畫折線、x坐標(biāo)軸上的小圓點(diǎn)和折線上表情
代碼如下:
//畫X軸坐標(biāo)點(diǎn),折線,表情 @SuppressLint("ResourceAsColor") private void drawX (Canvas canvas) { Paint x_coordPaint =new Paint(); x_coordPaint.setTextSize(xytextsize); x_coordPaint.setStyle(Paint.Style.FILL); Path path=new Path(); //畫坐標(biāo)軸上小原點(diǎn),坐標(biāo)軸文字 for(int i=0;i<x_coords.size();i++){ int x=i*interval+xinit; if(i==0){ path.moveTo(x, getYValue(x_coord_values.get(i))); }else{ path.lineTo(x, getYValue(x_coord_values.get(i))); } x_coordPaint.setColor(xylinecolor); canvas.drawCircle(x, yori, xylinewidth*2, x_coordPaint); String text=x_coords.get(i); x_coordPaint.setColor(xytextcolor); canvas.drawText(text, x-x_coordPaint.measureText(text)/2, yori+xytextsize+xylinewidth*2, x_coordPaint); } x_coordPaint.setStyle(Paint.Style.STROKE); x_coordPaint.setStrokeWidth(xylinewidth); x_coordPaint.setColor(linecolor); //畫折線 canvas.drawPath(path, x_coordPaint); //畫表情 for(int i=0;i<x_coords.size();i++){ int x=i*interval+xinit; canvas.drawBitmap(getYBitmap(x_coord_values.get(i)), x-imageWidth/2, getYValue(x_coord_values.get(i))-imageWidth/2, x_coordPaint); } //將折線超出x軸坐標(biāo)的部分截取掉 x_coordPaint.setStyle(Paint.Style.FILL); x_coordPaint.setColor(bgColor); x_coordPaint.setXfermode(new PorterDuffXfermode( PorterDuff.Mode.SRC_OVER)); RectF rectF=new RectF(0, 0, xori, heigth); canvas.drawRect(rectF, x_coordPaint); }
以上代碼首先通過遍歷x_coords
和x_coord_values
這兩個(gè)List集合,來畫坐標(biāo)點(diǎn),折線,表情,由于在向左滑動(dòng)的時(shí)候有可能會(huì)將坐標(biāo)點(diǎn),折線繪制到y(tǒng)軸的左邊,所以需要對(duì)其進(jìn)行截取。其中getYValue
和getYBitmap方法,可以通過x_coord_values
的值計(jì)算y坐標(biāo)和相應(yīng)的表情。兩方法如:
//得到y(tǒng)坐標(biāo) private float getYValue(String value) { if(value.equalsIgnoreCase("A")){ return yori-interval/2; } else if(value.equalsIgnoreCase("B")){ return yori-interval; } else if(value.equalsIgnoreCase("C")){ return (float) (yori-interval*1.5); } else if(value.equalsIgnoreCase("D")){ return yori-interval*2; }else{ return yori; } } //得到表情圖 private Bitmap getYBitmap(String value){ Bitmap bitmap=null; if(value.equalsIgnoreCase("A")){ bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.facea); } else if(value.equalsIgnoreCase("B")){ bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.faceb); } else if(value.equalsIgnoreCase("C")){ bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.facec); } else if(value.equalsIgnoreCase("D")){ bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.faced); } return bitmap; }
六、畫好了坐標(biāo)點(diǎn),折線,表情,接下來就簡(jiǎn)單,就可以畫x y軸了,x y軸只要確定的原點(diǎn)坐標(biāo),就非常簡(jiǎn)單了,代碼如下:
//畫坐標(biāo)軸 private void drawXY(Canvas canvas){ Paint paint=new Paint(); paint.setColor(xylinecolor); paint.setStrokeWidth(xylinewidth); canvas.drawLine(xori, 0, xori, yori, paint); canvas.drawLine(xori, yori, width, yori, paint); }
七、最后就可以畫y軸上的坐標(biāo)小原點(diǎn)和y軸的文字了:
//畫Y軸坐標(biāo)點(diǎn) private void drawY(Canvas canvas){ Paint paint=new Paint(); paint.setColor(xylinecolor); paint.setStyle(Paint.Style.FILL); for(int i=1;i<5 ;i++){ canvas.drawCircle(xori, yori-(i*interval/2), xylinewidth*2, paint); } paint.setTextSize(xytextsize); paint.setColor(xytextcolor); canvas.drawText("D",xori-textwidth-6-xylinewidth , yori-(2*interval)+xytextsize/2, paint); canvas.drawText("C",xori-textwidth-6-xylinewidth , (float) (yori-(1.5*interval)+xytextsize/2), paint); canvas.drawText("B",xori-textwidth-6-xylinewidth , yori-interval+xytextsize/2, paint); canvas.drawText("A",xori-textwidth-6-xylinewidth , (float) (yori-(0.5*interval)+xytextsize/2), paint); }
八、寫完了以上三個(gè)方法:只需要重寫onDraw
方法,就可以進(jìn)行繪制了。
@Override protected void onDraw(Canvas canvas) { drawX(canvas); drawXY(canvas); drawY(canvas); }
九、為了可以進(jìn)行水平滑動(dòng),需要重寫控件的onTouchEvent方法,在滑動(dòng)時(shí)候,實(shí)時(shí)計(jì)算手指滑動(dòng)的距離來改變第一個(gè)點(diǎn)的x坐標(biāo),然后調(diào)用invalidate();
就可以刷新控件,重新繪制達(dá)到滑動(dòng)效果。
@Override public boolean onTouchEvent(MotionEvent event) { //如果不用滑動(dòng)就可以展示所有數(shù)據(jù),就不讓滑動(dòng) if(interval*x_coord_values.size()<=width-xori){ return false; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX=event.getX(); break; case MotionEvent.ACTION_MOVE: float dis=event.getX()-startX; startX=event.getX(); if(xinit+dis>maxXinit){ xinit=maxXinit; }else if(xinit+dis<minXinit){ xinit=minXinit; }else{ xinit=(int) (xinit+dis); } invalidate(); break; } return true; }
十、最后添加一個(gè)設(shè)置數(shù)據(jù)源的方法,設(shè)置x_coords
,x_coord_values
這個(gè)兩個(gè)List
集合,在設(shè)置完成之后調(diào)用invalidate()
,進(jìn)行控件刷新:
/** * 設(shè)置坐標(biāo)折線圖值 * @param x_coords 橫坐標(biāo)坐標(biāo)點(diǎn) * @param x_coord_values 每個(gè)點(diǎn)的值 */ public void setValue( List<String> x_coords ,List<String> x_coord_values) { if(x_coord_values.size()!=x_coords.size()){ throw new IllegalArgumentException("坐標(biāo)軸點(diǎn)和坐標(biāo)軸點(diǎn)的值的個(gè)數(shù)必須一樣!"); } this.x_coord_values=x_coord_values; this.x_coords=x_coords; invalidate(); }
總結(jié)
以上就是Android自定義View實(shí)現(xiàn)折線圖效果的全部?jī)?nèi)容,希望對(duì)大家開發(fā)Android能有所幫助。
相關(guān)文章
Android虛擬導(dǎo)航欄遮擋底部的輸入框的解決方法
下面小編就為大家分享一篇Android虛擬導(dǎo)航欄遮擋底部的輸入框的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android仿微信雷達(dá)輻射搜索好友(邏輯清晰實(shí)現(xiàn)簡(jiǎn)單)
仿微信雷達(dá)掃描,仿安卓微信、云播雷達(dá)掃描動(dòng)畫效果點(diǎn)擊中間的黑色圓圈開始掃描動(dòng)畫,再次點(diǎn)擊復(fù)位,需要這種效果的朋友可以自己下載看一下2016-02-02Android入門之使用SimpleAdapter實(shí)現(xiàn)復(fù)雜界面布局
這篇文章主要為大家詳細(xì)介紹了Android如何使用SimpleAdapter實(shí)現(xiàn)復(fù)雜的界面布局,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-11-11Android 第三方應(yīng)用接入微信平臺(tái)研究情況分享(二)
微信平臺(tái)開放后倒是挺火的,許多第三方應(yīng)用都想試下,這里把我的整個(gè)研究情況給出來,希望可以共同學(xué)習(xí),感興趣的朋友可以了解下2013-01-01Android Activity啟動(dòng)模式之singleTask實(shí)例詳解
這篇文章主要介紹了Android Activity啟動(dòng)模式之singleTask,結(jié)合實(shí)例形式較為詳細(xì)的分析了singleTask模式的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-01-01Flutter實(shí)現(xiàn)底部菜單導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)底部菜單導(dǎo)航,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02Android Parcleable接口的調(diào)用源碼層分析
這篇文章主要給大家介紹了關(guān)于利用Kotlin如何實(shí)現(xiàn)Android開發(fā)中的Parcelable的相關(guān)資料,并且給大家介紹了關(guān)于Android Parcleable源碼層問題,需要的朋友可以參考下2022-12-12Android簡(jiǎn)單實(shí)現(xiàn)菜單拖拽排序的功能
這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn)菜單拖拽排序的功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)價(jià)值,需要的朋友可以參考一下2022-07-07