先上图,里面的数据时伪数据,用的时候传入参数即可。柱状图会根据数值的大小来变换显示的颜色,比如绿色、土黄色和红色。柱状图升高采用了类似于动画效果,可以在创建时设置是否启动动画效果。这个柱状图实现很简单,已经封装成一个view,大家可以直接使用,有什么问题和意见欢迎大家指教。


ConfigurationView   柱状图组件
  1. package org.gmy.view.config;

  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.View;
  8. import android.widget.TextView;

  9. public class ConfigurationView extends View {

  10. private Paint paint;
  11. private Paint font_Paint;
  12. // 数值显示的偏移量
  13. private int numWidth = 9;

  14. // 起始高度为 最大高度减去 80 【注意这里的高度是反的,也就是说,y轴是逐渐减少的】
  15. private float startHeight = Configuration.HEIGHT-80;
  16. private float endHeight = startHeight;
  17. // 柱状图的宽度
  18. private int viewWidth = 20;

  19. // 组态图的高度 【显示的数值,100 为 100%】
  20. private int maxSize = 43;
  21. private int indexSize = 0;
  22. // 要显示的模式 【类型,比如:℃和百分比等】
  23. private String displayMode = "%";
  24. // 模式
  25. private boolean mode = false;
  26. // 线程控制
  27. private boolean display = true;
  28. // 是否开启动画效果
  29. private boolean animMode = true;

  30. /**
  31. *
  32. * @param context
  33. * @param maxSize 需要显示的数值
  34. * @param displayMode 显示的类型
  35. */
  36. public ConfigurationView(Context context, int maxSize, String displayMode) {
  37. super(context);
  38. this.maxSize = maxSize;
  39. this.displayMode = displayMode;
  40. init();
  41. }

  42. /**
  43. *
  44. * @param context
  45. * @param maxSize 需要显示的数值
  46. * @param displayMode 显示的类型
  47. * @param mode 显示的模式,默认为false,数值越高,颜色越偏向红色。为true反之
  48. */
  49. public ConfigurationView(Context context, int maxSize, String displayMode, boolean mode) {
  50. super(context);
  51. this.maxSize = maxSize;
  52. this.displayMode = displayMode;
  53. this.mode = mode;
  54. init();
  55. }

  56. /**
  57. *
  58. * @param context
  59. * @param maxSize 需要显示的数值
  60. * @param displayMode 显示的类型
  61. * @param mode 显示的模式,默认为false,数值越高,颜色越偏向红色。为true反之
  62. * @param animMode 是否显示动画加载效果,默认为true
  63. */
  64. public ConfigurationView(Context context, int maxSize, String displayMode, boolean mode, boolean animMode) {
  65. super(context);
  66. this.maxSize = maxSize;
  67. this.displayMode = displayMode;
  68. this.mode = mode;
  69. this.animMode = animMode;
  70. init();
  71. }

  72. // 绘制界面
  73. @Override
  74. protected void onDraw(Canvas canvas) {
  75. super.onDraw(canvas);
  76. canvas.drawRect(10, endHeight, 10+viewWidth, startHeight, paint);
  77. if(!display){
  78. // 这段if语句可以放在初始化中,这个就交给大家吧。
  79. if(!mode && indexSize >= 50){
  80. paint.setARGB(255, 200, 200, 60);
  81. if(!mode && indexSize >= 80){
  82. paint.setARGB(255, (indexSize<100)?(110+indexSize+45):255, (indexSize<100)?210-(indexSize+45):0, 20);
  83. }
  84. }else if(mode && indexSize <= 50){
  85. paint.setARGB(255, 200, 200, 60);
  86. if(mode && indexSize <= 30){
  87. paint.setARGB(255, 255-indexSize, indexSize, 20);
  88. }
  89. }
  90. canvas.drawRect(10, endHeight, 10+viewWidth, startHeight, paint);
  91. paint.setARGB(255, 99, 66, 0);
  92. canvas.drawText(""+indexSize, numWidth, endHeight-5, paint);
  93. paint.setARGB(255, 110, 210, 60);
  94. }
  95. canvas.drawText(displayMode, 0, startHeight+15, font_Paint);
  96. }

  97. // 初始化
  98. private void init(){
  99. // 数值初始化
  100. paint = new Paint();
  101. paint.setARGB(255, 110, 210, 20);
  102. font_Paint = new Paint();
  103. font_Paint.setARGB(255, 66, 66, 66);

  104. // 设置顶端数值显示的偏移量,数值越小,偏移量越大
  105. numWidth = 9;
  106. if(maxSize < 10){
  107. numWidth = 15;
  108. }else if(maxSize < 100){
  109. numWidth = 12;
  110. }

  111. if(animMode){
  112. // 启动一个线程来实现柱状图缓慢增高
  113. thread.start();
  114. }else{
  115. // 不启用动画效果,则直接赋值进行绘制
  116. display = false;
  117. indexSize = maxSize;
  118. endHeight = startHeight-(float) (maxSize*1.5);
  119. invalidate();
  120. }
  121. }

  122. private Handler handler = new Handler(){
  123. @Override
  124. public void handleMessage(Message msg) {
  125. super.handleMessage(msg);
  126. // 通过endHeight >= 20将柱状图的高度控制在150以内,这样刚好循环一百次到顶部
  127. if(msg.what == 1 && indexSize < maxSize && endHeight >= 20){
  128. endHeight -= 1.5;
  129. indexSize += 1;
  130. }else{
  131. display = false;
  132. }
  133. invalidate();
  134. }
  135. };


  136. private Thread thread = new Thread(){
  137. @Override
  138. public void run(){
  139. while(!Thread.currentThread().isInterrupted() && display )
  140. {
  141. Message msg = new Message();
  142. msg.what = 1;
  143. handler.sendMessage(msg);
  144. try {
  145. // 每隔15毫秒触发,尽量使升高的效果看起来平滑
  146. Thread.sleep(15);
  147. } catch (InterruptedException e) {
  148. System.err.println("InterruptedException!线程关闭");
  149. this.interrupt();
  150. }
  151. }
  152. }
  153. };

  154. }
复制代码
Configuration 类
一个普通的activity,创建了五个柱状图用于测试。第一个view = new ConfigurationView(this, 100, "温度/℃", false, false);没有使用动画效果。
  1. package org.gmy.view.config;

  2. import org.gmy.view.R;

  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.ViewGroup.LayoutParams;
  7. import android.widget.EditText;
  8. import android.widget.LinearLayout;
  9. import android.widget.TableRow;

  10. public class Configuration extends Activity {

  11. public static final int WIDTH = 280;
  12. public static final int HEIGHT = 250;

  13. private ConfigurationView view, view1, view2, view3, view4;
  14. private LinearLayout layout;

  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.config_dialog);
  19. layout = (LinearLayout) findViewById(R.id.configLayout);

  20. view = new ConfigurationView(this, 100, "温度/℃", false, false);
  21. layout.addView(view, new LayoutParams(50, LayoutParams.FILL_PARENT));

  22. view1 = new ConfigurationView(this, 100, "电量/%", true);
  23. layout.addView(view1, new LayoutParams(50, LayoutParams.FILL_PARENT));

  24. view2 = new ConfigurationView(this, 80, "电量/%", true);
  25. layout.addView(view2, new LayoutParams(50, LayoutParams.FILL_PARENT));

  26. view3 = new ConfigurationView(this, 40, "电量/%", true);
  27. layout.addView(view3, new LayoutParams(50, LayoutParams.FILL_PARENT));

  28. view4 = new ConfigurationView(this, 2, "电量/%", true);
  29. layout.addView(view4, new LayoutParams(50, LayoutParams.FILL_PARENT));
  30. }

  31. }
复制代码
config_dialog.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="280dip"
  6. android:layout_height="250dip"
  7. android:padding="10dip"
  8. android:layout_gravity="center_horizontal"
  9. android:background="@drawable/dialog_alert_bg2"
  10. >
  11. <TextView
  12. android:id="@+id/configTitle"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:gravity="center_horizontal"
  16. android:text="柱状图"
  17. android:textSize="18dip"
  18. android:textColor="#fdb814"
  19. />
  20. <LinearLayout
  21. android:orientation="horizontal"
  22. android:id="@+id/configLayout"
  23. android:layout_width="260dip"
  24. android:layout_height="220dip"
  25. >

  26. </LinearLayout>
  27. </LinearLayout>
复制代码
AndroidManifest.xml  使用了对话框的主题
  1. <activity android:name=".Configuration" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" >
  2. <intent-filter>
  3. <action android:name="android.intent.action.MAIN" />
  4. <category android:name="android.intent.category.LAUNCHER" />
  5. </intent-filter>
  6. </activity>
复制代码
点击下载代码
人始终是要有梦想的
高级工程师 2# lin0 发表于 2011-8-13 09:09:33
很好
高级工程师 3# wcl2222 发表于 2011-8-13 15:44:16
高级工程师 4# jipengweb 发表于 2012-2-6 17:07:51
中级工程师 5# casinosun 发表于 2012-2-8 09:04:33
画的更好一点就好了
初级构架师 6# bxxasn 发表于 2012-2-8 10:21:44

Configuration.HEIGHT

2.1点击不出来..
实习工程师 7# lyd6150 发表于 2012-2-13 12:42:01
有没有更炫一点的?
实习工程师 8# lyd6150 发表于 2012-2-13 12:42:19
有没有更炫一点的?
高级工程师 9# 半米阳光 发表于 2012-2-13 14:04:31
  大家共同学习
中级构架师 10# slider 发表于 2012-2-23 09:41:52
谢谢分享,收藏了
一切可以从头开始!!!
初级构架师 11# xyuan52021 发表于 2012-2-23 14:48:25
中级工程师 12# liaixuan14 发表于 2012-2-24 15:53:44
很好很强大啊
初级工程师 13# 111juji18 发表于 2012-2-25 18:02:11
看看 ,3Q
实习工程师 14# zhang_xiwei 发表于 2012-2-28 16:02:23
非常有想象!
高级工程师 15# samcheng 发表于 2012-2-29 10:05:57
_:)_ 谢谢楼主. Many Thxs
高级工程师 16# 迪大爷 发表于 2012-3-7 21:38:09
好东西  学习了
实习工程师 17# chenshijun 发表于 2012-3-31 10:08:37
lz为什么不把项目放在上面呢,期待中
您需要登录后才可以回帖 登录 | 注册

关于我们|手机版|Archiver|DEVDIV.COM ( 京ICP备07040843号 )  

GMT+8, 2012-5-19 16:07

Powered by DEVDIV.COM!

© 2010-2012 DEVDIV.COM Coummunity.

回顶部