Customizing dynamically generated line charts

Source: Internet
Author: User

package com.example.line;import java.util.arraylist;import java.util.hashmap;import  java.util.list;import java.util.map;import android.annotation.suppresslint;import  android.content.context;import android.graphics.canvas;import android.graphics.color;import  Android.graphics.paint;import android.graphics.paintflagsdrawfilter;import android.util.attributeset ; import android.view.view;public class lineview extends view { private  final static String X_KEY =  "Xpos"; private final static  string y_key =  "Ypos"; private list<map<string, integer>>  Mlistpoint = new arraylist<map<string, integer>> ();  Paint mPaint  = new paint ();  public lineview (Context context, attributeset attrs,  int defstyle)  {  super (cOntext, attrs, defstyle);  } public lineview (Context context, attributeset  attrs)  {  super (context, attrs);  } public lineview (Context  Context)  {  super (context); }  @SuppressLint ("drawallocation")   @Override   Protected void ondraw (Canvas canvas)  {  super.ondraw (Canvas);   Mpaint.setcolor (color.red);   mpaint.setantialias (True);  for  (int index =  0; index < mlistpoint.size ();  index++)  {   if  (index  > 0)  {    canvas.drawline (Mlistpoint.get (index - 1). Get (X_KEY ),       mlistpoint.get (index - 1). Get (Y_key),       mlistpoint.get (Index). Get (X_key),  mlistpoint.get (index)          .get (Y_key),  mpaint);     canvas.setdrawfilter (New paintflagsdrawfilter (0,       paint.anti_alias_flag | paint.filter_bitmap_flag));    }  } }  /**  *  Set Data   *  @param  curX  *  @param  curY   */ public void setlinepoint (Int curx, int cury)  {  Map< String, integer> temp = new hashmap<string, integer> ();   Temp.put (X_key, curx);   temp.put (Y_key, cury);   mlistpoint.add (temp);   invalidate ();  }}ui class:package com.example.line;import android.os.bundle;import  android.os.handler;import android.os.message;import android.annotation.suppresslint;import  android.app.Activity; @SuppressLint ("Handlerleak") public class mainactivity extends  activity { private static  final int msg_data_change = 0x11; private lineview mlineview; private  Handler mHandler; private int mX = 0;  @Override  protected  Void oncreate (bundle savedinstancestate)  {  super.oncreate (savedInstanceState);   setcontentview (R.layout.activity_main);   init ();  mhandler = new  Handler ()  {    @Override    public void handlemessage (message  msg)  {    switch  (msg.what)  {    case msg_data_ Change:     mlineview.setlinepoint (MSG.ARG1,&NBSP;MSG.ARG2);      break;    }    super.handlemessage (msg);    }   };  new thread ()  {    @Override    public  Void run ()  {    for  (int index = 0; index < 100; index++)  {      message message = new message ();      message.what = MSG_DATA_CHANGE;     message.arg1 = mX;      message.arg2 =  (int)   (math.random ()  * 200);      mhandler.sendmessage (message);     try {       sleep (;     } catch ) (interruptedexception e)  {      // TODO Auto-generated catch block       e.printstacktrace ();      }     mx  += 10;    }   }  }.start (); } private  Void init () &NBSP;{&NBSP;&Nbsp;mlineview =  (Lineview)  findviewbyid (r.id.line);  }} layout <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"     android:paddingbottom= "@dimen/activity_vertical_ Margin "    android:paddingleft=" @dimen/activity_horizontal_margin "     android:paddingright= "@dimen/activity_horizontal_margin"     android:paddingtop= "@dimen/ Activity_vertical_margin "    tools:context=". Mainactivity " >    <com.example.line.LineView         android:id= "@+id/line"         android:layout_ Centerinparent= "true"         android:layout_width="400DP"         android:layout_height= "400DP"          android:background= "#ffffff"         /></ Relativelayout>

This article is from the "It Software Development Materials" blog, please be sure to keep this source http://491733638.blog.51cto.com/3913830/1664714

Customizing dynamically generated line charts

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.