Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android自定義趨勢圖

android自定義趨勢圖

編輯:關於Android編程

 之前項目需求裡有一個需求是要根據每周的天氣溫度去繪制一個趨勢圖,這個圖不基於XY坐標,就是一個單純的趨勢圖,百度後看了一些博客,大體上有了一些思路,下面是整個趨勢圖的效果圖:

\

最下面的點線圖就是要做的效果。

下面貼出趨勢圖示例代碼:

package com.example.testxyjar;

import utils.XYViewDrawBitmap;
import utils.XYViewDrawLine;
import utils.XYViewDrawPointer;
import utils.XYViewDrawText;
import view.XYView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import bean.XYViewBitmapBean;
import bean.XYViewLineBean;
import bean.XYViewPointerBean;
import bean.XYViewTextBean;

public class MainActivity extends Activity {
    private XYView xyview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initCache();
        initView();
        initData();
    }

    private void initCache() {
    }

    private void initView() {
        xyview = (XYView) findViewById(R.id.main_page_view_xyview);

    }

    private void initData() {
        // 第一個點
        XYViewPointerBean xyPointer = new XYViewPointerBean();
        float data[] = { 10.0f, 20.0f, -10.0f, 40.0f, 20.0f, 50.0f };
        xyPointer.setData(data);
        xyPointer.setColor(Color.RED);
        xyPointer.setStrokeWidth(20.0f);
        XYViewDrawPointer xyViewDrawPointer = new XYViewDrawPointer(xyPointer);
        xyview.addXYViewDrawable(xyViewDrawPointer);
        // 畫圖
        XYViewBitmapBean xybitmap = new XYViewBitmapBean();
        float dataBitmap[] = { 10.0f, 20.0f, -10.0f, 40.0f, 20.0f, 50.0f };
        Drawable d = getResources().getDrawable(
                R.drawable.weather_temperature_point_icon_high);
        BitmapDrawable bd = (BitmapDrawable) d;
        Bitmap b = bd.getBitmap();
        Bitmap[] bs = { b, b, b, b, b, b };
        xybitmap.setY_away(40);
        xybitmap.setData(dataBitmap);
        xybitmap.setBitmapData(bs);
        XYViewDrawBitmap xyViewDrawBitmap = new XYViewDrawBitmap(xybitmap);
        xyview.addXYViewDrawable(xyViewDrawBitmap);
        // 第一個線
        XYViewLineBean xyLineBean = new XYViewLineBean();
        xyLineBean.setData(data);
        xyLineBean.setColor(Color.BLACK);
        xyLineBean.setStrokeWidth(2.0f);
        XYViewDrawLine xyViewLine = new XYViewDrawLine(xyLineBean);
        xyview.addXYViewDrawable(xyViewLine);
        //
        XYViewTextBean xyTexter = new XYViewTextBean();
        float dataText[] = { 10.0f, 20.0f, -10.0f, 40.0f, 20.0f, 50.0f };
        String [] dataStrs={"大娃","二娃","三娃","四娃","五娃","六娃"};
        xyTexter.setData(dataText);
        xyTexter.setColor(Color.RED);
        xyTexter.setStrokeWidth(1.0f);
        xyTexter.setTextSize(18.0f);
        xyTexter.setTextData(dataStrs);
        XYViewDrawText xyViewTexter = new XYViewDrawText(xyTexter);
        xyview.addXYViewDrawable(xyViewTexter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
代碼中用到的jar包以上傳到我的資源裡,如有求要,請下載使用。


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved