Custom view for Android (simple line chart) and custom view line for Android
Directly go to the code ....
Public class CastView7 extends View {
Private Paint linePaint; // linear Paint in the coordinate system
Private Paint zhexianPaint; // discount Paint brush
Private Paint dianPaint; // specifies the Paint brush of a vertex.
Private Paint textPaint; // the Paint brush for text
Private int x = 0;
Private int y = 0;
// Used to hold the (x, y) of each vertex)
Private List <Map <String, Integer> list = new ArrayList <Map <String, Integer> ();
Public final static String X = "x ";
Public final static String Y = "y ";
Public final static int DELL = 50;
Public CastView7 (Context context, AttributeSet attrs ){
Super (context, attrs );
// TODO Auto-generated constructor stub
Init ();
WindowManager wm = (WindowManager) getContext ()
. GetSystemService (Context. WINDOW_SERVICE );
// Obtain the screen width and height
X = wm. getdefadisplay display (). getWidth ();
Y = wm. getdefadisplay display (). getHeight ();
}
@ Override
Protected void onDraw (Canvas canvas ){
Canvas. drawLine (DELL, y-250, linePaint); // draw the coordinate line of X
Int county = (x-250)/DELL;
For (int I = 0; I <county/2; I ++) {// draw the lateral wired and numbers cyclically
Canvas. drawLine (DELL, (y-250)-(DELL * I), x-DELL, (y-250)-(DELL * I), linePaint );
Canvas. drawText (I + "", 30, (y-250)-(DELL * I), textPaint );
}
Int countx = x/DELL;
For (int I = 0; I <countx/2; I ++) {// draw a number on the Y axis
Canvas. drawText (I + "", DELL + (100 * I), y-220, textPaint );
}
For (int I = 0; I <list. size (); I ++) {// discounted graph and inflection point
If (I> 0)
Canvas. drawLine (list. get (I-1 ). get (X), list. get (I-1 ). get (Y), list. get (I ). get (X), list. get (I ). get (Y), zhexianPaint );
Canvas. drawCircle (list. get (I). get (X), list. get (I). get (Y), 5, dianPaint );
}
}
Private void init (){
LinePaint = new Paint ();
LinePaint. setAntiAlias (true );
LinePaint. setColor (Color. BLACK );
ZhexianPaint = new Paint ();
ZhexianPaint. setAntiAlias (true );
ZhexianPaint. setColor (Color. RED );
DianPaint = new Paint ();
DianPaint. setAntiAlias (true );
DianPaint. setStyle (Style. FILL );
DianPaint. setColor (Color. BLUE );
TextPaint = new Paint ();
TextPaint. setColor (Color. BLACK );
TextPaint. setAntiAlias (true );
}
// Left the method called by the activity to be repainted.
Public void setList (int x, int y ){
Map <String, Integer> map = new HashMap <String, Integer> ();
Map. put (X, x );
Map. put (Y, y );
List. add (map );
Invalidate ();
}
}
Activity Code:
Public class Activity7 extends Activity {
Private CastView7 cv7;
Private int x = 0;
Private int y = 0;
Private Handler h = new Handler (){
Public void handleMessage (android. OS. Message msg ){
Cv7.setList (msg. arg1, msg. arg2); // call setlist to redraw the view
};
};
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity7 );
Cv7 = (CastView7) findViewById (R. id. cv7 );
WindowManager wm = (WindowManager) this
. GetSystemService (Context. WINDOW_SERVICE );
X = wm. getdefadisplay display (). getWidth ();
Y = wm. getdefadisplay display (). getHeight ();
New Thread (){
Public void run (){
Int countx = x/50; Log. I ("zhangteng", countx/2 + "");
For (int I = 0; I <countx/2; I ++ ){
Int xx = 50 + (100 * I );
Int yy = (y-250)-(50 * (int) (Math. random () * 16 ))));
Message msg = new Message ();
Msg. what = 1;
Msg. arg1 = xx;
Msg. arg2 = yy;
H. sendMessage (msg );
Try {
Sleep (1000); // draw a line in one second
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
};
}. Start ();
}
}
Layout file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<Com. zhangteng. castomview7.CastView7
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/cv7"
/>
</LinearLayout>
Finally, do not forget to set the configuration file and set the mobile phone to Landscape mode.