安卓自訂view(簡單折線圖),安卓自訂view折線

來源:互聯網
上載者:User

安卓自訂view(簡單折線圖),安卓自訂view折線

直接上代碼。。。。

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>

 

最後別忘了設定資訊清單檔,把手機設定成為橫屏模式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.