1、定义视图类:
package com.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 DynamicLine 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 DynamicLine(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public DynamicLine(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public DynamicLine(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
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 which x position you want to draw.
* @param curY which y position you want to draw.
* @see all you put x-y position will connect to a line.
*/
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();
}
}
<!-- 左侧布局 -->
<RelativeLayout android:id="@+id/RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#eeeeee">
<com.view.ITypeTiXin
android:id="@+id/ITypeTiXin"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<<span style="color:#ff0000;">com.view.DynamicLine
android:id="@+id/DynamicLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
/></span>
</RelativeLayout>
3、Activity调用:
package com.activity.pelvicmuscletrain;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import com.view.DynamicLine;
public class TrainGradeActivity extends Activity {
<span style="color:#ff0000;">private static final int MSG_DATA_CHANGE = 0x11;
private DynamicLine dynamicLine;
private Handler mHandler;
private int mX = 40;</span>
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
setContentView(R.layout.activity_traingrade);
//动态绘制压力曲线视图
<span style="color:#ff0000;">dynamicLine = (DynamicLine) this.findViewById(R.id.DynamicLine);
//建立handler消息句柄
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case MSG_DATA_CHANGE:
dynamicLine.setLinePoint(msg.arg1, msg.arg2);
break;
default:
break;
}
super.handleMessage(msg);
}
};
//新建线程,模拟消息发送,重绘压力曲线
new Thread(){
public void run() {
for (int index=0; index<20; 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 += 30;
}
};
}.start(); </span>
}
@Override
protected void onResume() {
/**
* 设置为横屏
*/
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
super.onResume();
}
}