cao死我好湿好紧好爽动态视屏|精选久久久久久久久久|中文无码精品一区二区三区四季|AAA国语精品刺激对白视频|

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

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

小程序開發(fā)1年前 (2024-05-08)392

作者:yechaoa

作者:yechaoa

簡介

ShapeableImageView 是 ImageView 的一個子類。

特點(diǎn)

在 不寫shape、不引入三方庫 的情況下,可實(shí)現(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

沒有直接設(shè)置圓角的屬性,需要用到 app:shapeAppearance ,后面會說

cornerFamily 角的處理方式,rounded圓角,cut裁剪

cornerSize 圓角大小

沒有直接設(shè)置圓角的屬性,需要用到 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的形狀外觀覆蓋樣式參考。

?

前面可以看到我們設(shè)置圓角其實(shí)是用的 style ,那為什么不直接用 attrs 呢,不是更加直觀方便嗎,帶著疑問來看看源碼是怎么處理的。

直接看 ShapeableImageView 的次構(gòu)造方法:

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ī)操作,獲取自定義屬性。

關(guān)鍵的兩行代碼:

shapeAppearanceModel = ShapeAppearanceModel.builder(context, attrs, defStyle, DEF_STYLE_RES).build;

shadowDrawable = new MaterialShapeDrawable(shapeAppearanceModel);

也就是說我們給 shapeAppearance 設(shè)置的style,并不是 ShapeableImageView 自己來處理的,而是由 ShapeAppearanceModel 來構(gòu)建的,然后又交給 MaterialShapeDrawable 來繪制的。

2. ShapeAppearanceModel

有點(diǎn)類似 Flutter 中的Decoration,可以構(gòu)建出花里胡哨的效果。

來看 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{}

...

}

...

}

可以看到有各種邊和角的屬性,這里注意兩個點(diǎn):

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

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

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

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

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

// 代碼設(shè)置 角和邊

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

再比如這樣:

// 代碼設(shè)置 聊天框效果

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在其范圍內(nèi)

mBinding.text3.setTextColor(Color.WHITE)

mBinding.text3.background = drawable3

3. MaterialShapeDrawable

用于設(shè)置背景、陰影等其他屬性。源碼如下(有刪減):

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

而 MaterialShapeDrawable 其實(shí)就是 Drawable ,是所有View都可以設(shè)置的。

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

而 MaterialShapeDrawable 其實(shí)就是 Drawable ,是所有View都可以設(shè)置的。

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

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

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

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

? 重生!進(jìn)階三部曲第一部《Android進(jìn)階之光》第2版 出版!

BATcoder技術(shù)群,讓一部分人先進(jìn)大廠

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

想要加入 BATcoder技術(shù)群,公號回復(fù)BAT 即可。

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

微信改了推送機(jī)制,真愛請星標(biāo)本公號??

掃描二維碼推送至手機(jī)訪問。

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

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

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

云南人才招聘網(wǎng)官網(wǎng)入口(云南人才招聘網(wǎng)報名入口)

云南人才招聘網(wǎng)官網(wǎng)入口(云南人才招聘網(wǎng)報名入口)

本篇文章給大家談?wù)勗颇先瞬耪衅妇W(wǎng)官網(wǎng)入口,以及云南人才招聘網(wǎng)報名入口對應(yīng)的知識點(diǎn),希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、如何登錄云南招聘網(wǎng)? 2、云南省人才網(wǎng)報名入口 3、云南省官方人才招聘網(wǎng)是哪個? 如何登錄云南招聘網(wǎng)? 點(diǎn)開云南招聘網(wǎng)首頁后,在登陸那選擇會員登陸...

個人求職自我介紹(個人求職自我介紹計算機(jī))

個人求職自我介紹(個人求職自我介紹計算機(jī))

本篇文章給大家談?wù)剛€人求職自我介紹,以及個人求職自我介紹計算機(jī)對應(yīng)的知識點(diǎn),希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、求職自我介紹怎么說 2、求職自我介紹 3、簡短求職自我介紹 4、求職面試自我介紹 求職自我介紹怎么說 求職自我介紹怎么說 求職自我介紹怎么說,在我...

成都房產(chǎn)信息網(wǎng)上查詢(成都市查房產(chǎn)信息查詢)

成都房產(chǎn)信息網(wǎng)上查詢(成都市查房產(chǎn)信息查詢)

本篇文章給大家談?wù)劤啥挤慨a(chǎn)信息網(wǎng)上查詢,以及成都市查房產(chǎn)信息查詢對應(yīng)的知識點(diǎn),希望對各位有所幫助,不要忘了收藏本站喔。 本文目錄一覽: 1、成都房產(chǎn)登記信息如何查詢?去哪里查詢 2、房產(chǎn)證查詢網(wǎng)上怎么查詢?成都房產(chǎn)證查詢網(wǎng)上查詢方法是什么 3、成都房產(chǎn)證查詢方法有哪些 成都房產(chǎn)登記信息如何...

中國學(xué)校是什么時候產(chǎn)生的(我國學(xué)校正式產(chǎn)生于什么時候)

中國學(xué)校是什么時候產(chǎn)生的(我國學(xué)校正式產(chǎn)生于什么時候)

今天給各位分享中國學(xué)校是什么時候產(chǎn)生的的知識,其中也會對我國學(xué)校正式產(chǎn)生于什么時候進(jìn)行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧!本文目錄一覽: 1、中國什么時候開始有大學(xué)? 2、中國最早學(xué)校出現(xiàn)在什么時候 3、我國學(xué)校最早產(chǎn)生于( )。 a 夏朝 b 商 c 周 d...

求職信200(求職信200字)

求職信200(求職信200字)

今天給各位分享求職信200的知識,其中也會對求職信200字進(jìn)行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧!本文目錄一覽: 1、應(yīng)屆畢業(yè)生求職信模板5篇 2、會計求職信怎么寫200字左右 3、急需求職信 4、萬能的個人求職信模板自薦信范文 5、郵件求職信范文精選6...

平湖人才招聘網(wǎng)最新招聘廣陳鎮(zhèn)(平湖市廣陳鎮(zhèn)招工信息)

平湖人才招聘網(wǎng)最新招聘廣陳鎮(zhèn)(平湖市廣陳鎮(zhèn)招工信息)

今天給各位分享平湖人才招聘網(wǎng)最新招聘廣陳鎮(zhèn)的知識,其中也會對平湖市廣陳鎮(zhèn)招工信息進(jìn)行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關(guān)注本站,現(xiàn)在開始吧!本文目錄一覽: 1、在平湖廣陳鎮(zhèn)買房怎么樣 2、上海9月份天氣熱嗎? 3、廣陳鎮(zhèn)智能裝備產(chǎn)業(yè)園地址 4、竹塔村歷史 5、2021年平湖...

西安市| 潮安县| 武威市| 大兴区| 屯留县| 博湖县| 台东市| 惠州市| 五指山市| 榆社县| 雅安市| 合阳县| 宁武县| 揭阳市| 临猗县| 博客| 秦皇岛市| 佛冈县| 仲巴县| 富源县| 浠水县| 富平县| 景泰县| 蓝山县| 宝应县| 石城县| 景谷| 达拉特旗| 保靖县| 库伦旗| 上饶市| 淳安县| 宜都市| 凤庆县| 龙游县| 遂溪县| 奈曼旗| 洛川县| 观塘区| 乾安县| 黄平县|