Android移动应用开发教程(微课版) 课件 单元七 Android常用框架_第1页
Android移动应用开发教程(微课版) 课件 单元七 Android常用框架_第2页
Android移动应用开发教程(微课版) 课件 单元七 Android常用框架_第3页
Android移动应用开发教程(微课版) 课件 单元七 Android常用框架_第4页
Android移动应用开发教程(微课版) 课件 单元七 Android常用框架_第5页
已阅读5页,还剩44页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

单元七

Android常用框架学习导读

框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法。随着互联网行业的不断发展,产品项目中的模块越来越多,现在GitHub上流行的开源库极大地节省了开发人员的开发时间,很多企业和个人都在GitHub上开源自己的项目。这些开源库或框架可以通过GitHub快速查询并集成到正在开发的项目上,如此便捷的技术极大地节省了开发时间。本单元通过介绍Android常用框架,帮助读者了解如何下载框架、使用框架,从而提高开发效率。学习目标知识目标:1.了解ButterKnife、MPAndroidChart、SmartRefreshLayout等框架;2.掌握框架引入方式和框架的基本应用方式;3.熟悉框架提供的相关类。技能目标:1.能够利用ButterKnife框架实现视图和事件绑定;2.能够利用MPAndroidChart框架绘制折线图、柱状图、动态折线图等;3.能够利用SmartRefreshLayout框架实现下拉数据刷新。素养目标:1.培养自我学习、自我探究的能力,在学习和实践中完善自我、丰富视野;2.培养踏实、勤奋的学习态度,在学习实践中锻炼独立思考的能力。思维导图7.1.1导入依赖和初始化ButterKnife1.导入依赖implementation'com.jakewharton:butterknife:10.1.0'annotationProcessor'com.jakewharton:butterknife-compiler:10.1.0'2.初始化ButterKnife框架

//初始化ButterKnife框架

ButterKnife.bind(this);Unbinderunbinder;unbinder=ButterKnife.bind(this,view);unbinder.unbind();7.1.2注解类型7.1.2注解类型7.1.3案例

通过绑定按钮、图片框、文本框组件和数组资源、图片资源及其按钮事件,来实现ButterKnife框架的基本应用。1.学会添加ButterKnife框架依赖2.初始化ButterKnife框架3.学会绑定视图组建4.学会绑定资源5.学会绑定事件7.1.3案例

通过绑定按钮、图片框、文本框组件和数组资源、图片资源及其按钮事件,来实现ButterKnife框架的基本应用。1.创建数组资源2.主页面布局文件设计3.MainActivity设计7.2MPAndroidChart框架

MPAndroidChart框架是Android一个强大且支持拖动和缩放、容易使用的开源图表库。它支持折线图、柱状图、饼图、散点图、气泡图和蜘蛛网状图等图表,其中柱状图支持3D效果。

其框架可以在Android和iOS平台上使用。使用MPAndroidChart框架绘制折线图、柱状图、饼图之前需要引入该框架。添加依赖implementation'com.github.PhilJay:MPAndroidChart:v3.1.0'。添加maven{url"https://jitpack.io"}语句7.2.1折线图绘制

LineChart类用于绘制折线图,位于com.github.mikephil.charting.charts包下,绘制折线图的相关类也都在此包中。

绘制基本折线图涉及Entry、LineDataSet、ILineDataSet、LineData、LineChart等类。7.2.1折线图绘制

3.绘制基本折线图步骤:1.在布局文件创建折线图组建2.创建折线点集合3.创建LineDataSet对象4.创建LineData对象,将数据放入图表控件中5.修饰折线图6.设置图例7.设置描述8.设置X、Y轴标识7.2.1折线图绘制

3.绘制基本折线图

(1)布局文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.github.mikephil.charting.charts.LineChart android:id="@+id/lineChart" android:layout_width="match_parent" android:layout_height="300dp"/></LinearLayout>7.2.1折线图绘制

(2)创建12个Entry对象,并放到集合中List<Entry>entries=newArrayList<>();Entryentry1=newEntry(1,-13.3f);entries.add(entry1);Entryentry2=newEntry(2,-5.5f);entries.add(entry2);Entryentry3=newEntry(3,2.4f);entries.add(entry3);Entryentry4=newEntry(4,16.7f);entries.add(entry4);Entryentry5=newEntry(5,20.1f);entries.add(entry5);Entryentry6=newEntry(6,24.4f);entries.add(entry6);Entryentry7=newEntry(7,30.2f);entries.add(entry7);Entryentry8=newEntry(8,23.6f);entries.add(entry8);Entryentry9=newEntry(9,18.7f);entries.add(entry9);Entryentry10=newEntry(10,4.6f);entries.add(entry10);Entryentry11=newEntry(11,-6.7f);entries.add(entry11);Entryentry12=newEntry(12,-24.4f);entries.add(entry12);7.2.1折线图绘制(3)创建LineDataSet对象LineDataSetlineDataSet=newLineDataSet(entries,"温度");(4)创建LineData对象,将数据放入图表控件中//获得LineChart对象LineChartlineChart=(LineChart)this.findViewById(R.id.lineChart);//创建LineDataSet对象LineDataSetlineDataSet=newLineDataSet(entries,"温度");//创建LineData对象LineDatadata=newLineData(lineDataSet);//设置LineChart的数据源lineChart.setData(data);7.2.1折线图绘制4.修饰折线图//设置折线的颜色lineDataSet.setColor(Color.RED);//设置折线的宽度lineDataSet.setLineWidth(2f);//设置折线点的颜色lineDataSet.setCircleColor(Color.RED);//设置折线点圆的半径lineDataSet.setCircleRadius(3f);//设置折线点的圆是实心还是空心的,设置为false表示是实心圆lineDataSet.setDrawCircleHole(false);//设置折线点处文字的大小lineDataSet.setValueTextSize(15f);7.2.1折线图绘制5.设置图例//获得图例对象Legendlegend=lineChart.getLegend();//设置图例是短线legend.setForm(Legend.LegendForm.LINE);//设置图例的文字大小legend.setTextSize(15f);//设置图例的显示方向为垂直legend.setOrientation(Legend.LegendOrientation.VERTICAL);7.2.1折线图绘制6.设置描述//获得描述对象Descriptiondescription=lineChart.getDescription();//设置描述的位置description.setPosition(1200,80);//设置描述的内容description.setText("2021年某地区温度统计");//设置描述的文字大小description.setTextSize(25f);7.2.1折线图绘制7.设置x轴标识//获得x轴对象XAxisxAxis=lineChart.getXAxis();//设置x轴在折线图的底部xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置x轴坐标之间的最小间隔xAxis.setGranularity(1);//去掉x轴的延长线xAxis.setDrawGridLines(false);//设置x轴显示标签的数量xAxis.setLabelCount(12);finalString[]months={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};7.2.1折线图绘制7.设置x轴标识

//设置x轴值标签,显示12个月的月份xAxis.setValueFormatter(newIAxisValueFormatter(){@OverridepublicStringgetFormattedValue(floatvalue,AxisBaseaxis){intindex=(int)value;returnmonths[index-1];}});7.2.1折线图绘制8.设置y轴标识

//获得左y轴对象YAxisleftYAxis=lineChart.getAxisLeft();//获得右y轴对象YAxisrightYAxis=lineChart.getAxisRight();//设置右y轴不可见rightYAxis.setEnabled(false);//去掉左y轴延长线leftYAxis.setDrawGridLines(false);7.2.1折线图绘制8.设置y轴标识

//设置左y轴显示标签的格式leftYAxis.setValueFormatter(newIAxisValueFormatter(){@OverridepublicStringgetFormattedValue(floatvalue,AxisBaseaxis){Stringy_value=(int)value+"℃";returny_value;}});7.2.2柱状图绘制MPAndroidChart框架提供了绘制柱状图的BarChart类,提供了BarEntry、IBarDataSet、BarDataSet、BarData等类。7.2.2柱状图绘制MPAndroidChart框架提供了绘制柱状图的BarChart类,提供了BarEntry、IBarDataSet、BarDataSet、BarData等类。7.2.2柱状图绘制(1)主页面布局文件设计<com.github.mikephil.charting.charts.BarChart

android:id="@+id/barChart"

android:layout_width="match_parent"

android:layout_height="300dp"/>7.2.2柱状图绘制(2)MainActivity设计//设置第一条柱状图数据List<BarEntry>barEntryList1=newArrayList<BarEntry>();barEntryList1.add(newBarEntry(0,390f));barEntryList1.add(newBarEntry(1,1100f));barEntryList1.add(newBarEntry(2,900f));barEntryList1.add(newBarEntry(3,700f));barEntryList1.add(newBarEntry(4,300f));//把第一条柱状图数据放到BarDataSet对象中BarDataSetbarDataSet1=newBarDataSet(barEntryList1,"蔬菜");//设置第一条柱状图barDataSet1.setColor(Color.RED);barDataSet1.setValueTextColor(Color.RED);barDataSet1.setValueTextSize(15f);7.2.2柱状图绘制(2)MainActivity设计//设置数据值,显示为数值+单位,如390斤barDataSet1.setValueFormatter(newValueFormatter(){@Override

publicStringgetBarLabel(BarEntrybarEntry){

Stringvalue=(int)barEntry.getY()+"斤";

returnvalue;

}});7.2.2柱状图绘制(2)MainActivity设计//设置第二条柱状图数据List<BarEntry>barEntryList2=newArrayList<BarEntry>();barEntryList2.add(newBarEntry(0.3f,200f));barEntryList2.add(newBarEntry(1.3f,400f));barEntryList2.add(newBarEntry(2.3f,410f));barEntryList2.add(newBarEntry(3.3f,420f));barEntryList2.add(newBarEntry(4.3f,490f));//把第二条柱状图数据放到BarDataSet对象中BarDataSetbarDataSet2=newBarDataSet(barEntryList2,"水果");barDataSet2.setColor(Color.BLUE);barDataSet2.setValueTextColor(Color.BLUE);barDataSet2.setValueTextSize(15f);7.2.2柱状图绘制(2)MainActivity设计//设置第二条柱状图不显示值barDataSet2.setDrawValues(false);//定义一个List集合,集合的泛型必须是IbarDataSet类型List<IBarDataSet>list=newArrayList<IBarDataSet>();//把两个BarDataSet对象添加到List集合中list.add(barDataSet1);list.add(barDataSet2);//把List对象放到BarData对象中BarDatabarData=newBarData(list);//设置柱状图的宽度barData.setBarWidth(0.3f);//设置BarChart组件的数据barChart.setData(barData);XAxisxAxis=barChart.getXAxis();7.2.2柱状图绘制(2)MainActivity设计//设置x轴在柱状图的底部xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置x轴显示的标签数量xAxis.setLabelCount(5);finalString[]labelName={"一月","二月","三月","四月","五月"};//设置x轴显示值的格式xAxis.setValueFormatter(newIAxisValueFormatter(){@Override

publicStringgetFormattedValue(floatvalue,AxisBaseaxis){

returnlabelName[(int)value];

}});}}7.2.3饼图绘制

饼图方式与折线图、柱状图两个图形绘制方式一致。MPAndroidChart框架提供了绘制饼图的PieChart类,同时也提供了PieEntry、IPieDataSet、PieDataSet、PieData等类用于绘制饼图。7.2.3饼图绘制

(1)主页面布局文件设计

<com.github.mikephil.charting.charts.PieChart

android:id="@+id/pieChart"

android:layout_width="match_parent"

android:layout_height="match_parent"/>7.2.3饼图绘制(2)MainActivity设计//设置饼图数据List<PieEntry>list=newArrayList<PieEntry>();list.add(newPieEntry(24,"优秀"));list.add(newPieEntry(30,"良好"));list.add(newPieEntry(45,"一般"));list.add(newPieEntry(52,"及格"));list.add(newPieEntry(45,"不及格"));PieDataSetdataSet=newPieDataSet(list,"");7.2.3饼图绘制(2)MainActivity设计//设置颜色ArrayList<Integer>colors=newArrayList<Integer>();for(intc:ColorTemplate.VORDIPLOM_COLORS)colors.add(c);//给饼图设置颜色dataSet.setColors(colors);//设置数据集合PieDatapieData=newPieData(dataSet);//设置数值字号大小pieData.setValueTextSize(20f);//设置数值不显示//pieData.setDrawValues(false);pieData.setValueTextColor(Color.BLACK);7.2.3饼图绘制(2)MainActivity设计//设置数值带%pieData.setValueFormatter(newPercentFormatter(pieChart));//设置数值按照百分比显示pieChart.setUsePercentValues(true);pieChart.setData(pieData);//设置中心圆的半径pieChart.setHoleRadius(10f);//设置透明圆的半径pieChart.setTransparentCircleRadius(20f);7.2.3饼图绘制(2)MainActivity设计//设置数值带%pieData.setValueFormatter(newPercentFormatter(pieChart));//设置数值按照百分比显示pieChart.setUsePercentValues(true);pieChart.setData(pieData);//设置中心圆的半径pieChart.setHoleRadius(10f);//设置透明圆的半径pieChart.setTransparentCircleRadius(20f);//设置文字标签的颜色pieChart.setEntryLabelColor(Color.BLUE);//设置文字标签的文字大小pieChart.setEntryLabelTextSize(20f);7.2.3饼图绘制(2)MainActivity设计//设置图例Legendlegend=pieChart.getLegend();legend.setTextSize(22f);legend.setFormSize(20f);legend.setOrientation(Legend.LegendOrientation.VERTICAL);legend.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);//设置图表描述Descriptiondescription=pieChart.getDescription();description.setText("Android应用开发成绩分析");description.setTextSize(22f);7.2.4动态折线图绘制1.绘制动态折线图的思路(1)从LineChart对象中获得LineData对象。(2)从LineData对象中获得实时变化的LineDataSet对象。(3)按照下一个坐标点生成Entry(x,y)对象。(4)把生成的Entry对象添加到LineDataSet对象中。(5)通知LineData对象和LineChart对象数据集合已发生变化。(6)刷新LineChart组件。7.2.4动态折线图绘制7.2.4动态折线图绘制//获得LineChart组件所绑定的LineData对象LineDatalineData=lineChart.getLineData();//通过索引从lineData中获得第一个ILineDataSet对象ILineDataSetlineDataSet=lineData.getDataSetByIndex(0);//生成(x,y)数据,x坐标值是x值自增1x++;//随机生成y坐标值floaty=(float)Math.random()*60;Entryentry=newEntry(x,y);//添加数据lineDataSet.addEntry(entry);//移除第一个节点数据,始终保持10条数据lineDataSet.removeFirst();//更新数据集合lineData.notifyDataChanged();lineChart.notifyDataSetChanged();//重新绘制折线图lineChart.invalidate();7.3SmartRefreshLayoutSmartRefreshLayout是一个第三方开发的强大、稳定、成熟的智能刷新布局框架,其集成了各种“炫酷”、实用、美观的Header(头)和Footer(脚)。该框架继承的是ViewGroup,而不是FrameLayout或LinearLayout,因而提高了性能。可支持多层嵌套的视图结构(如LinearLayout、FrameLayout等);支持所有的视图(如AbsListView、RecyclerView、WebView等);支持自定义Header和Footer;支持列表视图的无缝同步滚动和协调器布局的嵌套滚动;支持自动刷新、自动上拉加载;支持自定义回弹动画的插值器,可以实现各种“炫酷”的动画效果;支持设置主题来适配任何场景的App,不会出现“炫酷”但很尴尬的情况;支持设置多种滑动方式(如平移、拉伸、背后固定、顶层固定、全屏等);支持所有可滚动视图的越界回弹。7.3.1导入依赖在build.gradle文件中找到dependencies模块,在其中添加如下语句导入依赖。implementation'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-andx-11'implementation'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-andx-11'implementation'androidx.legacy:legacy-support-v4:1.0.0'7.3.2提供的类SmartRefreshLayoutClassicsHeaderClassicsFooter7.3.3在布局文件中使用(1)只使用SmartRefreshLayout组件

<com.scwang.smartrefresh.layout.SmartRefreshLayoutandroid:id="@+id/refreshLayout"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.recyclerview.widget.RecyclerView

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@an

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论