Activity:java
package com.example.zz;android
import java.util.ArrayList;canvas
import java.util.List;app
import android.os.Bundle;dom
import android.app.Activity;ide
import android.graphics.Color;佈局
import android.graphics.drawable.GradientDrawable.Orientation;this
import android.util.Log;spa
import android.view.Menu;orm
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
LineChar line1 = (LineChar) findViewById(R.id.lineChar1);
List<Integer> list = new ArrayList();
list.add(20);
list.add(2);
list.add(5);
list.add(1);
list.add(3);
List<Integer> list1 = new ArrayList();
list1.add(50);
list1.add(20);
list1.add(15);
list1.add(12);
list1.add(35);
List<Integer> list2 = new ArrayList();
list2.add(20);
list2.add(7);
list2.add(8);
list2.add(13);
List<List<Integer>> l = new ArrayList();
l.add(list);
l.add(list1);
l.add(list2);
line1.setList(l);
List<Integer> color = new ArrayList();
color.add(Color.RED);
color.add(Color.BLUE);
color.add(Color.GREEN);
line1.setColor(color);
LineChar line2 = (LineChar) findViewById(R.id.lineChar2);
Log.e("adnroid", "set");
List<Integer> list3 = new ArrayList();
list3.add(20);
list3.add(2);
list3.add(5);
list3.add(1);
list3.add(3);
List<Integer> list4 = new ArrayList();
list4.add(120);
list4.add(22);
list4.add(51);
list4.add(51);
list4.add(93);
List<List<Integer>> l2 = new ArrayList();
l2.add(list3);
l2.add(list4);
line2.setList(l2);
// LineChar line2 = (LineChar) findViewById(R.id.lineChar2);
// Log.e("adnroid", "set");
}
}
自定義折線圖Class:
package com.example.zz;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
/**
* @description 多條折線 折線圖
* @author 李海藝
* @date 2013.12.10
*/
public class LineChar extends SurfaceView implements Callback
{
private SurfaceHolder sfh;
private final int xSize = 24;// 將x軸分紅24份
private final int ySize = 10;// 將Y軸分紅10份
private float xSpace;// 兩條豎線的間距
private float ySpace;// 兩條橫線的間距
private List<List<Integer>> list;// Y軸數據
private final int left = 20;// 距離左邊的距離
private final int right = 20;// 距離右邊的距離
private final int top = 20;// 距離頂部的距離
private final int bottom = 20;// 距離底部的距離
private float max;// Y軸數據的最大值
private List<Integer> color = new ArrayList();// 折線圖的顏色列表
public List<Integer> getColor()
{
return color;
}
public void setColor(List<Integer> color)
{
this.color = color;
}
public List<List<Integer>> getList()
{
return list;
}
public void setList(List<List<Integer>> list)
{
this.list = list;
}
public LineChar(Context context)
{
super(context);
init();
}
public LineChar(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
private void init()
{
sfh = this.getHolder();
sfh.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Canvas canvas = sfh.lockCanvas();
// 畫座標軸
drawXY(canvas);
// 畫折線圖
drawChar(canvas);
sfh.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
}
/**
* 畫座標軸
*
* @param canvas
*/
private void drawXY(Canvas canvas)
{
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
// 畫Y軸
xSpace = (getWidth() - left - right) / xSize;
for (int i = 0; i < xSize; i++)
{
canvas.drawLine(left + right + i * xSpace, top, left + right + i
* xSpace, getHeight() - bottom, paint);
}
// 畫X軸文字
for (int i = 0; i < xSize; i++)
{
Paint paintText = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
canvas.drawText(i + "", left + right - 5 + i * xSpace, getHeight()
- bottom / 5, paintText);
}
// 畫X軸
ySpace = (getHeight() - top - bottom) / ySize;
for (int i = 0; i <= ySize; i++)
{
canvas.drawLine(left + right, top + ySpace * i, getWidth() - left
- right, top + ySpace * i, paint);
}
for (int i = 0; i < list.size(); i++)
{
for (int j = 0; j < list.get(i).size(); j++)
{
if (list.get(i).get(j) > max)
{
max = list.get(i).get(j);
}
}
}
// 畫Y軸文字
for (int i = 0; i < ySize; i++)
{
Paint paintText = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
canvas.drawText((max / 10 * (ySize - i)) + "", left / 2, top
+ ySpace * i + 5, paintText);
}
}
/**
* 畫折線圖
*
* @param canvas
*/
private void drawChar(Canvas canvas)
{
if (color.size() < list.size())
{
for (int i = color.size(); i < list.size(); i++)
{
int r = (int) Math.round(Math.random() * 255);
int g = (int) Math.round(Math.random() * 255);
int b = (int) Math.round(Math.random() * 255);
color.add(Color.rgb(r, g, b));
}
}
for (int i = 0; i < list.size(); i++)
{
for (int j = 0; j < list.get(i).size(); j++)
{
Paint paintCicle = new Paint();
paintCicle.setColor(color.get(i));
paintCicle.setAntiAlias(true);
canvas.drawCircle(left + right + j * xSpace,
top + (ySize - list.get(i).get(j) / (max / ySize))
* ySpace, 3, paintCicle);
if (j < list.get(i).size() - 1)
{
canvas.drawLine(left + right + j * xSpace, top
+ (ySize - list.get(i).get(j) / (max / ySize))
* ySpace, left + right + (j + 1) * xSpace, top
+ (ySize - list.get(i).get(j + 1) / (max / ySize))
* ySpace, paintCicle);
}
}
}
}
/**
* 把畫布擦乾淨,準備繪圖使用。
*/
private void clearCanvas()
{
Canvas canvas = sfh.lockCanvas();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);// 清除畫布
drawXY(canvas);
sfh.unlockCanvasAndPost(canvas);
}
佈局Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.zz.LineChar
android:id="@+id/lineChar1"
android:layout_width="wrap_content"
android:layout_height="200dp" />
<com.example.zz.LineChar
android:id="@+id/lineChar2"
android:layout_width="wrap_content"
android:layout_height="100dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>