android自定义带动画的柱状图控件

本文介绍了一位开发者原创的Android自定义柱状图控件,该控件具备动画效果和点击事件。作者通过继承View,重写ondraw方法,使用Canvas进行绘制。文章分享了实现思路,包括数据接收、刻度绘制、柱子布局、颜色随机分配以及动画效果的同步绘制等,并提供了部分代码。同时,作者还提到了回调代码的相关内容。
摘要由CSDN通过智能技术生成

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)
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值