欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義View實(shí)現(xiàn)折線圖效果

 更新時(shí)間:2016年08月24日 15:40:10   投稿:daisy  
這篇文章介紹的是一個(gè)折線圖控件,用來顯示一系列的狀態(tài),并可以進(jìn)行滑動(dòng)。有需要的可以參考借鑒。

下面就是結(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_coordsx_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_coordsx_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)文章

最新評(píng)論