Android Hongyang博客自定义View练习与优化
来源:互联网 发布:无线传屏软件 编辑:程序博客网 时间:2024/06/05 01:04
###所写内容基本来自Hongyang的CSDN博客,以及练习时可以优化处进行优化:> 对应GitHub地址:[https://github.com/youlookwhat/CustomViewStudy](https://github.com/youlookwhat/CustomViewStudy)###一. 博客目录:####1. [Android 自定义View (一)](http://blog.csdn.net/lmj623565791/article/details/24252901)####2. [Android 自定义View (二) 进阶](http://blog.csdn.net/lmj623565791/article/details/24300125)####3. [Android 自定义View (三) 圆环交替 等待效果](http://blog.csdn.net/lmj623565791/article/details/24500107)####4. [Android 自定义View (四) 视频音量调控](http://blog.csdn.net/lmj623565791/article/details/24529807)####5. [Android 手把手教您自定义ViewGroup(一)](http://blog.csdn.net/lmj623565791/article/details/38339817)####6. [Android 深入理解Android中的自定义属性](http://blog.csdn.net/lmj623565791/article/details/45022631)####7. [Android ViewDragHelper完全解析 自定义ViewGroup神器](http://blog.csdn.net/lmj623565791/article/details/46858663)—###二. 主要源码 >- 1.[CustomTitleView.java](https://github.com/youlookwhat/CustomViewStudy/blob/master/app/src/main/java/com/example/jingbin/customview/view/CustomTitleView.java)>- 2.[CustomImageView.java](https://github.com/youlookwhat/CustomViewStudy/blob/master/app/src/main/java/com/example/jingbin/customview/view/CustomImageView.java)>- 3.[CustomProgressBar.java](https://github.com/youlookwhat/CustomViewStudy/blob/master/app/src/main/java/com/example/jingbin/customview/view/CustomProgressBar.java)>- 4.[CustomVolumControlBar.java](https://github.com/youlookwhat/CustomViewStudy/blob/master/app/src/main/java/com/example/jingbin/customview/view/CustomVolumControlBar.java)>- 5.[CustomImgContainer.java](https://github.com/youlookwhat/CustomViewStudy/blob/master/app/src/main/java/com/example/jingbin/customview/viewgroup/CustomImgContainer.java)>- 6.[DeepUnderstandAttrActivity.java](https://github.com/youlookwhat/CustomViewStudy/blob/master/app/src/main/java/com/example/jingbin/customview/activity/DeepUnderstandAttrActivity.java)>- 7.[VDHDeepLayout.java](https://github.com/youlookwhat/CustomViewStudy/blob/master/app/src/main/java/com/example/jingbin/customview/viewgroup/VDHDeepLayout.java)—###三. 项目图片(部分)
四. 代码优化
1. CustomTitleView优化
1.1 关于文字显示优化:
// int textWidth = mRect.width(); // 这样mRect.width()直接计算出来的会有误差 float textWidth = mPaint.measureText(mTitleText);// int textHeight = mRect.height(); //直接计算出来的会有误差 Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); float textHeight = Math.abs((fontMetrics.bottom - fontMetrics.top));
1.2 onDraw里画Text时起点坐标优化:
canvas.drawText(mTitleText, getWidth() / 2 - mRect.width() / 2 - mRect.left, getHeight() / 2 + mRect.height() / 2, mPaint);
canvas.drawText(String text,float x,float y,Paint paint); x和y是绘制时的起点坐标(左下角);
” - mRect.left”: 就很标准,居中显示(csdn:yql_running解决)
1.3 参考文档
- 1.Android 自定义View-怎么绘制居中文本?
- 2.Android的DrawText详解
2. 圆环交替 等待效果优化
2.1 新开线程画线,离开页面时线程未关闭优化
// 用来开关线程 private boolean isContinue; // 绘图线程 new Thread() { public void run() { while (isContinue) { mProgress++; if (mProgress == 360) { mProgress = 0; isNext = !isNext; } Log.e("--------", "在执行.."); postInvalidate(); try { Thread.sleep(100 / mSpeed);// 这里优化了一下,值越大,速度越快 } catch (InterruptedException e) { e.printStackTrace(); } } } }.start();
Activity相关代码:
@Override protected void onStop() { super.onStop(); customProgressBar01.setContinue(false); customProgressBar02.setContinue(false); customProgressBar03.setContinue(false); }
2.2 用户宽高若设置wrap_content时优化
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// super.onMeasure(widthMeasureSpec, heightMeasureSpec); int modeWidth = MeasureSpec.getMode(widthMeasureSpec); int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); if (modeWidth == MeasureSpec.EXACTLY) { width = sizeWidth; } else {//默认宽度200dp width = (int) getContext().getResources().getDimension(R.dimen.width); } Log.e("------------->", "width:" + width); setMeasuredDimension(width, width); }
五. Thanks
- CSDN:张鸿洋
0 0
- Android Hongyang博客自定义View练习与优化
- android自定义View练习之波浪View
- Android 自定义View练习之水波纹
- Android自定义View博客阅读总结
- Android:View滑动与自定义
- Android几种自定义view,优化UI
- Android内存优化--自定义Adapter复用View
- hongyang的博文深入理解Android中的自定义属性 学习笔记
- 自定义View小练习
- 自定义View练习
- 自定义View练习
- 自定义View练习
- 自定义View练习
- 自定义View练习
- 自定义View练习一
- 自定义view-canvas练习
- 自定义View练习1
- 自定义View练习2
- 微机原理==第二章16位和32位微处理器(4)
- 博客 first
- select系统调用与FD_SET,FD_ISSET,FD_ZERO
- 查看进程的内存和CPU占用
- [LeetCode 226] Invert Binary Tree
- Android Hongyang博客自定义View练习与优化
- Maven依赖机制 工作原理 (分析maven项目和非maven项目的差别)
- Win7安装Visual Studio2015 失败的解决方案
- js实现checkbox全选功能
- 直接用电脑下载google play应用
- 判断栈的出栈顺序是否正确
- platform 平台总线
- 求空间两条直线之间的距离
- android学习笔记之二:界面展现