最近公司做的项目中要用到折线图,网上看了下都不是很能满足自己的需要,因此就自己写了个,废话不说了,直接上代码

package com.example.view;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.PaintFlagsDrawFilter;import android.graphics.Typeface;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;import android.widget.Scroller;import com.example.aa.R;/*** LastTNT* @author 翟昆* @date 2014-5-6 上午10:28:53* @version 1.0*/public class LineChartView extends View {private int mov_x; // 移动X坐标private int mov_y; // 移动Y坐标private int diff_Y; // Y轴高度private Paint paint;// 声明画笔private int color_xy = Color.rgb(77, 189, 235);//设置标线颜色private int line_color_y = Color.rgb(18, 197, 255);//标注线的颜色private int zhexian_color = Color.rgb(21, 139, 229);//折线的颜色private int Guides_line_color = Color.rgb(229, 21, 60);//参考线的颜色private int text_color = Color.rgb(254, 102, 1);//折线字体颜色private int width_xy = 4;//设置标线宽度private int line_width_y = 2;//标注线的宽度private int zhexian_width = 4;//折线的宽度int init_x = 0;//初始按下的X坐标int init_left = 0;//左边距private int MAX_X = 2000;private int MAX_X_Extend = 100;//向右侧继续延伸100private int MAX_Y = 100;//Y轴最大值private int lineNum = 24; //标线份数private int lineInterval_x =0;//标线间隔private int lineInterval_y = 0;//标注线间隔private int width = 0;//控件宽度private int height = 0;//控件高度private Bitmap bmp;//标记点的图private int Guides_line = 80;//参考线private float textSize = 22;//折线图字体大小private float CalloutSize = 18;//标注字体大小private PaintFlagsDrawFilter pfd;private Matrix matrix = new Matrix();private int paddingBottom = 50; private int paddingTop = 10;private int[] nums = new int[24];//实现惯性滑动效果private Scroller mScroller;private int mTouchSlop;private int mMinimumVelocity;private int mMaximumVelocity;public LineChartView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public LineChartView(Context context) {super(context);}public LineChartView(Context context, AttributeSet attrs) {super(context, attrs);paint = new Paint();pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG| Paint.FILTER_BITMAP_FLAG);paint.setAntiAlias(true);matrix.postScale(1f, 1f);lineInterval_x = MAX_X/lineNum;bmp = BitmapFactory.decodeResource(getResources(), R.drawable.point); for (int i = 0; i < nums.length; i++) {nums[i] = (int) (Math.random()*MAX_Y);}}//void init(Context context) {//mScroller = new Scroller(getContext());//setFocusable(true);//setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);//setWillNotDraw(false);//final ViewConfiguration configuration = ViewConfiguration.get(context);//mTouchSlop = configuration.getScaledTouchSlop();//mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();//mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();////}@Overrideprotected void onDraw(Canvas canvas) {if(init_left==0){init_left = this.getLeft();}if(width==0){width = this.getRight()-this.getLeft();height = this.getBottom() - this.getTop();lineInterval_y = (height*2/3)/MAX_Y;}diff_Y = this.getBottom() - this.getTop();// 设置画布颜色 也就是背景颜色canvas.drawColor(Color.TRANSPARENT);paint.setColor(Color.BLACK);canvas.drawText("绘制无规则几何图形喔!!!", 150, 30, paint);//绘制X坐标轴paint.setColor(color_xy);paint.setStrokeWidth(width_xy);canvas.drawLine(paddingBottom, diff_Y-paddingBottom, MAX_X+paddingBottom+MAX_X_Extend, diff_Y-paddingBottom, paint);//绘制Y坐标轴paint.setColor(color_xy);paint.setStrokeWidth(width_xy);canvas.drawLine(paddingBottom, diff_Y-paddingBottom, paddingBottom, this.getTop()+paddingTop, paint);//绘制标注线for (int i = 1; i <=lineNum; i++) {paint.setColor(line_color_y);paint.setStrokeWidth(line_width_y);canvas.drawLine(paddingBottom+lineInterval_x*i, diff_Y-paddingBottom, paddingBottom+lineInterval_x*i, this.getTop()+paddingTop, paint);//绘制标注线下的字体paint.setColor(Color.BLACK);paint.setTextSize(CalloutSize);paint.setTextScaleX(1.0f);//设置文本缩放paint.setTypeface(Typeface.SERIF);canvas.drawText("6日"+i+"时", paddingBottom+lineInterval_x*i-20, diff_Y-paddingBottom+20, paint);}//绘制参考线paint.setColor(Guides_line_color);paint.setStrokeWidth(width_xy);canvas.drawLine(paddingBottom, diff_Y-paddingBottom-lineInterval_y*Guides_line, MAX_X+paddingBottom+MAX_X_Extend, diff_Y-paddingBottom-lineInterval_y*Guides_line, paint);//绘制参考线标注paint.setColor(text_color);paint.setTextSize(textSize);paint.setTextScaleX(1.0f);//设置文本缩放paint.setTypeface(Typeface.SERIF);canvas.drawText(Guides_line+"", paddingBottom-30, diff_Y-paddingBottom-lineInterval_y*Guides_line+5, paint);//绘制折线for (int i = 1; i < nums.length; i++) {int temp = nums[i-1];if(temp==0){temp = 10;}int temp1 = nums[i];if(temp1==0){temp1 = 10;}paint.setColor(zhexian_color);paint.setStrokeWidth(zhexian_width);canvas.drawLine(paddingBottom + lineInterval_x * i,(diff_Y - paddingBottom) - (lineInterval_y * temp), paddingBottom+lineInterval_x*(i+1), (diff_Y-paddingBottom)-(lineInterval_y*temp1), paint);}//绘制折线点for (int i = 1; i <= nums.length; i++) {int temp = nums[i-1];if(temp==0){temp = 10;}// 绘制一个圆形canvas.drawBitmap(bmp, paddingBottom+lineInterval_x*i-8, (diff_Y-paddingBottom)-(lineInterval_y*temp)-5, paint);paint.setColor(text_color);paint.setTextSize(textSize);paint.setTextScaleX(1.0f);//设置文本缩放//设置字体样式 Typeface.DEFAULT:默认字体;Typeface.DEFAULT_BOLD:加粗字体;Typeface.MONOSPACE:monospace;Typeface.SANS_SERIF:sans;Typeface.SERIF:serifpaint.setTypeface(Typeface.SERIF);canvas.drawText(temp+"", paddingBottom+lineInterval_x*i-8, (diff_Y-paddingBottom)-(lineInterval_y*temp)-10, paint);}}@Overridepublic boolean onTouchEvent(MotionEvent event) {mov_x = (int) event.getX();switch (event.getAction()) {case MotionEvent.ACTION_UP:break;case MotionEvent.ACTION_DOWN:init_x = (int) event.getX();break;case MotionEvent.ACTION_MOVE:int diff_left = this.getLeft() + mov_x - init_x;int diff_right = MAX_X+paddingBottom+MAX_X_Extend-width;//向左滑动的最大距离if (diff_left < init_left) {if(diff_right>0&&diff_left>-diff_right){this.setLeft(diff_left);}else{this.setLeft(-diff_right);}}else{this.setLeft(init_left);}break;}return true;}}


更多相关文章

  1. Android(安卓)studio(AS) svg图片使用
  2. 〖Android〗我的ADT Eclipse定制
  3. android 颜色值 代码
  4. android水平ProgressBar颜色设置
  5. Android(安卓)progressBar代码设置进度条颜色、背景颜色以及圆角
  6. android 自定义控件之折线图自己写代码,不用jar包
  7. Android(安卓)中 shape 图形的使用
  8. Android实现按钮点击效果(第一次点击变色,第二次恢复)
  9. Android编程实现自定义ProgressBar样式示例(背景色及一级、二级

随机推荐

  1. Flutter环境的搭建
  2. Android(安卓)UI 之 获取组件或者元素的
  3. Android开发者指南(2) —— aapt
  4. ArcGIS在Android的应用
  5. Android(安卓)studio 分渠道打包,引用不同
  6. Gradle 多渠道打包的使用和错误分析
  7. 垮平台开发平台
  8. 搞定个性化输入提示--自定义AutoComplete
  9. [译] C++ 和 Android(安卓)本地 Activity
  10. Android学习笔记八:基本视图组件:CheckBox