版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android如何实现带动画柱状图
/upload/information/20200623/125/126985.jpg/upload/information/20200623/125/126986.gifpackage
com.lixiaodaoaaa.view.pieview;
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Paint;
import
android.graphics.RectF;
import
android.support.annotation.Nullable;
import
android.util.AttributeSet;
import
android.view.View;
import
com.gcssloop.graphics.R;
import
com.lixiaodaoaaa.uitls.DensityUtils;
/**************************************
*
***
/lixiaodaoaaa
**
*
***
create
at
2017/5/18
23:45
****
*
*******
by:lixiaodaoaaa
**********
**************************************/
public
class
PColumn
extends
View
{
int
MAX
=
100;//最大
int
corner
=
40;
int
data
=
0;//显示的数
int
tempData
=
0;
int
textPadding
=
20;
Paint
mPaint;
int
mColor;
Context
mContext;
public
PColumn(Context
context)
{
super(context);
mContext
=
context;
}
public
PColumn(Context
context,
@Nullable
AttributeSet
attrs)
{
super(context,
attrs);
mContext
=
context;
initPaint();
}
public
PColumn(Context
context,
@Nullable
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
mContext
=
context;
initPaint();
}
private
void
initPaint()
{
mPaint
=
new
Paint();
mPaint.setAntiAlias(true);
mColor
=
mContext.getResources().getColor(R.color.colorPrimary);
mPaint.setColor(mColor);
}
@Override
public
void
draw(Canvas
canvas)
{
super.draw(canvas);
if
(data
==
0)
{
mPaint.setTextSize(getWidth()
/
2);
RectF
oval3
=
new
RectF(0,
getHeight()
-
DensityUtils.pxTodip(mContext,
20),
getWidth(),
getHeight());//
设置个新的长方形
canvas.drawRoundRect(oval3,
DensityUtils.pxTodip(mContext,
corner),
DensityUtils.pxTodip(mContext,
corner),
mPaint);
canvas.drawText("0",
getWidth()
*
0.5f
-
mPaint.measureText("0")
*
0.5f,
getHeight()
-
DensityUtils.pxTodip(mContext,
20)
-
2
*
DensityUtils.pxTodip(mContext,
textPadding),
mPaint);
return;
}
//防止数值很大的的时候,动画时间过长
int
step
=
data
/
100
+
1;
if
(tempData
<
data
-
step)
{
tempData
=
tempData
+
step;
}
else
{
tempData
=
data;
}
//画圆角矩形
String
S
=
tempData
+
"";
//一个字和两,三个字的字号相同
if
(S.length()
<
4)
{
mPaint.setTextSize(getWidth()
/
2);
}
else
{
mPaint.setTextSize(getWidth()
/
(S.length()
-
1));
}
float
textH
=
mPaint.ascent()
+
mPaint.descent();
float
MaxH
=
getHeight()
-
textH
-
2
*
DensityUtils.pxTodip(mContext,
textPadding);
//圆角矩形的实际高度
float
realH
=
MaxH
/
MAX
*
tempData;
RectF
oval3
=
new
RectF(0,
getHeight()
-
realH,
getWidth(),
getHeight());//
设置个新的长方形
canvas.drawRoundRect(oval3,
DensityUtils.pxTodip(mContext,
corner),
DensityUtils.pxTodip(mContext,
corner),
mPaint);
//写数字
canvas.drawText(S,
getWidth()
*
0.5f
-
mPaint.measureText(S)
*
0.5f,
getHeight()
-
realH
-
2
*
DensityUtils.pxTodip(mContext,
textPadding),
mPaint);
if
(tempData
!=
data)
{
postInvalidate();
}
}
public
void
setData(int
data,
int
MAX)
{
this.data
=
data;
tempData
=
0;
this.MAX
=
MAX;
postInvalidate();
}
}/*
*
Copyright
2016
GcsSloop
*
*
Licensed
under
the
Apache
License,
Version
2.0
(the
"License");
*
you
may
not
use
this
file
except
in
compliance
with
the
License.
*
You
may
obtain
a
copy
of
the
License
at
*
*
/licenses/LICENSE-2.0
*
*
Unless
required
by
applicable
law
or
agreed
to
in
writing,
software
*
distributed
under
the
License
is
distributed
on
an
"AS
IS"
BASIS,
*
WITHOUT
WARRANTIES
OR
CONDITIONS
OF
ANY
KIND,
either
express
or
implied.
*
See
the
License
for
the
specific
language
governing
permissions
and
*
limitations
under
the
License.
*
*
Last
modified
2016-10-02
00:22:33
*
*/
package
com.lixiaodaoaaa.graphics;
import
android.os.Bundle;
import
android.support.v7.app.AppCompatActivity;
import
com.gcssloop.graphics.R;
import
com.lixiaodaoaaa.view.pieview.PColumn;
public
class
MainActivity
extends
AppCompatActivity
{
private
PColumn
column_one;
private
PColumn
column_two;
private
PColumn
column_three;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initAllViews();
}
private
void
initAllViews()
{
column_one
=
(PColumn)
findViewById(R.id.column_one);
column_two
=
(PColumn)
findViewById(R.id.column_two);
column_three
=
(PColumn)
findViewById(R.id.column_three);
column_one.setData(0,
100);
column_two.setData(30,
100);
column_three.setData(40,
100);
}
}<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lixiaodaoaaa.graphics.MainActivity"
>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"/>
<com.lixiaodaoaaa.view.pieview.PColumn
android:id="@+id/column_one"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_wei
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB 11118-2026液压油(L-HL、L-HM、L-HV、L-HS、L-HG)
- 数学东北三四市(哈尔滨、长春、沈阳、大连)2026年高考第一次模拟考试(四市二模)(4.7-4.9)
- 华东交通大学《护理管理》2025-2026学年期末试卷
- 福建商学院《临床医学概论》2025-2026学年期末试卷
- 宣城职业技术学院《运动控制系统》2025-2026学年期末试卷
- 泉州信息工程学院《现代沟通技巧》2025-2026学年期末试卷
- 莆田学院《口腔修复学》2025-2026学年期末试卷
- 仰恩大学《税法》2025-2026学年期末试卷
- 扬州大学广陵学院《法理学》2025-2026学年期末试卷
- 南昌理工学院《计量经济学实验课》2025-2026学年期末试卷
- 2026年及未来5年市场数据中国纸质文具行业市场发展现状及未来发展趋势预测报告
- 2025年四川省省级机关公开遴选考试真题(附答案)
- 2026年统编版二年级道德与法治下册每课教学设计
- 21《杨氏之子》第一课时公开课一等奖创新教学设计
- 2026河南省烟草专卖局(公司)高校毕业生招聘190人备考题库及一套完整答案详解
- pe线管施工方案(3篇)
- 上海上海市农业科学院工作人员招聘35人(2025年第一批)笔试历年参考题库附带答案详解(5卷)
- 2025 年我国肉鸡产业形势分析、问题挑战与对策建议
- 2026及未来5年中国工业旅游行业市场现状调查及未来趋势研判报告
- 企业管理 华为会议接待全流程手册SOP
- 2025年浙江嘉兴大学三位一体笔试及答案
评论
0/150
提交评论