赞
踩
1.也是突然的想法,写了个柱状图,然后觉得静态的太死板又加上去了动画和点击事件.
2.下面开始上菜!各位道友觉得不好呢,就多给点批评建议,然后都给点个赞,哈哈~(原创作品,转载请标明)
(上传图片竟然不能超过2M,,,,,,,,,,,,本来做了个gif,一看9M多.....重新做了一个,,,就录了上方的一半将就看吧)
3.思路:一般自定义控件都要继承View或者一个控件,然后重写它的ondraw方法,在这个方法里面使用canvas各种浪.
4.柱状图控件代码,直接上了(细节思路,中途遇到的问题,存在的缺陷,心得等都在下面哦):
package com.jeska.testrecyclerview.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; /** * Created by jeska on 2016/8/12. */ public class ZhuZhuangTu extends View { private Thread mThread; /**标题*/ private String title = "柱状图"; /***/ private Paint mPaint; private String emptyWarning = "没有数据"; private Context mContext; private ArrayList<HashMap<String,String>> mDataList; private ArrayList<HashMap<String,Float>> mLocationList; private int[] colors; /**整个空间的宽度*/ private int width; /**整个空间的高度*/ private int height; /**箭头宽度*/ private int arrowWidth = 10; /**坐标原点距离边界*/ private int distance = 30; /**底部辅助横线条数*/ private int horizontalLineCount = 10; /**柱形图的宽度*/ private float ZhuZhuangTuWidth; /**水平线间隔*/ private float horizontalLineInterval; /**柱状图间隔*/ private float ZhuZhuangTuInterval; private Random mRandom; /**用来做动画的可变值*/ // private float delta; /**用来做动画的线程while的flag*/ private boolean run = true; /**动画时长*/ private int millions = 6000; /**回调*/ private OnZhuZhuangTuClickListener mListener; /**设备密度*/ private float density; /**存放所有的delta*/ private float[] deltas; /**最大值出现的位置*/ private int maxValuePosition; public ZhuZhuangTu(Context context) { super(context); init(context); } public ZhuZhuangTu(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public ZhuZhuangTu(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { this.mContext = context; mPaint = new Paint(); mLocationList = new ArrayList<>(); run = true; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); setWillNotDraw(false); if (isDataMapEmpty()){ canvas.drawColor(Color.GRAY); mPaint.setColor(Color.MAGENTA); mPaint.setTextSize(40f); canvas.drawText(emptyWarning, width / 2 - mPaint.getTextSize()*emptyWarning.length()/2, height / 2-mPaint.getTextSize()/2, mPaint); }else{ mPaint.setColor(Color.BLACK); mPaint.setStrokeWidth(4); //绘制x轴 canvas.drawLine(distance * density, height - distance * density, width - distance * density, height - distance * density, mPaint); //绘制x轴箭头 canvas.drawLine(width - distance * density-arrowWidth * density, height - distance * density-arrowWidth * density, width - distance * density, height - distance * density, mPaint); canvas.drawLine(width - distance * density - arrowWidth * density, height - distance * density + arrowWidth * density, width - distance * density, height - distance * density, mPaint); //绘制y轴 canvas.drawLine(distance * density, distance * density, distance * density, height - distance * density, mPaint); //绘制y轴箭头 canvas.drawLine(distance * density - arrowWidth * density, distance * density + arrowWidth * density, distance * density, distance * density, mPaint); canvas.drawLine(distance * density + arrowWidth * density, distance * density + arrowWidth * density, distance * density, distance * density, mPaint)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。