interface IRoundView {
fun setCornerRadius(cornerRadius: Float)
fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int)
}
- 2、定义 attrs 属性
在你的 attrs 的文件中,定义 declare-styleable 属性(为了可以在 xml 文件中输入的时候自动提示)
- 2、让 GeneralRoundImageView 实现 IRoundView 接口的方法
public class GeneralRoundImageView extends AppCompatImageView implements IRoundView {
public GeneralRoundImageView(Context context) {
this(context, null);
}
public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setCornerRadius(float cornerRadius) {
}
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
}
- 3、在 GeneralRoundImageView 中定义 GeneralRoundViewImpl 对象,本质上是裁剪 view 的 helper 类,让其初始化,并将 view 的实现分发到 GeneralRoundViewImpl
public class GeneralRoundImageView extends AppCompatImageView implements IRoundView {
private GeneralRoundViewImpl generalRoundViewImpl;
public GeneralRoundImageView(Context context) {
this(context, null);
}
public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(this, context, attrs);
}
public GeneralRoundImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(this, context, attrs);
}
@Override
public void setCornerRadius(float cornerRadius) {
generalRoundViewImpl.setCornerRadius(cornerRadius);
}
@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
generalRoundViewImpl.onLayout(changed, left, top, right, bottom);
}
private void init(GeneralRoundImageView view, Context context, AttributeSet attrs) {
generalRoundViewImpl = new GeneralRoundViewImpl(view,
context,
attrs,
R.styleable.GeneralRoundImageView,
R.styleable.GeneralRoundImageView_corner_radius);
}
}
- 4、重写 dispatchDraw 方法,将实现类的方法包装 super
@Override
protected void dispatchDraw(Canvas canvas) {
generalRoundViewImpl.beforeDispatchDraw(canvas);
super.dispatchDraw(canvas);
generalRoundViewImpl.afterDispatchDraw(canvas);
}
- 5、在你要使用的地方
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
尾声
评论里面有些同学有疑问关于如何学习material design控件,我的建议是去GitHub搜,有很多同行给的例子,这些栗子足够入门。
有朋友说要是动真格的话,需要NDK以及JVM等的知识,首现**NDK并不是神秘的东西,**你跟着官方的步骤走一遍就知道什么回事了,无非就是一些代码格式以及原生/JAVA内存交互,进阶一点的有原生/JAVA线程交互,线程交互确实有点蛋疼,但平常避免用就好了,再说对于初学者来说关心NDK干嘛,据鄙人以前的经历,只在音视频通信和一个嵌入式信号处理(离线)的两个项目中用过,嵌入式信号处理是JAVA->NDK->.SO->MATLAB这样调用的我原来MATLAB的代码,其他的大多就用在游戏上了吧,一般的互联网公司会有人给你公司的SO包的。
至于JVM,该掌握的那部分,相信我,你会掌握的,不该你掌握的,有那些专门研究JVM的人来做,不如省省心有空看看计算机系统,编译原理。
一句话,平常多写多练,这是最基本的程序员的素质,尽量挤时间,读理论基础书籍,JVM不是未来30年唯一的虚拟机,JAVA也不一定再风靡未来30年工业界,其他的系统和语言也会雨后春笋冒出来,但你理论扎实会让你很快理解学会一个语言或者框架,你平常写的多会让你很快熟练的将新学的东西应用到实际中。
初学者,一句话,多练。
包的。**
至于JVM,该掌握的那部分,相信我,你会掌握的,不该你掌握的,有那些专门研究JVM的人来做,不如省省心有空看看计算机系统,编译原理。
一句话,平常多写多练,这是最基本的程序员的素质,尽量挤时间,读理论基础书籍,JVM不是未来30年唯一的虚拟机,JAVA也不一定再风靡未来30年工业界,其他的系统和语言也会雨后春笋冒出来,但你理论扎实会让你很快理解学会一个语言或者框架,你平常写的多会让你很快熟练的将新学的东西应用到实际中。
初学者,一句话,多练。
由于文章篇幅问题复制链接查看详细文章以及获取学习笔记链接:前往我的GitHub