Android自定义统计图(柱状图,折线图,饼状图)

来源:互联网 发布:合同矩阵和相似矩阵 编辑:程序博客网 时间:2024/05/17 01:49

最近由于项目需要,研究了一些统计图的做法,开始时,看了很多博文,大部分都是引用第三方的库,虽然简单,

易上手,但是功能太死板,有很多要求都是不能满足的,所以经过研究,自己使用View中的canvas重新绘图制作

统计图。首先上几张的效果图吧。

 

\\\

 

 

点击这里下载(0分下载)

 

一、demo的结构

一个activity中嵌套了三个fragment(v4),是用viewpager对页面进行滑动,下面是整个项目的结构:

\

二、核心代码

首先是MainActivity,这个demo中只使用到一个activity,现在一个activity中镶嵌多个fragment很火,

很多Android应用都在使用这个布局,例如微信,QQ等。这样做节省了空间上的浪费。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
packagecom.example.statisticalchart;
 
importandroid.support.v4.app.Fragment;
importandroid.support.v4.app.FragmentActivity;
importandroid.support.v4.app.FragmentPagerAdapter;
importandroid.support.v4.view.ViewPager;
importandroid.os.Bundle;
 
importjava.util.ArrayList;
importjava.util.List;
 
publicclass MainActivity extendsFragmentActivity {
 
    privateViewPager viewPager;
    privateList<fragment> fragments;
    privateFragmentPagerAdapter adapter;
    // 设置是否显示动画,为了防止在创建时就开启动画,用以下三个参数做了判断,只有当看到视图后才会显示动画
    publicstatic int flag1 = 2;
    publicstatic int flag2 = 1;
    publicstatic int flag3 = 1;
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
 
    privatevoid initView() {
        viewPager = (ViewPager) findViewById(R.id.record_viewpager);
        fragments = newArrayList<fragment>();
        RecordPager1 recordPager1 = newRecordPager1();
        RecordPager2 recordPager2 = newRecordPager2();
        RecordPager3 recordPager3 = newRecordPager3();
        fragments.add(recordPager1);
        fragments.add(recordPager2);
        fragments.add(recordPager3);
 
        adapter = newFragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            publicFragment getItem(intposition) {
                returnfragments.get(position);
            }
 
            @Override
            publicint getCount() {
                returnfragments.size();
            }
        };
        viewPager.setAdapter(adapter);
 
        viewPager.setOnPageChangeListener(newViewPager.OnPageChangeListener() {
            @Override
            publicvoid onPageScrolled(intposition, floatpositionOffset,
                    intpositionOffsetPixels) {
 
            }
 
            @Override
            publicvoid onPageSelected(intposition) {
                if(position == 0&& flag1 == 1) {
                    flag1 = 2;
                    fragments.get(0).onResume();
                    flag1 = 3;
                }
                if(position == 1&& flag2 == 1) {
                    flag2 = 2;
                    fragments.get(1).onResume();
                    flag2 = 3;
 
                }
                if(position == 2&& flag3 == 1) {
                    flag3 = 2;
                    fragments.get(2).onResume();
                    flag3 = 3;
                }
            }
 
            @Override
            publicvoid onPageScrollStateChanged(intstate) {
 
            }
        });
    }
 
}
</fragment></fragment>

接下来是这个项目中最主要的三个类:HistogramView、LineChartView、PinChart。这三个类继承View类,重新构图,分别画成了柱状图,折线图,饼状图,然后给出三个类的代码:

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
packagecom.example.statisticalchart;
 
importandroid.annotation.SuppressLint;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Paint.Align;
importandroid.graphics.Rect;
importandroid.os.Looper;
importandroid.util.AttributeSet;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.Transformation;
 
publicclass HistogramView extendsView {
 
    privatePaint xLinePaint;// 坐标轴 轴线 画笔:
    privatePaint hLinePaint;// 坐标轴水平内部 虚线画笔
    privatePaint titlePaint;// 绘制文本的画笔
    privatePaint paint;// 矩形画笔 柱状图的样式信息
    privateint[] progress = { 2000,5000,6000,8000,500,6000,9000};// 7
                                                                            // 条,显示各个柱状的数据
    privateint[] aniProgress;// 实现动画的值
    privatefinal int TRUE = 1;// 在柱状图上显示数字
    privateint[] text;// 设置点击事件,显示哪一条柱状的信息
    privateBitmap bitmap;
    // 坐标轴左侧的数标
    privateString[] ySteps;
    // 坐标轴底部的星期数
    privateString[] xWeeks;
    privateint flag;// 是否使用动画
 
    privateHistogramAnimation ani;
 
    publicHistogramView(Context context) {
        super(context);
        init();
    }
 
    publicHistogramView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
 
    privatevoid init() {
 
        ySteps = newString[] { "10k","7.5k","5k","2.5k","0"};
        xWeeks = newString[] { "周一","周二","周三","周四","周五","周六","周日"};
        text = newint[] { 0,0,0,0,0,0,0};
        aniProgress = newint[] { 0,0,0,0,0,0,0};
        ani = newHistogramAnimation();
        ani.setDuration(2000);
 
        xLinePaint = newPaint();
        hLinePaint = newPaint();
        titlePaint = newPaint();
        paint = newPaint();
 
        // 给画笔设置颜色
        xLinePaint.setColor(Color.DKGRAY);
        hLinePaint.setColor(Color.LTGRAY);
        titlePaint.setColor(Color.BLACK);
 
        // 加载画图
        bitmap = BitmapFactory
                .decodeResource(getResources(), R.drawable.column);
    }
 
    publicvoid start(intflag) {
        this.flag = flag;
        this.startAnimation(ani);
    }
 
    @SuppressLint("DrawAllocation")
    @Override
    protectedvoid onDraw(Canvas canvas) {
        super.onDraw(canvas);
 
        intwidth = getWidth();
        intheight = getHeight() - dp2px(50);
        // 绘制底部的线条
        canvas.drawLine(dp2px(30), height + dp2px(3), width - dp2px(30), height
                + dp2px(3), xLinePaint);
 
        intleftHeight = height - dp2px(5);// 左侧外周的 需要划分的高度:
 
        inthPerHeight = leftHeight / 4;// 分成四部分
 
        hLinePaint.setTextAlign(Align.CENTER);
        // 设置四条虚线
        for(inti = 0; i < 4; i++) {
            canvas.drawLine(dp2px(30), dp2px(10) + i * hPerHeight, width
                    - dp2px(30), dp2px(10) + i * hPerHeight, hLinePaint);
        }
 
        // 绘制 Y 周坐标
        titlePaint.setTextAlign(Align.RIGHT);
        titlePaint.setTextSize(sp2px(12));
        titlePaint.setAntiAlias(true);
        titlePaint.setStyle(Paint.Style.FILL);
        // 设置左部的数字
        for(inti = 0; i < ySteps.length; i++) {
            canvas.drawText(ySteps[i], dp2px(25), dp2px(13) + i * hPerHeight,
                    titlePaint);
        }
 
        // 绘制 X 周 做坐标
        intxAxisLength = width - dp2px(30);
        intcolumCount = xWeeks.length + 1;
        intstep = xAxisLength / columCount;
 
        // 设置底部的数字
        for(inti = 0; i < columCount - 1; i++) {
            // text, baseX, baseY, textPaint
            canvas.drawText(xWeeks[i], dp2px(25) + step * (i + 1), height
                    + dp2px(20), titlePaint);
        }
 
        // 绘制矩形
        if(aniProgress != null&& aniProgress.length > 0) {
            for(inti = 0; i < aniProgress.length; i++) {// 循环遍历将7条柱状图形画出来
                intvalue = aniProgress[i];
                paint.setAntiAlias(true);// 抗锯齿效果
                paint.setStyle(Paint.Style.FILL);
                paint.setTextSize(sp2px(15));// 字体大小
                paint.setColor(Color.parseColor("#6DCAEC"));// 字体颜色
                Rect rect = newRect();// 柱状图的形状
 
                rect.left = step * (i + 1);
                rect.right = dp2px(30) + step * (i + 1);
                intrh = (int) (leftHeight - leftHeight * (value / 10000.0));
                rect.top = rh + dp2px(10);
                rect.bottom = height;
 
                canvas.drawBitmap(bitmap,null, rect, paint);
                // 是否显示柱状图上方的数字
                if(this.text[i] == TRUE) {
                    canvas.drawText(value + "", dp2px(15) + step * (i + 1)
                            - dp2px(15), rh + dp2px(5), paint);
                }
 
            }
        }
 
    }
 
    privateint dp2px(intvalue) {
        floatv = getContext().getResources().getDisplayMetrics().density;
        return(int) (v * value + 0.5f);
    }
 
    privateint sp2px(intvalue) {
        floatv = getContext().getResources().getDisplayMetrics().scaledDensity;
        return(int) (v * value + 0.5f);
    }
 
    /**
     * 设置点击事件,是否显示数字
     */
    publicboolean onTouchEvent(MotionEvent event) {
        intstep = (getWidth() - dp2px(30)) / 8;
        intx = (int) event.getX();
        for(inti = 0; i < 7; i++) {
            if(x > (dp2px(15) + step * (i + 1) - dp2px(15))
                    && x < (dp2px(15) + step * (i + 1) + dp2px(15))) {
                text[i] = 1;
                for(intj = 0; j < 7; j++) {
                    if(i != j) {
                        text[j] = 0;
                    }
                }
                if(Looper.getMainLooper() == Looper.myLooper()) {
                    invalidate();
                }else{
                    postInvalidate();
                }
            }
        }
        returnsuper.onTouchEvent(event);
    }
 
    /**
     * 集成animation的一个动画类
     *
     * @author 李垭超
     */
    privateclass HistogramAnimation extendsAnimation {
        protectedvoid applyTransformation(floatinterpolatedTime,
                Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if(interpolatedTime < 1.0f && flag == 2) {
                for(inti = 0; i < aniProgress.length; i++) {
                    aniProgress[i] = (int) (progress[i] * interpolatedTime);
                }
            }else{
                for(inti = 0; i < aniProgress.length; i++) {
                    aniProgress[i] = progress[i];
                }
            }
            invalidate();
        }
    }
 
}


 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
packagecom.example.statisticalchart;
 
importandroid.annotation.SuppressLint;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.graphics.BitmapFactory;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Paint.Align;
importandroid.graphics.Path;
importandroid.graphics.PorterDuff;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Rect;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.Transformation;
 
publicclass LineChartView extendsView {
 
    privatePaint rectPaint;// 设置左侧为白色,显示数表
    privatePaint hLinePaint;// 坐标轴水平内部 虚线画笔
    privatePaint titlePaint;// 绘制文本的画笔
    privatePaint linePaint;
    privatePaint paint;// 矩形画笔 柱状图的样式信息
    privateint[] text;// 折线的转折点
    intx, y, preX, preY;
    // 坐标轴左侧的数标
    privateBitmap mBitmap;
    // 坐标轴底部的星期数
    privateString[] str = { "62","72","82","92","102","112","122",
            "132","142"};
 
    privateHistogramAnimation ani;
    privateint flag;
 
    publicLineChartView(Context context) {
        super(context);
        init(context,null);
    }
 
    publicLineChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init(context, attrs);
    }
 
    privatevoid init(Context context, AttributeSet attrs) {
 
        text = newint[] { 6,5,5,4,5,3,2,3,1,1};
 
        ani = newHistogramAnimation();
        ani.setDuration(4000);
 
        rectPaint = newPaint();
        titlePaint = newPaint();
 
    }
 
    @Override
    protectedvoid onDraw(Canvas canvas) {
        super.onDraw(canvas);
        linePaint = newPaint();
 
        //
        titlePaint.setAntiAlias(true);
        Rect bundle1 = newRect();
 
        Rect bundle2 = newRect();
        hLinePaint = newPaint();
 
        intperWidth = getWidth() / 10;// 将宽度分为10部分
        inthPerHeight = getHeight() / 10;// 将高度分为10部分
        rectPaint.setColor(Color.WHITE);
 
        canvas.drawRect(0,0, dp2px(30), getHeight(), rectPaint);// 画一块白色区域
 
        Path path = newPath();// 折线图的路径
        mBitmap = Bitmap.createBitmap(getWidth(), getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas mCanvas = newCanvas(mBitmap);
 
        for(inti = 0; i < 10; i++) {// 画x线,并在左侧显示相应的数值
            hLinePaint.setTextAlign(Align.CENTER);
            hLinePaint.setColor(Color.WHITE);
            y = i * hPerHeight;
            if(i == 2) {
                hLinePaint.setStrokeWidth(4);
                for(intj = 0; j < 10; j++) {
                    canvas.drawLine(dp2px(30) + j * perWidth, y, dp2px(28)
                            + (j + 1) * perWidth, y, hLinePaint);
                }
                titlePaint.setTextSize(sp2px(20));
                titlePaint.getTextBounds(str[i - 1],0, str[i - 1].length(),
                        bundle1);
                canvas.drawText(str[i - 1], dp2px(25) - bundle1.width(), i
                        * hPerHeight + (bundle1.height() / 2), titlePaint);
            }else{
                hLinePaint.setStrokeWidth(1);
                canvas.drawLine(dp2px(30), y, getWidth(), y, hLinePaint);
                if(i != 0) {
                    titlePaint.setTextSize(sp2px(15));
                    titlePaint.getTextBounds(str[i - 1],0,
                            str[i - 1].length(), bundle2);
                    canvas.drawText(str[i - 1], dp2px(25) - bundle2.width(), i
                            * hPerHeight + (bundle2.height() / 2), titlePaint);
                }
            }
 
            x = i * perWidth + dp2px(30);
            if(i == 0) {
                path.moveTo(x, text[i] * hPerHeight);
            }else{
                path.lineTo(x, text[i] * hPerHeight);
            }
            linePaint.setColor(Color.parseColor("#bb2222"));
            linePaint.setAntiAlias(true);
 
            paint = newPaint();
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(dp2px(1));
            if(i != 0) {
                mCanvas.drawCircle(x, text[i] * hPerHeight, dp2px(3), linePaint);
            }
            paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.DST_OVER));
            mCanvas.drawPath(path, paint);
        }
 
        paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        paint.setStyle(Paint.Style.FILL);
 
        mCanvas.drawRect(preX + dp2px(30),0, getWidth(), getHeight(), paint);
        canvas.drawBitmap(mBitmap,0,0,null);
        // Log.i("tag", "onDraw()1111");
    }
 
    privateint dp2px(intvalue) {
        floatv = getContext().getResources().getDisplayMetrics().density;
        return(int) (v * value + 0.5f);
    }
 
    privateint sp2px(intvalue) {
        floatv = getContext().getResources().getDisplayMetrics().scaledDensity;
        return(int) (v * value + 0.5f);
    }
 
    publicvoid start(intflag) {
        startAnimation(ani);
        this.flag = flag;
    }
 
    /**
     * 集成animation的一个动画类
     *
     * @author
     */
    privateclass HistogramAnimation extendsAnimation {
        @Override
        protectedvoid applyTransformation(floatinterpolatedTime,
                Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if(interpolatedTime < 1.0f && flag == 2) {
                preX = (int) ((getWidth() - dp2px(30)) * interpolatedTime);
            }else{
 
                preX = getWidth();
 
            }
            invalidate();
        }
    }
 
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
packagecom.example.statisticalchart;
 
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.PorterDuff;
importandroid.graphics.PorterDuffXfermode;
importandroid.graphics.Rect;
importandroid.graphics.RectF;
importandroid.util.AttributeSet;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.animation.Animation;
importandroid.view.animation.Transformation;
 
publicclass PinChart extendsView {
 
    staticCanvas c;
    privatePaint[] mPaints;
    privateRectF mBigOval;
    float[] mSweep = { 0,0,0,0,0,0,0,0,0,0};
    privateint preWidth;
    privatemAnimation ani;
    privateint flag;
    privateint centerX;
    privateint centerY;
    intvalueX;
    intvalueY;
 
    publicstatic float[] humidity = { 110,60,50,50,40,30,10,10};
    privateString str[] = { "数据24%","数据19%","数据21%","其他18%","数据3%",
            "数据3%","数据4%","数据6%"};
 
    privatefinal String color[] = { "#2cbae7","#ffa500","#ff5b3b",
            "#9fa0a4","#6a71e5","#f83f5d","#64a300","#64ef85"};
 
    publicPinChart(Context context) {
        super(context);
        initView();
    }
 
    publicPinChart(Context context, AttributeSet atr) {
        super(context, atr);
        initView();
    }
 
    privatevoid initView() {
        ani = newmAnimation();
        ani.setDuration(2000);
    }
 
    @Override
    protectedvoid onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);// 设置背景颜色(透明)
        mPaints = newPaint[humidity.length];
 
        for(inti = 0; i < humidity.length; i++) {
            mPaints[i] = newPaint();
            mPaints[i].setAntiAlias(true);
            mPaints[i].setStyle(Paint.Style.FILL);
            mPaints[i].setColor(Color.parseColor(color[i]));
        }
        intcicleWidth = getWidth() - dp2px(60);
        centerX = getWidth() / 2;
        centerY = dp2px(10) + cicleWidth / 2;
        preWidth = (getWidth() - dp2px(40)) / 4;
        inthalf = getWidth() / 2;
 
        mBigOval = newRectF();// 饼图的四周边界
        mBigOval.top = dp2px(10);
        mBigOval.left = half - cicleWidth / 2;
        mBigOval.bottom = dp2px(10) + cicleWidth;
        mBigOval.right = half + cicleWidth / 2;
 
        floatstart = -180;
        Rect bounds = newRect();
        for(inti = 0; i < humidity.length; i++) {
            canvas.drawArc(mBigOval, start, mSweep[i], true, mPaints[i]);
            if(humidity[i] > 45) {
                mPaints[i].setXfermode(newPorterDuffXfermode(
                        PorterDuff.Mode.SRC_OVER));
                mPaints[i].setAntiAlias(true);
                mPaints[i].setColor(Color.WHITE);
                mPaints[i].getTextBounds(str[i],0, str[i].length(), bounds);
                mPaints[i].setTextSize(sp2px(15));
                measureText(start + 180, humidity[i], cicleWidth / 3, i);
                canvas.drawText(str[i], valueX - mPaints[i].measureText(str[i])
                        /2, valueY + bounds.height() / 2, mPaints[i]);
            }
            start += humidity[i];
            intj = 1;
            intk;
            if(i < 4) {
                j = 0;
                k = i;
            }else{
                j = 1;
                k = i - 4;
            }
            mPaints[i] = newPaint();
            mPaints[i].setAntiAlias(true);
            mPaints[i].setStyle(Paint.Style.FILL);
            mPaints[i].setColor(Color.parseColor(color[i]));
            canvas.drawRect(newRectF(dp2px(20) + preWidth * k, cicleWidth
                    + dp2px(j * 30+ 20), dp2px(20) + preWidth * (k + 1),
                    cicleWidth + dp2px(50+ j * 30)), mPaints[i]);
            mPaints[i].setXfermode(newPorterDuffXfermode(
                    PorterDuff.Mode.SRC_OVER));
            mPaints[i].setAntiAlias(true);
            mPaints[i].setColor(Color.WHITE);
            mPaints[i].getTextBounds(str[i],0, str[i].length(), bounds);
            mPaints[i].setTextSize(sp2px(15));
            canvas.drawText(str[i], dp2px(20) + preWidth * k + preWidth / 2
                    - mPaints[i].measureText(str[i]) / 2, cicleWidth
                    + dp2px(j * 30+ 20)
                    + (dp2px(30) / 2+ bounds.height() / 2), mPaints[i]);
        }
    }
 
    /**
     * 显示相应区域字开始的x,y坐标
     *
     * @param start
     * @param angle
     * @param radius
     * @param i
     */
    privatevoid measureText(floatstart, floatangle, intradius, inti) {
        floattemp = start + (angle / 2);
 
        if(temp < 90) {
            valueX = (int) (centerX - Math.abs(radius
                    * Math.sin((temp / 180) * Math.PI)));
            valueY = (int) (centerY - Math.abs(radius
                    * Math.cos((temp / 180) * Math.PI)));
        }elseif (temp > 90&& temp < 180) {
            temp = 180- temp;
            valueX = centerX
                    + (int) Math
                            .abs((radius * Math.cos((temp / 180) * Math.PI)));
            valueY = centerY
                    - (int) Math
                            .abs((radius * Math.sin((temp / 180) * Math.PI)));
        }elseif (temp > 180&& temp < 270) {
            temp = temp - 180;
            valueX = centerX
                    + (int) Math
                            .abs((radius * Math.cos((temp / 180) * Math.PI)));
            valueY = centerY
                    + (int) Math
                            .abs((radius * Math.sin((temp / 180) * Math.PI)));
        }else{
            temp = 360- temp;
            valueX = centerX
                    - (int) Math
                            .abs((radius * Math.cos((temp / 180) * Math.PI)));
            valueY = centerY
                    + (int) Math
                            .abs((radius * Math.sin((temp / 180) * Math.PI)));
        }
 
    }
 
    privateint sp2px(intvalue) {
        floatv = getResources().getDisplayMetrics().scaledDensity;
        return(int) (value * v + 0.5f);
    }
 
    privateint dp2px(intvalue) {
        floatv = getResources().getDisplayMetrics().density;
        return(int) (value * v + 0.5f);
    }
 
    publicvoid start(intflag) {
        startAnimation(ani);
        this.flag = flag;
    }
 
    classmAnimation extendsAnimation {
        @Override
        protectedvoid applyTransformation(floatinterpolatedTime,
                Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if(interpolatedTime < 1.0f && flag == 2) {
                for(inti = 0; i < humidity.length; i++) {
                    mSweep[i] = humidity[i] * interpolatedTime;
                }
            }elseif (flag == 1) {
                for(inti = 0; i < humidity.length; i++) {
                    mSweep[i] = 0;
                }
            }else{
                for(inti = 0; i < humidity.length; i++) {
                    mSweep[i] = humidity[i];
                }
            }
            invalidate();
        }
    }
 
}

以上都是核心代码,并不是全部的代码,起他部分的代码就不贴出来了,感兴趣的朋友可以下载来研究研究。

 

0 0
原创粉丝点击