日韩精品亚洲精品中文字幕乱伦AV|曰韩区二区三区日韩中文字幕五码|ady99久久人人看人人摸人人|动漫一区二区黄99精品视频在线|AV片在线观看亚洲中文国产精品|伦乱在线VA欧美性爱一二区|亚洲无码mv91热色视频|激情六月综合欧美精品中文

當前位置:首頁 > 小程序開發(fā) > 正文內容

view屬性微信小程序樣式(微信小程序ss新增樣式特性)

小程序開發(fā)10個月前 (05-08)368

作者:yechaoa

作者:yechaoa

簡介

ShapeableImageView 是 ImageView 的一個子類。

特點

在 不寫shape、不引入三方庫 的情況下,可實現(xiàn)較多場景下的圖片顯示效果,具體如下圖:

效果 使用介紹1. 引入Material包implementation 'com.google.android.material:material:1.2.1'

2. 常用屬性

屬性

描述

strokeWidth

描邊寬度

strokeColor

描邊顏色

shapeAppearance

外觀樣式

shapeAppearanceOverlay

同上,疊加層

展開全文

3. 常規(guī)使用

和ImageView正常使用沒有區(qū)別

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

會涉及到源碼,但是經(jīng)過去繁從簡,看起來也非常輕松的。

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);

}

}

}

常規(guī)操作,獲取自定義屬性。

關鍵的兩行代碼:

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 創(chuàng)建默認角的處理方式

MaterialShapeUtils.createDefaultEdgeTreatment 創(chuàng)建默認邊的處理方式

MaterialShapeUtils.createDefaultCornerTreatment 創(chuàng)建默認角的處理方式

MaterialShapeUtils.createDefaultEdgeTreatment 創(chuàng)建默認邊的處理方式

也就意味著,邊和角除了默認,是可以自定義的,這就有極大的想象空間了, 比如這樣:

// 代碼設置 角和邊

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)

view屬性微信小程序樣式(微信小程序ss新增樣式特性)

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 只能是實現(xiàn) Shapeable 接口的View才可以設置,比如 Chip 、 MaterialButtom 等。

而 MaterialShapeDrawable 其實就是 Drawable ,是所有View都可以設置的。

ShapeAppearanceModel 只能是實現(xiàn) Shapeable 接口的View才可以設置,比如 Chip 、 MaterialButtom 等。

而 MaterialShapeDrawable 其實就是 Drawable ,是所有View都可以設置的。

至此,關于ShapeableImageView這個小而美的官方圖片顯示控件講解完畢。

? 耗時2年,Android進階三部曲第三部《Android進階指北》出版!

? 『BATcoder』做了多年安卓還沒編譯過源碼?一個視頻帶你玩轉!

? 『BATcoder』我去!安裝Ubuntu還有坑?

? 重生!進階三部曲第一部《Android進階之光》第2版 出版!

BATcoder技術群,讓一部分人先進大廠

大家好,我是劉望舒,騰訊TVP,著有三本業(yè)內知名暢銷書,連續(xù)四年蟬聯(lián)電子工業(yè)出版社年度優(yōu)秀作者,百度百科收錄的資深技術專家。

想要加入 BATcoder技術群,公號回復BAT 即可。

為了防止失聯(lián),歡迎關注我的小號

微信改了推送機制,真愛請星標本公號??

掃描二維碼推送至手機訪問。

版權聲明:本文由飛速云SEO網(wǎng)絡優(yōu)化推廣發(fā)布,如需轉載請注明出處。

本文鏈接:http://m.atlasseeker.com/post/112250.html

分享給朋友:

“view屬性微信小程序樣式(微信小程序ss新增樣式特性)” 的相關文章

個人求職簡歷大學生(個人求職簡歷大學生模板)

個人求職簡歷大學生(個人求職簡歷大學生模板)

今天給各位分享個人求職簡歷大學生的知識,其中也會對個人求職簡歷大學生模板進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關注本站,現(xiàn)在開始吧!本文目錄一覽: 1、大學生個人簡歷范文 2、大學生求職個人簡歷模板5篇 3、大學生求職簡歷范文(精選5篇) 大學生個人簡歷范文 大學生個人簡歷范文...

個人求職簡歷表電子版(個人求職工作簡歷表格電子版)

個人求職簡歷表電子版(個人求職工作簡歷表格電子版)

本篇文章給大家談談個人求職簡歷表電子版,以及個人求職工作簡歷表格電子版對應的知識點,希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、電子版?zhèn)€人簡歷格式10篇 2、個人簡歷電子版最精辟范本5篇 3、個人簡歷電子版最經(jīng)典的模板格式 4、應聘工作的個人簡歷電子版五篇 電子版?zhèn)€人...

東莞市人才招聘網(wǎng)官網(wǎng)(東莞市人才招聘網(wǎng)官網(wǎng)首頁)

東莞市人才招聘網(wǎng)官網(wǎng)(東莞市人才招聘網(wǎng)官網(wǎng)首頁)

今天給各位分享東莞市人才招聘網(wǎng)官網(wǎng)的知識,其中也會對東莞市人才招聘網(wǎng)官網(wǎng)首頁進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關注本站,現(xiàn)在開始吧!本文目錄一覽: 1、東莞有什么人才招聘網(wǎng) 2、東莞好的招聘網(wǎng)站請推薦下。。。 3、廣東東莞最大最有名的人才招聘網(wǎng)站網(wǎng)址是多少?拜托各位了 3Q...

業(yè)之峰全包裝修價格(業(yè)之峰裝飾公司裝修每平米價格)

業(yè)之峰全包裝修價格(業(yè)之峰裝飾公司裝修每平米價格)

本篇文章給大家談談業(yè)之峰全包裝修價格,以及業(yè)之峰裝飾公司裝修每平米價格對應的知識點,希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、現(xiàn)在裝修一平米大概多少錢? 2、我想知道西安老房改造裝修公司哪家好? 3、北京業(yè)之峰裝飾裝修,平均每平米大概多少錢? 現(xiàn)在裝修一平米大概多少錢?...

杭州人才招聘網(wǎng)官網(wǎng)(杭州市人才網(wǎng)官網(wǎng)招聘)

杭州人才招聘網(wǎng)官網(wǎng)(杭州市人才網(wǎng)官網(wǎng)招聘)

本篇文章給大家談談杭州人才招聘網(wǎng)官網(wǎng),以及杭州市人才網(wǎng)官網(wǎng)招聘對應的知識點,希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、杭州招聘網(wǎng)站哪個最好 2、杭州人才網(wǎng) 3、杭州有哪些招聘網(wǎng)? 4、杭州人才招聘的主要網(wǎng)址是哪些?三個以上! 5、請問杭州網(wǎng)上找工作都有哪些網(wǎng)站.平...

求職申請信英語作文模板(求職申請信英文范文)

求職申請信英語作文模板(求職申請信英文范文)

今天給各位分享求職申請信英語作文模板的知識,其中也會對求職申請信英文范文進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關注本站,現(xiàn)在開始吧!本文目錄一覽: 1、英文求職申請信范文3篇 2、英文求職申請信 3、求職申請書英語作文 4、100字英文求職信范文3篇 5、英語求職信 英文...