直接上代碼。。。。 public class CastView7 extends View{ private Paint linePaint;//坐標系的直線畫筆 private Paint zhexianPaint;//折現畫筆 private Paint dianPaint;//點的畫筆 pri ...
直接上代碼。。。。
public class CastView7 extends View{
private Paint linePaint;//坐標系的直線畫筆
private Paint zhexianPaint;//折現畫筆
private Paint dianPaint;//點的畫筆
private Paint textPaint;//文字的畫筆
private int x=0;
private int y=0;
//用來裝每個點的(x,y)
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);
//獲取屏幕寬高
x = wm.getDefaultDisplay().getWidth();
y = wm.getDefaultDisplay().getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawLine(DELL,DELL,DELL,y-250, linePaint);//畫X的正坐標線
int county=(x-250)/DELL;
for(int i=0;i<county/2;i++){ //迴圈畫出橫向的所有線和數字
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++){//畫Y軸的數字
canvas.drawText(i+"", DELL+(100*i),y-220, textPaint);
}
for(int i=0;i<list.size();i++){//折現圖和拐點
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);
}
//留給activity調用的方法,重繪。
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的代碼:
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);//調用setlist重繪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.getDefaultDisplay().getWidth();
y = wm.getDefaultDisplay().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);//一秒畫一條
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
}
}
佈局文件:
<?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>
最後別忘了設置清單文件,把手機設置成為橫屏模式。