achartengine作为android开发中最常用的实现图标的开源框架,使用比较方便,参考官方文档谢了如下Demo,实现了自定义折线图。
package edu.ustb.chart; import org.achartengine.ChartFactory; import org.achartengine.GraphicalView; import org.achartengine.chart.PointStyle; import org.achartengine.model.SeriesSelection; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.model.XYSeries; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends Activity { private GraphicalView mChartView; private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset(); double[] xdata = new double[] { 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 }; double[] ydata = new double[] { 11, 11, 22, 33, 44, 55, 66, 77, 88, 99 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } /** * 得到住渲染器,并对其各项属性进行设置 * * @return */ public XYMultipleSeriesRenderer getRenderer() { /** 设置主渲染器的各种属性 */ // 设置背景色是否启用 mRenderer.setApplyBackgroundColor(true); // 设置背景色 mRenderer.setBackgroundColor(Color.argb(100, 20, 30, 40)); // 设置x y轴标题字体大小 mRenderer.setAxisTitleTextSize(16); // 设置表格标题字体大小 mRenderer.setChartTitleTextSize(20); // 设置标签字体大小 mRenderer.setLabelsTextSize(15); // 设置图例字体大小 mRenderer.setLegendTextSize(15); mRenderer.setMargins(new int[] { 20, 20, 20, 20 }); // 设置是否显示放大缩小按钮 mRenderer.setZoomButtonsVisible(true); // 设置图表上显示点的大小 mRenderer.setPointSize(5); // create a new renderer for the new series XYSeriesRenderer renderer = new XYSeriesRenderer(); // set some renderer properties renderer.setPointStyle(PointStyle.CIRCLE); renderer.setFillPoints(true); renderer.setDisplayChartValues(true); renderer.setDisplayChartValuesDistance(100); renderer.setColor(Color.RED); setSeriesWidgetsEnabled(true); mRenderer.addSeriesRenderer(renderer); return mRenderer; } /** * 得到住渲染器,并对其各项属性进行设置 * * @return */ public XYMultipleSeriesDataset getDataset() { String seriesTitle = "健康数据"; XYSeries mSeries = new XYSeries(seriesTitle); for (int i = 0; i < 10; i++) { double x = xdata[i]; double y = ydata[i]; // 把坐标添加到当前序列中去 mSeries.add(x, y); } mDataset.addSeries(mSeries); return mDataset; } @Override protected void onResume() { super.onResume(); if (mChartView == null) { LinearLayout layout = (LinearLayout) findViewById(R.id.chart); mChartView = ChartFactory.getLineChartView(this, getDataset(), getRenderer()); // enable the chart click events mRenderer.setClickEnabled(true); mRenderer.setSelectableBuffer(10); mChartView.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // handle the click event on the chart SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint(); if (seriesSelection == null) { Toast.makeText(MainActivity.this, "No chart element", Toast.LENGTH_SHORT).show(); } else { // display information of the clicked point Toast.makeText(MainActivity.this, "Chart element in series index " + seriesSelection.getSeriesIndex() + " data point index " + seriesSelection.getPointIndex() + " was clicked" + " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue(), Toast.LENGTH_SHORT).show(); } } }); layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); boolean enabled = mDataset.getSeriesCount() > 0; setSeriesWidgetsEnabled(enabled); } else { mChartView.repaint(); } } /** * Enable or disable the add data to series widgets * * @param enabled * the enabled state */ private void setSeriesWidgetsEnabled(boolean enabled) { // mX.setEnabled(enabled); // mY.setEnabled(enabled); // mAdd.setEnabled(enabled); } }
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TimePicker android:id="@+id/time1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <DatePicker android:id="@+id/date1" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
类
package com.android.Main; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.TimePicker; public class ActivityMain extends Activity { private TimePicker time1; private DatePicker date1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); time1=(TimePicker)this.findViewById(R.id.time1); date1=(DatePicker)this.findViewById(R.id.date1); time1.setIs24HourView(true); time1.setCurrentHour(18); time1.setCurrentMinute(25); date1.updateDate(2005,6, 27); } }
一、硬件要求
(1)AT91SAM9260平台
(2)时钟芯片:Rx8025 SA/NB
(3)接口:通过I2C总线读取,两条总线为PA23、PA24。
二、配置内核
进入内核根目录下,运行配置菜单。
#make menuconfig
(1)选中I2C
Device Drivers --->
I2C suport --->
I2C Hardware Bus support--->
<*> GPIO-based bitbanging I2C
(2)选中以下RTC相关的选项
Device Drivers --->
Real Time Clock --->
(3)选中 Epson RX-8025SA/NB
Device Drivers --->
Real Time Clock --->
Epson Rx-8025SA/NB
注意:不可以选中以下图中AT91 SAM9x/AT91 CAP9选项,否则使用的是内部时钟。
三、添加设备注册
在内核中的/arch/arm/mach-at91/board-sam9260ek.c文件中添加以下蓝色字体语句:
static struct i2c_board_info __initdata ek_i2c_devices[] = {
{
I2C_BOARD_INFO("24c512", 0x50),
.platform_data = &at24c512,
},
/* more devices can be added using expansion connectors */
/* Added for R8025 rtc 20120914 */
{
I2C_BOARD_INFO("rx8025", 0x32),
}
};
四、创建设备节点
在根文件系统/dev目录下执行:
# mknod rtc c 10 135
重新编译内核及根文件系统下载到开发板,查看启动信息,如下图所示:
五、设置RTC时间及系统时间
开发板进入系统后,在终端执行以下命令:
# date 得到系统当前时间
Thu Jan 1 00:00:13 (null) 1970
# hwclock得硬件时钟当前时间
hwclock: RTC_RD_TIME: Invalid or incomplete multibyte or wide character
# date -s "2012-09-17 10:40:40" 设置系统时间为当前时间
Mon Sep 17 10:40:40:40 (null) 2012
# date
Mon Sep 17 10:40:40:42 (null) 2012
# hwclock -w使用硬件时钟与系统时间同步
# hwclock
Mon Sep 17 10:40:40:53 2012 0.000000 seconds
开发板断电一段时间后再次加电启动开发板,进入系统后再使用hwclock命令查看硬件时钟睥当前是否为当前时间。
北京中臻精件科技发展有限公司
网址:www.warewin.com Email:cyj@warewin.com QQ号码:824820111
电话:(010)51735158 51735698 手机:13381082912 联系人:程先生