首页 > 代码库 > 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包以上传到我的资源里,如有求要,请下载使用。
android趋势图创建与使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。