安卓自定義view模擬每隔一定時間更新股市


代碼如下:

package com.example.customview;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;


import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class CustomView extends View {
ArrayList<HashMap<String, String>> list1;
ArrayList<HashMap<String, String>> list2;
    int viewWidth,viewHeight;
    int xGap;//時間間距
    int maxPrice;//最大價格
    Random random=new Random();
    boolean isRunning=true;
private int y;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
//每秒獲取一個數據
new Thread(new Runnable() {
@Override
public void run() {
while(isRunning){
try {
list1=getData1();
list2=getData2();
y=0;
postInvalidate();
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
    }

private ArrayList<HashMap<String, String>> getData1() {
ArrayList<HashMap<String, String>> list=
new ArrayList<HashMap<String,String>>();
HashMap<String, String> map1=new HashMap<String, String>();
map1.put("time","9");
map1.put("price",String.valueOf(random.nextInt(6000)));
list.add(map1);


HashMap<String, String> map2=new HashMap<String, String>();
map2.put("time","10");
map2.put("price",String.valueOf(random.nextInt(6000)));
list.add(map2);


HashMap<String, String> map3=new HashMap<String, String>();
map3.put("time","11");
map3.put("price",String.valueOf(random.nextInt(6000)));
list.add(map3);


HashMap<String, String> map4=new HashMap<String, String>();
map4.put("time","13");
map4.put("price",String.valueOf(random.nextInt(6000)));
list.add(map4);
        
//求最大價格
for(int i=0;i<list.size();i++){
HashMap<String,String> map=list.get(i);

int currentPrice=Integer.parseInt(map.get("price"));
if(currentPrice>maxPrice){
maxPrice=currentPrice;
}
}
Log.i("TAG", "最大價格="+maxPrice);
return list;
}


private ArrayList<HashMap<String, String>> getData2() {
ArrayList<HashMap<String, String>> list=
new ArrayList<HashMap<String,String>>();
HashMap<String, String> map1=new HashMap<String, String>();
map1.put("time","9");
map1.put("price",String.valueOf(random.nextInt(6000)));
list.add(map1);


HashMap<String, String> map2=new HashMap<String, String>();
map2.put("time","10");
map2.put("price",String.valueOf(random.nextInt(6000)));
list.add(map2);


HashMap<String, String> map3=new HashMap<String, String>();
map3.put("time","11");
map3.put("price",String.valueOf(random.nextInt(6000)));
list.add(map3);


HashMap<String, String> map4=new HashMap<String, String>();
map4.put("time","13");
map4.put("price",String.valueOf(random.nextInt(6000)));
list.add(map4);
        
//求最大價格
for(int i=0;i<list.size();i++){
HashMap<String,String> map=list.get(i);

int currentPrice=Integer.parseInt(map.get("price"));
if(currentPrice>maxPrice){
maxPrice=currentPrice;
}
}
Log.i("TAG", "最大價格="+maxPrice);
return list;
}


@Override
protected void onDraw(Canvas canvas) {

// 畫背景,最好畫矩形
Paint paint=new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(18);

Rect rectbackground=new Rect(0 , 0, viewWidth, viewHeight);
canvas.drawRect(rectbackground, paint);

onDrawList(canvas,list1,paint);
onDrawList(canvas,list2,paint);
//點擊屏幕畫一條線
canvas.drawLine(0, y, viewWidth, y, paint);

}


private void onDrawList(Canvas canvas,ArrayList<HashMap<String, String>> list,Paint paint) {
int timeHeight=40;
int lastPriceWidth = 48;
//畫時間
//計算最後一個時間字符串的寬度
//int lastWidth = getLastPriceWidth(paint);
paint.setColor(0xFFFFFFFF);
xGap=viewWidth/(list.size()-1);//求間距
xGap = (viewWidth - lastPriceWidth) / (list.size() - 1);
for(int i=0;i<list.size();i++){
HashMap<String, String> map=list.get(i);
String time=map.get("time");
int timeLeft=xGap*i;
int timeTop=viewHeight;

canvas.drawText(time, timeLeft, timeTop, paint);
//畫價格
//顯示價格區域的寬度
int priceHeight=viewHeight-timeHeight;

int currentPrice=Integer.parseInt(map.get("price"));
int priceTop=currentPrice*priceHeight/maxPrice;
//問題:越大的顯示在最下面;解決:
priceTop=priceHeight-priceTop;
//問題:最大價格不顯示;解決:
priceTop=priceTop+24;
canvas.drawText(map.get("price"), timeLeft, priceTop, paint);

//畫線
if(i<list.size()-1){

int startX=timeLeft;
int startY=priceTop;
int stopX=xGap*(i+1);

int stopPrice=Integer.parseInt(list.get(i+1).get("price"));
int stopTop=stopPrice*priceHeight/maxPrice;
int stopY=priceHeight-stopTop+24;
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
}
}

//求字符串寬度
public int getLastPriceWidth(Paint paint,ArrayList<HashMap<String, String>> list){
    Rect rect=new Rect();
    HashMap<String, String> map=list.get(list.size()-1);
    String lastTime=map.get("time");
    Log.i("TAG","要畫的時間是:"+lastTime);
   
    paint.getTextBounds(lastTime, 0,lastTime.length() , rect);
   
    int strwid = rect.width();
return strwid;
    }
@Override
public boolean onTouchEvent(MotionEvent event) {
y=(int) event.getY();
invalidate();
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
viewWidth=w;
viewHeight=h; 
}
}


main文件沒有寫任何代碼和layout文件添加了自定義佈局:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章