view属性微信小程序样式(微信小程序ss新增样式特性)
作者:yechaoa
作者:yechaoa
简介
ShapeableImageView 是 ImageView 的一个子类。
特点
在 不写shape、不引入三方库 的情况下,可实现较多场景下的图片显示效果,具体如下图:
效果 使用介绍1. 引入Material包implementation 'com.google.android.material:material:1.2.1'
2. 常用属性
属性
描述
strokeWidth
描边宽度
strokeColor
描边颜色
shapeAppearance
外观样式
shapeAppearanceOverlay
同上,叠加层
展开全文
3. 常规使用
和ImageView正常使用没有区别
com.google.android.material.imageview.ShapeableImageView
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_margin= "10dp"
android:src= "@mipmap/ic_avatar"/
常用效果
下面主要介绍的使用效果是:
圆角
圆
半圆
菱形
com.google.android.material.imageview.ShapeableImageView
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_margin= "10dp"
android:src= "@mipmap/ic_avatar"
app:shapeAppearance= "@style/RoundedStyle"/
!--ShapeableImageView 圆角--
style name= "RoundedStyle"
item name= "cornerFamily"rounded/item
item name= "cornerSize"10dp/item
/style
没有直接设置圆角的属性,需要用到 app:shapeAppearance ,后面会说
cornerFamily 角的处理方式,rounded圆角,cut裁剪
cornerSize 圆角大小
没有直接设置圆角的属性,需要用到 app:shapeAppearance ,后面会说
cornerFamily 角的处理方式,rounded圆角,cut裁剪
cornerSize 圆角大小
com.google.android.material.imageview.ShapeableImageView
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_margin= "10dp"
android:src= "@mipmap/ic_avatar"
app:shapeAppearance= "@style/CircleStyle"/
!--ShapeableImageView 圆 --
style name= "CircleStyle"
item name= "cornerFamily"rounded/item
item name= "cornerSize"50%/item
/style
圆角的大小可以用百分比,也可以自己计算,比如宽高100dp,圆角50dp
圆角的大小可以用百分比,也可以自己计算,比如宽高100dp,圆角50dp
com.google.android.material.imageview.ShapeableImageView
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_margin= "10dp"
android:padding= "2dp"
android:src= "@mipmap/ic_avatar"
app:shapeAppearance= "@style/SemicircleStyle"
app:strokeColor= "@color/red"
app:strokeWidth= "4dp"/
!--ShapeableImageView 半圆 --
style name= "SemicircleStyle"
item name= "cornerFamily"rounded/item
item name= "cornerSizeTopLeft"50%/item
item name= "cornerSizeTopRight"50%/item
/style
4. 菱形
com.google.android.material.imageview.ShapeableImageView
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_margin= "10dp"
android:padding= "2dp"
android:src= "@mipmap/ic_avatar"
app:shapeAppearance= "@style/RhombusStyle"
app:strokeColor= "@color/red"
app:strokeWidth= "4dp"/
!--ShapeableImageView 菱形 --
style name= "RhombusStyle"
item name= "cornerFamily"cut/item
item name= "cornerSize"50%/item
/style
同样,裁剪模式下圆角大小也可以计算
同样,裁剪模式下圆角大小也可以计算
主要介绍:shapeAppearance、ShapeAppearanceModel、MaterialShapeDrawable
会涉及到源码,但是经过去繁从简,看起来也非常轻松的。
1. ShapeAppearance
❝
Shape appearance overlay style reference for ShapeableImageView. ShapeableImageView的形状外观覆盖样式参考。
❞
Shape appearance overlay style reference for ShapeableImageView. ShapeableImageView的形状外观覆盖样式参考。
❞
前面可以看到我们设置圆角其实是用的 style ,那为什么不直接用 attrs 呢,不是更加直观方便吗,带着疑问来看看源码是怎么处理的。
直接看 ShapeableImageView 的次构造方法:
public class ShapeableImageView extends AppCompatImageView implements Shapeable {
...
public ShapeableImageView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(wrap(context, attrs, defStyle, DEF_STYLE_RES), attrs, defStyle);
// Ensure we are using the correctly themed context rather than the context that was passed in.
context = getContext;
clearPaint = new Paint;
clearPaint.setAntiAlias( true);
clearPaint.setColor(Color.WHITE);
clearPaint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
destination = new RectF;
maskRect = new RectF;
maskPath = new Path;
TypedArray attributes =
context.obtainStyledAttributes(
attrs, R.styleable.ShapeableImageView, defStyle, DEF_STYLE_RES);
strokeColor =
MaterialResources.getColorStateList(
context, attributes, R.styleable.ShapeableImageView_strokeColor);
strokeWidth = attributes.getDimensionPixelSize(R.styleable.ShapeableImageView_strokeWidth, 0);
borderPaint = new Paint;
borderPaint.setStyle(Style.STROKE);
borderPaint.setAntiAlias( true);
shapeAppearanceModel =
ShapeAppearanceModel.builder(context, attrs, defStyle, DEF_STYLE_RES).build;
shadowDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
if(VERSION.SDK_INT = VERSION_CODES.LOLLIPOP) {
setOutlineProvider(new OutlineProvider);
}
}
}
常规操作,获取自定义属性。
关键的两行代码:
shapeAppearanceModel = ShapeAppearanceModel.builder(context, attrs, defStyle, DEF_STYLE_RES).build;
shadowDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
也就是说我们给 shapeAppearance 设置的style,并不是 ShapeableImageView 自己来处理的,而是由 ShapeAppearanceModel 来构建的,然后又交给 MaterialShapeDrawable 来绘制的。
2. ShapeAppearanceModel
有点类似 Flutter 中的Decoration,可以构建出花里胡哨的效果。
来看 ShapeAppearanceModel 部分源码:
public class ShapeAppearanceModel {
/** Builder to create instances of {@link ShapeAppearanceModel}s. */
public static final class Builder {
@NonNull
private CornerTreatment topLeftCorner = MaterialShapeUtils.createDefaultCornerTreatment;
@NonNull
private CornerTreatment topRightCorner = MaterialShapeUtils.createDefaultCornerTreatment;
@NonNull
private CornerTreatment bottomRightCorner = MaterialShapeUtils.createDefaultCornerTreatment;
@NonNull
private CornerTreatment bottomLeftCorner = MaterialShapeUtils.createDefaultCornerTreatment;
@NonNull private CornerSize topLeftCornerSize = new AbsoluteCornerSize(0);
@NonNull private CornerSize topRightCornerSize = new AbsoluteCornerSize(0);
@NonNull private CornerSize bottomRightCornerSize = new AbsoluteCornerSize(0);
@NonNull private CornerSize bottomLeftCornerSize = new AbsoluteCornerSize(0);
@NonNull private EdgeTreatment topEdge = MaterialShapeUtils.createDefaultEdgeTreatment;
@NonNull private EdgeTreatment rightEdge = MaterialShapeUtils.createDefaultEdgeTreatment;
@NonNull private EdgeTreatment bottomEdge = MaterialShapeUtils.createDefaultEdgeTreatment;
@NonNull private EdgeTreatment leftEdge = MaterialShapeUtils.createDefaultEdgeTreatment;
public Builder{}
...
}
...
}
可以看到有各种边和角的属性,这里注意两个点:
MaterialShapeUtils.createDefaultCornerTreatment 创建默认角的处理方式
MaterialShapeUtils.createDefaultEdgeTreatment 创建默认边的处理方式
MaterialShapeUtils.createDefaultCornerTreatment 创建默认角的处理方式
MaterialShapeUtils.createDefaultEdgeTreatment 创建默认边的处理方式
也就意味着,边和角除了默认,是可以自定义的,这就有极大的想象空间了, 比如这样:
// 代码设置 角和边
val shapeAppearanceModel2 = ShapeAppearanceModel.builder.apply {
setAllCorners(RoundedCornerTreatment)
setAllCornerSizes(50f)
setAllEdges(TriangleEdgeTreatment(50f, false))
}.build
val drawable2 = MaterialShapeDrawable(shapeAppearanceModel2).apply {
setTint(ContextCompat.getColor(this@ShapeableImageViewActivity, R.color.colorPrimary))
paintStyle = Paint.Style.FILL_AND_STROKE
strokeWidth = 50f
strokeColor = ContextCompat.getColorStateList(this@ShapeableImageViewActivity, R.color.red)
}
mBinding.text2.setTextColor(Color.WHITE)
mBinding.text2.background = drawable2
再比如这样:
// 代码设置 聊天框效果
val shapeAppearanceModel3 = ShapeAppearanceModel.builder.apply {
setAllCorners(RoundedCornerTreatment)
setAllCornerSizes(20f)
setRightEdge(object : TriangleEdgeTreatment(20f, false) {
// center 位置 , interpolation 角的大小
override fun getEdgePath(length: Float, center: Float, interpolation: Float, shapePath: ShapePath) {
super.getEdgePath(length, 35f, interpolation, shapePath)
}
})
}.build
val drawable3 = MaterialShapeDrawable(shapeAppearanceModel3).apply {
setTint(ContextCompat.getColor(this@ShapeableImageViewActivity, R.color.colorPrimary))
paintStyle = Paint.Style.FILL
}
(mBinding.text3.parent as ViewGroup).clipChildren = false// 不限制子view在其范围内
mBinding.text3.setTextColor(Color.WHITE)
mBinding.text3.background = drawable3
3. MaterialShapeDrawable
用于设置背景、阴影等其他属性。源码如下(有删减):
public class MaterialShapeDrawable extends Drawable implements TintAwareDrawable, Shapeable {
...
@Override
public void draw(@NonNull Canvas canvas) {
fillPaint.setColorFilter(tintFilter);
final int prevAlpha = fillPaint.getAlpha;
fillPaint.setAlpha(modulateAlpha(prevAlpha, drawableState.alpha));
strokePaint.setColorFilter(strokeTintFilter);
strokePaint.setStrokeWidth(drawableState.strokeWidth);
final int prevStrokeAlpha = strokePaint.getAlpha;
strokePaint.setAlpha(modulateAlpha(prevStrokeAlpha, drawableState.alpha));
if(pathDirty) {
calculateStrokePath;
calculatePath(getBoundsAsRectF, path);
pathDirty = false;
}
maybeDrawCompatShadow(canvas);
if(hasFill) {
drawFillShape(canvas);
}
if(hasStroke) {
drawStrokeShape(canvas);
}
...
static final class MaterialShapeDrawableState extends ConstantState {
...
public MaterialShapeDrawableState(@NonNull MaterialShapeDrawableState orig) {
shapeAppearanceModel = orig.shapeAppearanceModel;
elevationOverlayProvider = orig.elevationOverlayProvider;
strokeWidth = orig.strokeWidth;
colorFilter = orig.colorFilter;
fillColor = orig.fillColor;
strokeColor = orig.strokeColor;
tintMode = orig.tintMode;
tintList = orig.tintList;
alpha = orig.alpha;
scale = orig.scale;
shadowCompatOffset = orig.shadowCompatOffset;
shadowCompatMode = orig.shadowCompatMode;
useTintColorForShadow = orig.useTintColorForShadow;
interpolation = orig.interpolation;
parentAbsoluteElevation = orig.parentAbsoluteElevation;
elevation = orig.elevation;
translationZ = orig.translationZ;
shadowCompatRadius = orig.shadowCompatRadius;
shadowCompatRotation = orig.shadowCompatRotation;
strokeTintList = orig.strokeTintList;
paintStyle = orig.paintStyle;
if(orig.padding != null) {
padding = new Rect(orig.padding);
}
}
...
}
...
}
需要特别说明的是:
ShapeAppearanceModel 只能是实现 Shapeable 接口的View才可以设置,比如 Chip 、 MaterialButtom 等。
而 MaterialShapeDrawable 其实就是 Drawable ,是所有View都可以设置的。
ShapeAppearanceModel 只能是实现 Shapeable 接口的View才可以设置,比如 Chip 、 MaterialButtom 等。
而 MaterialShapeDrawable 其实就是 Drawable ,是所有View都可以设置的。
至此,关于ShapeableImageView这个小而美的官方图片显示控件讲解完毕。
• 耗时2年,Android进阶三部曲第三部《Android进阶指北》出版!
• 『BATcoder』做了多年安卓还没编译过源码?一个视频带你玩转!
• 『BATcoder』我去!安装Ubuntu还有坑?
• 重生!进阶三部曲第一部《Android进阶之光》第2版 出版!
BATcoder技术群,让一部分人先进大厂
大家好,我是刘望舒,腾讯TVP,著有三本业内知名畅销书,连续四年蝉联电子工业出版社年度优秀作者,百度百科收录的资深技术专家。
想要加入 BATcoder技术群,公号回复BAT 即可。
为了防止失联,欢迎关注我的小号
微信改了推送机制,真爱请星标本公号👇