文档详情

Android-解析自定义view之圆形头像的各类方案.doc

发布:2017-01-30约字共7页下载文档
文本预览下载声明
Android-解析自定义view之圆形头像的各类方案 我们可以看到很多app都采用了圆形头像,那么怎么绘制圆形头像才是性能最好?代码复用性最强?也最方便呢?本博主做了一些探究。 文章结构:1.利用shape来制作圆形头像(一种死方案,要求是美工愿意配合你) 2.结合一个会导致oom的实现圆形头像方案进行性能分析 3.最优的圆形头像方案 一、利用shape来制作圆形头像(要求是美工愿意配合你) 为什么要求美工配合你呢??因为这个方案是在ImageView直接调用资源文件的,也就是直接用了ImageView的LayoutParams的match_parent模式。不能按照那个圆的大小来适配。 给出代码讲解:drawable文件的shape标签 ?xml version=1.0 encoding=utf-8? shape xmlns:android=/apk/res/android android:shape=oval !-- Corner的属性是设置圆角的半径的-- solid android:color=#FFFFFF / stroke android:width=2dp android:color=#777777/stroke size android:width=120dp android:height=120dp / /shape 在xml文件中的调用: ?xml version=1.0 encoding=utf-8? LinearLayout xmlns:android=/apk/res/android xmlns:app=/apk/res-auto xmlns:tools=/tools android:layout_width=match_parent android:layout_height=match_parent android:orientation=vertical tools:context=com.demo.fuzhucheng.someShapesImageview.ImageViewActivity !--第一种方案 -- ImageView android:id=@+id/shapecircle android:layout_width=150dp android:layout_height=150dp android:background=@drawable/activity_circle_circleimageview android:src=@drawable/activity_imageview_photo / !--最优方案,也就是本文的第三种方案 -- com.demo.fuzhucheng.someShapesImageview.CircleImageview android:id=@+id/mycircle android:layout_width=180dp android:layout_height=180dp android:background=@color/white android:src=@drawable/timg app:backgroundHeadColor=@color/yellow app:circleBorderHeadWidth=5dp app:ringHeadColor=@color/colorAccent / /LinearLayout 二、对一种容易导致OOM的方案进行分析: 给出代码分析:下面这个是别人的代码,由于点评就不给链接的。 public class CircleImageView extends ImageView{ public CircleImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { //获得图片的宽度 int width=getWidth(); //获得图片的高度 int height=getH
显示全部
相似文档