Skip to content

Commit

Permalink
优化、新增阴影背景功能支持
Browse files Browse the repository at this point in the history
  • Loading branch information
heyongrui committed Dec 13, 2019
1 parent 39d0de0 commit f73851d
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 64 deletions.
124 changes: 124 additions & 0 deletions base/src/main/java/com/heyongrui/base/utils/DrawableUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
Expand All @@ -19,11 +20,68 @@
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import com.blankj.utilcode.util.ConvertUtils;

import java.util.ArrayList;
import java.util.List;

/**
* Created by lambert on 2018/8/30.
* Drawable工具类,避免过多的drawable.xml资源创建
*/
public class DrawableUtil {

/**
* 个别复杂使用示例
*
* int roundRadius = 20;
* GradientDrawable maskDrawable = new GradientDrawable();
* maskDrawable.setColor(Color.BLACK);
* maskDrawable.setCornerRadius(roundRadius);
* maskDrawable.setStroke(1, Color.WHITE);
* GradientDrawable inviteNormalDrawable = new DrawableUtil.DrawableBuilder(mContext).setGradientRoundRadius(roundRadius)
* .setGradientColors(new int[]{Color.CYAN, Color.LTGRAY})
* .setGradientOrientation(GradientDrawable.Orientation.TOP_BOTTOM)
* .createGradientDrawable();
* GradientDrawable invitePressDrawable = new DrawableUtil.DrawableBuilder(mContext).setGradientRoundRadius(roundRadius)
* .setColor(Color.BLUE).createGradientDrawable();
* StateListDrawable inviteStateListDrawable = new DrawableUtil.DrawableBuilder(mContext).setStateListPressedDrawable(invitePressDrawable)
* .setStateListNormalDrawable(inviteNormalDrawable).createStateListDrawable();
* Drawable rippleDrawable = new DrawableUtil.DrawableBuilder(mContext)
* .setRippleColor(ContextCompat.getColor(mContext, R.color.colorAccent))
* .setRippleNormalDrawable(inviteStateListDrawable)
* .setRippleMaskDrawable(maskDrawable).createRippleDrawable();
* LayerDrawable shadonLayerDrawable = DrawableUtil.createShadowLayerDrawable(roundRadius + shadowColors.length, shadowColors, rippleDrawable);
*
*
* int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
* int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
* border.measure(w, h);
* int height = border.getMeasuredHeight();
* int width = border.getMeasuredWidth();
* //启用时边框背景
* int[] colors = {ContextCompat.getColor(mContext, R.color.colorPrimary),
* ContextCompat.getColor(mContext, R.color.colorPrimary)};
* LinearGradient shader = new LinearGradient(0, 0, width, height, colors, null, Shader.TileMode.MIRROR);
* ShapeDrawable borderEnableBg = new DrawableUtil.DrawableBuilder(mContext).setRingOutRadius(10).setRingInnerRadius(8)
* .setRingShader(shader)
* .setRingSpaceOutAndInner(2).setColor(R.color.colorPrimary).createRingDrawable();
* //禁用时边框背景
* ShapeDrawable borderDisableBg = new DrawableUtil.DrawableBuilder(mContext).setRingOutRadius(10)
* .setRingInnerRadius(8).setRingSpaceOutAndInner(2).setColor(R.color.gray).createRingDrawable();
* //启用时兑换按钮背景
* GradientDrawable convertEnableBg = new DrawableUtil.DrawableBuilder(mContext).setGradientOrientation(GradientDrawable.Orientation.TL_BR)
* .setGradientColors(new int[]{R.color.colorPrimary, R.color.colorPrimary})
* .setGradientType(GradientDrawable.LINEAR_GRADIENT).setGradientRoundRadius(10).createGradientDrawable();
* //禁用时兑换按钮背景
* GradientDrawable convertDisableBg = new DrawableUtil.DrawableBuilder(mContext)
* .setColor(R.color.gray).setGradientRoundRadius(10).createGradientDrawable();
* //初始化输入控件背景(左边圆角)
* ShapeDrawable etBg = new DrawableUtil.DrawableBuilder(mContext).setRingLTOutRadius(10).setRingLBOutRadius(10)
* .setColor(R.color.white).setGradientRoundRadius(10).createRingDrawable();
*/


/**
* 对目标Drawable 进行着色
*/
Expand Down Expand Up @@ -86,6 +144,72 @@ private static Drawable getCanTintDrawable(@NonNull Drawable drawable) {
return DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
}

/**
* 创建带阴影的图层Drawable
*
* @param shadowRoundRadius 阴影半径
* @param shadowColors 阴影颜色数组(一般渐隐递减)
* @param offset 阴影偏移(固定四个数,分别对应左、上、右、下)
* @param drawable 最上层的Drawable
*/
public static LayerDrawable createShadowLayerDrawable(int shadowRoundRadius, int[] shadowColors, int[] offset, Drawable drawable) {
List<Drawable> layers = new ArrayList<>();

//设置阴影层
if (null != shadowColors && shadowColors.length > 0) {
for (int i = 0; i < shadowColors.length; i++) {
//阴影层级颜色,从最外层到最里层
int shadowColor = shadowColors[i];
ShapeDrawable shapeDrawableBg = new ShapeDrawable();

int radius0 = (shadowRoundRadius - ConvertUtils.dp2px(1)) < 0 ? 0 : (shadowRoundRadius - ConvertUtils.dp2px(1));
float[] outerR = new float[]{radius0, radius0, radius0, radius0, radius0, radius0, radius0, radius0};
RoundRectShape roundRectShape0 = new RoundRectShape(outerR, null, null);

shapeDrawableBg.setPadding(ConvertUtils.dp2px(1), ConvertUtils.dp2px(1), ConvertUtils.dp2px(1), ConvertUtils.dp2px(1));
shapeDrawableBg.setShape(roundRectShape0);
// shapeDrawableBg.setShape(new OvalShape());

shapeDrawableBg.getPaint().setStyle(Paint.Style.FILL);
shapeDrawableBg.getPaint().setColor(shadowColor);

layers.add(shapeDrawableBg);
}
}

//设置阴影与顶层Drawable中间的透明过渡层
// int radius1 = (shadowRoundRadius - ConvertUtils.dp2px(shadowColors.length - 1)) < 0 ? 0 : (shadowRoundRadius - ConvertUtils.dp2px(shadowColors.length - 1));
// float[] outerR1 = new float[]{radius1, radius1, radius1, radius1, radius1, radius1, radius1, radius1};
// RoundRectShape roundRectShape1 = new RoundRectShape(outerR1, null, null);
// ShapeDrawable shapeDrawableFg = new ShapeDrawable();
// shapeDrawableFg.setPadding(ConvertUtils.dp2px(1), ConvertUtils.dp2px(1), ConvertUtils.dp2px(1), ConvertUtils.dp2px(1));
// shapeDrawableFg.setShape(roundRectShape1);
// shapeDrawableFg.getPaint().setStyle(Paint.Style.FILL);
// shapeDrawableFg.getPaint().setColor(Color.TRANSPARENT);
// layers.add(shapeDrawableFg);

//设置顶层背景
if (null != drawable) {
layers.add(drawable);
}
LayerDrawable layerDrawable = new LayerDrawable(layers.toArray(new Drawable[layers.size()]));
//设置偏移
if (null != layers && !layers.isEmpty()) {
if (null != offset) {
try {
layerDrawable.setLayerInset(layers.size() - 1, offset[0], offset[1], offset[2], offset[3]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return layerDrawable;
}

public static LayerDrawable createShadowLayerDrawable(int shadowRoundRadius, int[] shadowColors, Drawable drawable) {
return createShadowLayerDrawable(shadowRoundRadius, shadowColors, null, drawable);
}

public static class DrawableBuilder {
//公共参数
private Context mContext;
Expand Down
44 changes: 16 additions & 28 deletions base/src/main/java/com/heyongrui/base/utils/UiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,40 +267,28 @@ public static ColorStateList createColorStateList(int normalColor, int pressedCo
}

/**
* 为单个view设置点击效果,高版本带涟漪反馈
* 设置按钮反馈效果
*
* @param context 上下文
* @param normalColor 未点击的颜色
* @param pressColor 按下的颜色
* @param view 目标view
*/
public static void setOnclickFeedBack(Context context, int normalColor, int pressColor, View view) {
Drawable bgDrawble;
ColorDrawable drawablePressed = new ColorDrawable(pressColor);//分别解析两种颜色为colordrawble
ColorDrawable drawableNormal = new ColorDrawable(normalColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//高版本设置RippleDrawable 而低版本设置 StateListDrawable也就是selector
ColorStateList stateList = ColorStateList.valueOf(pressColor);
bgDrawble = new RippleDrawable(stateList, drawableNormal, drawablePressed);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed);
stateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, drawableNormal);
bgDrawble = stateListDrawable;
}
ViewCompat.setBackground(view, bgDrawble);
}

/**
* 支持同时设置多个view
*
* @param context 上下文
* @param normalColor 正常颜色
* @param pressColor 按下颜色
* @param views 目标view群
*/
public static void setOnclickFeedBack(Context context, @ColorInt int normalColor, @ColorInt int pressColor, View... views) {
public static void setOnclickFeedBack(@ColorInt int normalColor, @ColorInt int pressColor, View... views) {
if (null == views || views.length <= 0) return;
for (View view : views) {
setOnclickFeedBack(context, normalColor, pressColor, view);
Drawable bgDrawble;
ColorDrawable drawablePressed = new ColorDrawable(pressColor);//分别解析两种颜色为colordrawble
ColorDrawable drawableNormal = new ColorDrawable(normalColor);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//高版本设置RippleDrawable 而低版本设置 StateListDrawable也就是selector
ColorStateList stateList = ColorStateList.valueOf(pressColor);
bgDrawble = new RippleDrawable(stateList, drawableNormal, drawablePressed);
} else {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed);
stateListDrawable.addState(new int[]{-android.R.attr.state_pressed}, drawableNormal);
bgDrawble = stateListDrawable;
}
ViewCompat.setBackground(view, bgDrawble);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ExpandableAdapter(List<MultiItemEntity> data) {

@Override
protected void convert(BaseViewHolder helper, MultiItemEntity item) {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
switch (helper.getItemViewType()) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ExpandableAdapter(List<MultiItemEntity> data) {

@Override
protected void convert(BaseViewHolder helper, MultiItemEntity item) {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
switch (helper.getItemViewType()) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected void convertHead(BaseViewHolder helper, ModuleSectionEntity item) {
protected void convert(BaseViewHolder helper, ModuleSectionEntity item) {
switch (helper.getItemViewType()) {
case ModuleSectionEntity.KAIYAN_ONE: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
ImageView ivCover = helper.getView(R.id.iv_cover);
TextView tvDuration = helper.getView(R.id.tv_duration);
ImageView ivAvatar = helper.getView(R.id.iv_avatar);
Expand Down Expand Up @@ -152,7 +152,7 @@ protected void convert(BaseViewHolder helper, ModuleSectionEntity item) {
}
break;
case ModuleSectionEntity.KAIYAN_TWO: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
ImageView ivCover = helper.getView(R.id.iv_cover);
TextView tvDuration = helper.getView(R.id.tv_duration);
TextView tvTitle = helper.getView(R.id.tv_title);
Expand Down Expand Up @@ -280,7 +280,7 @@ protected void convert(BaseViewHolder helper, ModuleSectionEntity item) {
}
break;
case ModuleSectionEntity.TEA_NORMAL: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
ImageView oneAvatarIv = helper.getView(R.id.avatar_iv);
TextView oneNameTv = helper.getView(R.id.name_tv);
TextView oneCategoryTv = helper.getView(R.id.category_tv);
Expand Down Expand Up @@ -341,7 +341,7 @@ protected void convert(BaseViewHolder helper, ModuleSectionEntity item) {
}
break;
case ModuleSectionEntity.TEA_NINE_GRID: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
ImageView twoAvatarIv = helper.getView(R.id.avatar_iv);
TextView twoNameTv = helper.getView(R.id.name_tv);
TextView twoCategoryTv = helper.getView(R.id.category_tv);
Expand Down Expand Up @@ -447,7 +447,7 @@ public void onItemClick(Context context, int position, RatioImageView ratioImage
}
break;
case ModuleSectionEntity.MONO_HISTORY_DATE: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
TextView yearMonthTv = helper.getView(R.id.year_month_tv);
TextView dayTv = helper.getView(R.id.day_tv);
ImageView iconIv = helper.getView(R.id.icon_iv);
Expand All @@ -469,7 +469,7 @@ public void onItemClick(Context context, int position, RatioImageView ratioImage
}
break;
case ModuleSectionEntity.MONO_CATEGORY: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
ImageView categoryThumbIv = helper.getView(R.id.thumb_iv);
TextView categoryMultipleTv = helper.getView(R.id.multiple_tv);
TextView categoryTitleTv = helper.getView(R.id.title_tv);
Expand Down Expand Up @@ -521,7 +521,7 @@ public void onItemClick(Context context, int position, RatioImageView ratioImage
}
break;
case ModuleSectionEntity.POEM: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
TextView tvName = helper.getView(R.id.tv_name);
TextView tvAuthor = helper.getView(R.id.tv_author);
TextView tvContent = helper.getView(R.id.tv_content);
Expand Down Expand Up @@ -572,7 +572,7 @@ public void onItemClick(Context context, int position, RatioImageView ratioImage
}
break;
case ModuleSectionEntity.DOUBAN_MOVIE: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
ImageView ivCover = helper.getView(R.id.iv_cover);
TextView tvName = helper.getView(R.id.tv_name);
RatingBar ratingBar = helper.getView(R.id.rating_bar);
Expand All @@ -598,7 +598,7 @@ public void onItemClick(Context context, int position, RatioImageView ratioImage
}
break;
case ModuleSectionEntity.ZHIHU_NEWS: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
TextView tvTitle = helper.getView(R.id.tv_title);
ImageView ivCover = helper.getView(R.id.iv_cover);

Expand All @@ -616,7 +616,7 @@ public void onItemClick(Context context, int position, RatioImageView ratioImage
}
break;
case ModuleSectionEntity.Q_DAILY_ONE: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);

String cover = "", title = "", desc = "";
Post post = item.getPost();
Expand All @@ -632,7 +632,7 @@ public void onItemClick(Context context, int position, RatioImageView ratioImage
}
break;
case ModuleSectionEntity.Q_DAILY_TWO: {
UiUtil.setOnclickFeedBack(mContext, ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);
UiUtil.setOnclickFeedBack(ContextCompat.getColor(mContext, R.color.background), ContextCompat.getColor(mContext, R.color.gray), helper.itemView);

String cover = "", title = "";
Post post = item.getPost();
Expand Down
Loading

0 comments on commit f73851d

Please sign in to comment.