Android Custom View: Line chart (with animation effect)

Source: Internet
Author: User

Package com.chk.uefa.thirdviews;

Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.os.AsyncTask;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import Android.view.View;

Import Com.lidroid.xutils.util.LogUtils;

/**
* Created by Yulook on 2016/4/7.
*/
public class Brokenlineview extends View {

Private String Price1,price2,price3,price4,price5,price6,price7;
Private String Maxprice,minprice;
private int wide=0,high=0;

Public Brokenlineview (Context context) {
Super (context);
}

Public Brokenlineview (context context, AttributeSet Attrs) {
Super (context, attrs);
}

Public Brokenlineview (context context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);
}

/** seven price coordinate points */
public void Setsevenprice (String price1,string price2,string price3,string price4,string price5,string price6,String Price7) {
This.price1=price1;
This.price2=price2;
This.price3=price3;
This.price4=price4;
This.price5=price5;
This.price6=price6;
This.price7=price7;
}

/**y Axis Maximum Minimum value */
public void Setmaxminprice (String maxprice,string minprice) {
This.maxprice=maxprice;
This.minprice=minprice;
}

/** Get Wide Height */
public void Setwidehigh (Int. Wide,int High) {
This.wide=wide;
This.high=high;

}

@Override
protected void OnDraw (canvas canvas) {
Super.ondraw (canvas);

Paint p = new paint ();
P.setstrokewidth ((float) 2.0);
P.setantialias (TRUE);//Set the brush's jagged effect
P.setcolor (Getresources (). GetColor (r.color.red));

if (wide>0) {

if (time<=5&&time>0) {

Double s_y = Getroundy (Price1);
Double e_y = Getroundy (PRICE2);
Double a= (s_y-e_y)/5;
Double b=s_y-a*time;
Canvas.drawline (0, (float) getroundy (Price1), Wide/30*time, (float) b, p);

}else if (time>5&&time<=10) {

Double s_y = Getroundy (PRICE2);
Double e_y = Getroundy (PRICE3);
Double a= (s_y-e_y)/5;
Double b=s_y-a* (time-5);

Canvas.drawline (0, (float) getroundy (Price1), WIDE/6, (float) getroundy (PRICE2), p);
Canvas.drawline (WIDE/6, (float) getroundy (PRICE2), wide/6+ (WIDE/6) * (time-5)/5, (float) b, p);
}else if (time>10&&time<=15) {

Double s_y = Getroundy (PRICE3);
Double e_y = Getroundy (Price4);
Double a= (s_y-e_y)/5;
Double b=s_y-a* (TIME-10);

Canvas.drawline (0, (float) getroundy (Price1), WIDE/6, (float) getroundy (PRICE2), p);
Canvas.drawline (WIDE/6, (float) getroundy (price2), WIDE/3, (float) getroundy (price3), p);
Canvas.drawline (WIDE/3, (float) getroundy (price3), wide/3+ (WIDE/6) * (TIME-10)/5, (float) b, p);
}else if (time>15&&time<=20) {

Double s_y = Getroundy (Price4);
Double e_y = Getroundy (Price5);
Double a= (s_y-e_y)/5;
Double b=s_y-a* (time-15);

Canvas.drawline (0, (float) getroundy (Price1), WIDE/6, (float) getroundy (PRICE2), p);
Canvas.drawline (WIDE/6, (float) getroundy (price2), WIDE/3, (float) getroundy (price3), p);
Canvas.drawline (WIDE/3, (float) getroundy (price3), WIDE/2, (float) getroundy (price4), p);
Canvas.drawline (WIDE/2, (float) getroundy (Price4), wide/2+ (WIDE/6) * (time-15)/5, (float) b, p);
}else if (time>20&&time<=25) {

Double s_y = Getroundy (Price5);
Double e_y = Getroundy (Price6);
Double a= (s_y-e_y)/5;
Double b=s_y-a* (time-20);

Canvas.drawline (0, (float) getroundy (Price1), WIDE/6, (float) getroundy (PRICE2), p);
Canvas.drawline (WIDE/6, (float) getroundy (price2), WIDE/3, (float) getroundy (price3), p);
Canvas.drawline (WIDE/3, (float) getroundy (price3), WIDE/2, (float) getroundy (price4), p);
Canvas.drawline (WIDE/2, (float) getroundy (price4), wide/6*4, (float) getroundy (price5), p);
Canvas.drawline (Wide/6*4, (float) getroundy (price5), wide/6*4+ (WIDE/6) * (time-20)/5, (float) b, p);
}else if (time>25) {

Double s_y = Getroundy (Price6);
Double e_y = Getroundy (Price7);
Double a= (s_y-e_y)/5;
Double b=s_y-a* (time-25);

Canvas.drawline (0, (float) getroundy (Price1), WIDE/6, (float) getroundy (PRICE2), p);
Canvas.drawline (WIDE/6, (float) getroundy (price2), WIDE/3, (float) getroundy (price3), p);
Canvas.drawline (WIDE/3, (float) getroundy (price3), WIDE/2, (float) getroundy (price4), p);
Canvas.drawline (WIDE/2, (float) getroundy (price4), wide/6*4, (float) getroundy (price5), p);
Canvas.drawline (Wide/6*4, (float) getroundy (price5), wide/6*5, (float) getroundy (price6), p);
Canvas.drawline (wide/6*5, (float) getroundy (Price6), wide/6*5+ (WIDE/6) * (time-25)/5, (float) b, p);
}

Canvas.drawline (0, (float) getroundy (Price1), WIDE/6, (float) getroundy (PRICE2), p);
Canvas.drawline (WIDE/6, (float) getroundy (price2), WIDE/3, (float) getroundy (price3), p);
Canvas.drawline (WIDE/3, (float) getroundy (price3), WIDE/2, (float) getroundy (price4), p);
Canvas.drawline (WIDE/2, (float) getroundy (price4), wide/6*4, (float) getroundy (price5), p);
Canvas.drawline (Wide/6*4, (float) getroundy (price5), wide/6*5, (float) getroundy (price6), p);
Canvas.drawline (wide/6*5, (float) getroundy (PRICE6), wide, (float) getroundy (price7), p);


}
}

Get the y-axis coordinate point
Private double Getroundy (String str) {
Double D_max = double.parsedouble (Maxprice);
Double d_min = double.parsedouble (Minprice);

return (double) high)-((Double.parsedouble (str)-d_min)/(d_max-d_min)) *high;
Return ((double.parsedouble (str)-d_min)/(d_max-d_min)) *high;
}


Animation asynchronous task
private int time=0;
public class Task extends asynctask{

Private Handler Handler;
Public task (Handler Handler) {
This.handler=handler;
}

@Override
Protected Object doinbackground (object[] params) {

for (int i=1;i<31;i++) {
try {
Time=i;
Handler.sendemptymessage (1);
Thread.Sleep ((Long) (600/30));
}catch (Exception p) {
P.printstacktrace ();
}
}
Handler.sendemptymessage (2);
return null;
}
}


Accept the message Refresh interface
Private Handler handler=new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case 1:
Invalidate ();
Break
Case 2:
if (a!=null) {
A.cancel (TRUE);
A=null;
}
Break
}
}
};

Private Asynctask A;

Start animation
public void Start () {
A=new task (handler);
A.execute ();
}

End Animation manually
public void Stop () {
if (a!=null) {
A.cancel (TRUE);
A=null;
}
time=0;
Invalidate ();
}

}

Android Custom View: Line chart (with animation effect)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.