日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

打包android阴影不见,Android无pading超简单超实用阴影解决方案

發布時間:2025/3/20 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 打包android阴影不见,Android无pading超简单超实用阴影解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

這個迭代,UI在給了幾張帶陰影的圖片,那種陰影范圍很大,實際內容卻只有一點的圖片。

效果類似這樣。

不知道這張圖有沒有表達清楚,就是那種圖片之間陰影需要重疊才能使內容對其,陰影還有顏色的效果。

Android 5.0后才支持elevation屬性,還不支持陰影顏色的設定。IOS同事笑了,他們說直接把陰影效果給他們,不要帶陰影的圖片,他們天然支持陰影,可以直接繪制。

于是,上網搜索,發現目前Andorid 平臺實現陰影大概有這么幾種方式

2、CardView 不支持陰影顏色

3、開源庫ShadowLayout

4、模仿FloatingActionButton 實現陰影等等。

這些方式是可以實現陰影的顯示,但是基本都是將陰影作為控件的一部分去實現的。這樣,就需要給控件設置一些padding值,才能讓陰影顯示出來。這種方式使得布局很不方便對齊。

我的解決方案

先上效果看看

既然將陰影作為控件的一部分去實現不利于控件的布局和對其,那就咱就在ViewGroup里去實現陰影。繪制的時候根據子view的位置繪制出陰影,這樣就不會影響控件的布局和對其了。

其實我覺得控件的陰影天然就應該在父布局去實現,就像現實中的陰影那樣。

實現思路

1、繼承ViewGoup

Android 中有FrameLayout、LinearLayout、RelativeLayout、ConstraintLayout等等,這些layout是為了進行子view布局而設計的,如果不進行背景色的設置,默認是不走ondraw方法的。

我們可以繼承這些ViewGroup,設置setWillNotDraw(false)標志位強制進行繪制,這樣我們就既擁有了布局的功能,也擁有了繪制的功能。然后就能在onDraw方法中,根據子view的位置繪制背景了。

2、復寫onDraw方法繪制子view背景

這一步很簡單,一個循環遍歷子view就好了

如何繪制

1、繪制之前 需要設置 setLayerType(View.LAYER_TYPE_SOFTWARE, null)關閉硬件加速,因為一些高級繪制方法可能不支持硬件加速。

2、為paint設置 shadowLayer

paint.setShadowLayer(shadowLayoutParams.shadowRadius, shadowLayoutParams.xOffset

, shadowLayoutParams.yOffset, shadowLayoutParams.shadowColor);

復制代碼

3、最后的代碼

public class ShadowConstraintLayout extends ConstraintLayout {

Paint paint;

RectF rectF = new RectF();

public ShadowConstraintLayout(Context context) {

this(context, null);

}

public ShadowConstraintLayout(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public ShadowConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

// 關鍵點,使ViewGroup調用onDraw方法

setWillNotDraw(false);

paint = new Paint();

paint.setStyle(Paint.Style.FILL);

paint.setAntiAlias(true);

this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

int childCount = getChildCount();

// 根據參數在父布局繪制背景

for (int i = 0; i < childCount; i++) {

View child = getChildAt(i);

ViewGroup.LayoutParams layoutParams = child.getLayoutParams();

if (layoutParams instanceof ShadowConstraintLayout.LayoutParams) {

ShadowConstraintLayout.LayoutParams shadowLayoutParams = (LayoutParams) layoutParams;

if (shadowLayoutParams.shadowRadius > 0 && shadowLayoutParams.shadowColor != Color.TRANSPARENT) {

rectF.left = child.getLeft();

rectF.right = child.getRight();

rectF.top = child.getTop();

rectF.bottom = child.getBottom();

paint.setStyle(Paint.Style.FILL);

paint.setShadowLayer(shadowLayoutParams.shadowRadius, shadowLayoutParams.xOffset

, shadowLayoutParams.yOffset, shadowLayoutParams.shadowColor);

paint.setColor(shadowLayoutParams.shadowColor);

canvas.drawRoundRect(rectF, shadowLayoutParams.shadowRoundRadius, shadowLayoutParams.shadowRoundRadius, paint);

}

}

}

}

public static class LayoutParams extends ConstraintLayout.LayoutParams {

private float xOffset;

private float yOffset;

// 陰影顏色

private int shadowColor;

// 陰影大小

private float shadowRadius;

// 陰影圓角大小

private float shadowRoundRadius;

public LayoutParams(int width, int height) {

super(width, height);

}

public LayoutParams(ShadowConstraintLayout.LayoutParams source) {

super(source);

xOffset = source.getXOffset();

yOffset = source.getYOffset();

shadowColor = source.getShadowColor();

shadowRadius = source.getShadowRadius();

shadowRoundRadius = source.getShadowRoundRadius();

}

public LayoutParams(Context c, AttributeSet attrs) {

super(c, attrs);

TypedArray attributes = c.obtainStyledAttributes(attrs, R.styleable.ShadowConstraintLayout_Layout);

xOffset = attributes.getDimension(R.styleable.ShadowConstraintLayout_Layout_layout_xOffset, 0);

yOffset = attributes.getDimension(R.styleable.ShadowConstraintLayout_Layout_layout_yOffset, 0);

shadowRadius = attributes.getDimension(R.styleable.ShadowConstraintLayout_Layout_layout_shadowRadius, 0);

shadowColor = attributes.getColor(R.styleable.ShadowConstraintLayout_Layout_layout_shadowColor, 0);

shadowRoundRadius = attributes.getDimension(R.styleable.ShadowConstraintLayout_Layout_layout_shadowRoundRadius, 0);

attributes.recycle();

}

public LayoutParams(ViewGroup.LayoutParams source) {

super(source);

}

public float getXOffset() {

return xOffset;

}

public void setXOffset(float xOffset) {

this.xOffset = xOffset;

}

public float getYOffset() {

return yOffset;

}

public void setYOffset(float yOffset) {

this.yOffset = yOffset;

}

public int getShadowColor() {

return shadowColor;

}

public void setShadowColor(int shadowColor) {

this.shadowColor = shadowColor;

}

public float getShadowRadius() {

return shadowRadius;

}

public void setShadowRadius(float shadowRadius) {

this.shadowRadius = shadowRadius;

}

public float getShadowRoundRadius() {

return shadowRoundRadius;

}

public void setShadowRoundRadius(float shadowRoundRadius) {

this.shadowRoundRadius = shadowRoundRadius;

}

}

public ShadowConstraintLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {

return new ShadowConstraintLayout.LayoutParams(this.getContext(), attrs);

}

protected ShadowConstraintLayout.LayoutParams generateDefaultLayoutParams() {

return new ShadowConstraintLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

}

protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {

return new ShadowConstraintLayout.LayoutParams(p);

}

protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {

return p instanceof ShadowConstraintLayout.LayoutParams;

}

}

復制代碼

使用起來大概像這樣。

android:layout_width="match_parent"

android:layout_height="match_parent"

xmlns:app="http://schemas.android.com/apk/res-auto">

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_centerHorizontal="true"

android:background="@drawable/blue_round_bg"

app:layout_shadowColor="#805468A5"

app:layout_shadowRadius="8dp"

app:layout_shadowRoundRadius="5dp"

app:layout_yOffset="2dp" />

復制代碼

目前已實現 FrameLayout、LinearLayout、RelativeLayout、ConstraintLayout等等

添加依賴 implementation 'com.github.ZhangHao555:AndroidGroupShadow:v1.0'

總結

以上是生活随笔為你收集整理的打包android阴影不见,Android无pading超简单超实用阴影解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。