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包以上传到我的资源里,如有求要,请下载使用。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。