0
点赞
收藏
分享

微信扫一扫

GenaralRoundLayout,androidui图形化

小迁不秃头 2022-01-12 阅读 75

android:background="@color/colorAccent" />

</com.minminaya.widget.GeneralRoundFrameLayout>

给自定义 view 加上圆角裁剪特性

GeneralRoundLayout 设计初期是为了方便各种布局的扩展,因此可以使任何一个 view 支持圆角特性,你只需要重写几个方法

  • 1、让你的自定义 view 比如 GeneralRoundImageView 实现 IRoundView 接口

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、在你要使用的地方

<com.minminaya.genaral.custom.GeneralRoundImageView

android:layout_width=“200dp”

android:layout_height=“200dp”

android:layout_gravity=“center”

android:layout_marginTop=“20dp”

android:src="@color/colorPrimaryDark"

app:corner_radius=“60dp” />

m.minminaya.genaral.custom.GeneralRoundImageView

android:layout_width=“200dp”

android:layout_height=“200dp”

android:layout_gravity=“center”

android:layout_marginTop=“20dp”

android:src="@color/colorPrimaryDark"

app:corner_radius=“60dp” />

举报

相关推荐

0 条评论