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));
}
}
}
/**
* 设置数据
* @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类:
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, 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(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mX += 10;
}
}
}.start();
}
private void init() {
mLineView = (LineView) findViewById(R.id.line);
}
}
布局
<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>
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。