Skip to content

Commit 8f75e99

Browse files
committed
新增promptTopDrawable和promptTopBitmap方法,支持顶部图片设置Drawable和Bitmap
1 parent 764c09d commit 8f75e99

File tree

7 files changed

+108
-4
lines changed

7 files changed

+108
-4
lines changed

app/src/main/java/com/xuexiang/xupdatedemo/fragment/AdvancedUseFragment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ protected void onItemClick(int position) {
7979
.updateUrl("/xuexiangjys/XUpdate/raw/master/jsonapi/update_test.json")
8080
.promptThemeColor(ResUtils.getColor(R.color.update_theme_color))
8181
.promptButtonTextColor(Color.WHITE)
82-
.promptTopResId(R.mipmap.bg_update_top)
82+
.promptTopDrawable(ResUtils.getDrawable(getContext(), R.mipmap.bg_update_top))
83+
// .promptTopResId(R.mipmap.bg_update_top)
8384
.promptWidthRatio(0.7F)
8485
.update();
8586
break;

xupdate-lib/src/main/java/com/xuexiang/xupdate/UpdateManager.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package com.xuexiang.xupdate;
1818

1919
import android.content.Context;
20+
import android.graphics.Bitmap;
21+
import android.graphics.drawable.BitmapDrawable;
22+
import android.graphics.drawable.Drawable;
2023
import android.text.TextUtils;
2124

2225
import androidx.annotation.ColorInt;
@@ -719,6 +722,34 @@ public Builder promptTopResId(@DrawableRes int topResId) {
719722
return this;
720723
}
721724

725+
/**
726+
* 设置顶部背景图片
727+
*
728+
* @param topDrawable 顶部背景图片
729+
* @return this
730+
*/
731+
public Builder promptTopDrawable(Drawable topDrawable) {
732+
if (topDrawable != null) {
733+
String tag = _XUpdate.saveTopDrawable(topDrawable);
734+
promptEntity.setTopDrawableTag(tag);
735+
}
736+
return this;
737+
}
738+
739+
/**
740+
* 设置顶部背景图片
741+
*
742+
* @param topBitmap 顶部背景图片
743+
* @return this
744+
*/
745+
public Builder promptTopBitmap(Bitmap topBitmap) {
746+
if (topBitmap != null) {
747+
String tag = _XUpdate.saveTopDrawable(new BitmapDrawable(context.getResources(), topBitmap));
748+
promptEntity.setTopDrawableTag(tag);
749+
}
750+
return this;
751+
}
752+
722753
/**
723754
* 设置按钮的文字颜色
724755
*

xupdate-lib/src/main/java/com/xuexiang/xupdate/_XUpdate.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package com.xuexiang.xupdate;
1818

1919
import android.content.Context;
20+
import android.graphics.drawable.Drawable;
2021
import android.os.Handler;
2122
import android.os.Looper;
2223
import android.text.TextUtils;
24+
import android.util.LruCache;
2325

2426
import androidx.annotation.NonNull;
2527

@@ -40,6 +42,7 @@
4042

4143
import java.io.File;
4244
import java.util.Map;
45+
import java.util.UUID;
4346
import java.util.concurrent.ConcurrentHashMap;
4447

4548
import static com.xuexiang.xupdate.entity.UpdateError.ERROR.INSTALL_FAILED;
@@ -65,6 +68,11 @@ public final class _XUpdate {
6568
*/
6669
private static Map<String, Runnable> sWaitRunnableMap = new ConcurrentHashMap<>();
6770

71+
/**
72+
* 存储顶部图片资源
73+
*/
74+
private static LruCache<String, Drawable> sTopDrawableCache = new LruCache<>(4);
75+
6876
private static Handler sMainHandler = new Handler(Looper.getMainLooper());
6977

7078
/**
@@ -137,6 +145,31 @@ public static boolean isPrompterShow(String url) {
137145
return isShow != null && isShow;
138146
}
139147

148+
/**
149+
* 保存顶部背景图片
150+
*
151+
* @param drawable 图片
152+
* @return 图片标识
153+
*/
154+
public static String saveTopDrawable(Drawable drawable) {
155+
String tag = UUID.randomUUID().toString();
156+
sTopDrawableCache.put(tag, drawable);
157+
return tag;
158+
}
159+
160+
/**
161+
* 获取顶部背景图片
162+
*
163+
* @param drawableTag 图片标识
164+
* @return 顶部背景图片
165+
*/
166+
public static Drawable getTopDrawable(String drawableTag) {
167+
if (TextUtils.isEmpty(drawableTag)) {
168+
return null;
169+
}
170+
return sTopDrawableCache.get(drawableTag);
171+
}
172+
140173
//===========================属性设置===================================//
141174

142175
public static Map<String, Object> getParams() {

xupdate-lib/src/main/java/com/xuexiang/xupdate/entity/PromptEntity.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public class PromptEntity implements Parcelable {
2424
*/
2525
@DrawableRes
2626
private int mTopResId;
27+
/**
28+
* 顶部背景图片Drawable标识
29+
*/
30+
private String mTopDrawableTag;
2731
/**
2832
* 按钮文字颜色
2933
*/
@@ -49,6 +53,7 @@ public class PromptEntity implements Parcelable {
4953
public PromptEntity() {
5054
mThemeColor = -1;
5155
mTopResId = -1;
56+
mTopDrawableTag = "";
5257
mButtonTextColor = 0;
5358
mSupportBackgroundUpdate = false;
5459
mWidthRatio = -1;
@@ -59,6 +64,7 @@ public PromptEntity() {
5964
protected PromptEntity(Parcel in) {
6065
mThemeColor = in.readInt();
6166
mTopResId = in.readInt();
67+
mTopDrawableTag = in.readString();
6268
mButtonTextColor = in.readInt();
6369
mSupportBackgroundUpdate = in.readByte() != 0;
6470
mWidthRatio = in.readFloat();
@@ -96,6 +102,15 @@ public PromptEntity setTopResId(int topResId) {
96102
return this;
97103
}
98104

105+
public String getTopDrawableTag() {
106+
return mTopDrawableTag;
107+
}
108+
109+
public PromptEntity setTopDrawableTag(String topDrawableTag) {
110+
mTopDrawableTag = topDrawableTag;
111+
return this;
112+
}
113+
99114
public int getButtonTextColor() {
100115
return mButtonTextColor;
101116
}
@@ -146,6 +161,7 @@ public String toString() {
146161
return "PromptEntity{" +
147162
"mThemeColor=" + mThemeColor +
148163
", mTopResId=" + mTopResId +
164+
", mTopDrawableTag=" + mTopDrawableTag +
149165
", mButtonTextColor=" + mButtonTextColor +
150166
", mSupportBackgroundUpdate=" + mSupportBackgroundUpdate +
151167
", mWidthRatio=" + mWidthRatio +
@@ -163,6 +179,7 @@ public int describeContents() {
163179
public void writeToParcel(Parcel dest, int flags) {
164180
dest.writeInt(mThemeColor);
165181
dest.writeInt(mTopResId);
182+
dest.writeString(mTopDrawableTag);
166183
dest.writeInt(mButtonTextColor);
167184
dest.writeByte((byte) (mSupportBackgroundUpdate ? 1 : 0));
168185
dest.writeFloat(mWidthRatio);

xupdate-lib/src/main/java/com/xuexiang/xupdate/widget/UpdateDialog.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.Context;
2222
import android.content.pm.PackageManager;
2323
import android.graphics.Color;
24+
import android.graphics.drawable.Drawable;
2425
import android.util.DisplayMetrics;
2526
import android.view.View;
2627
import android.view.Window;
@@ -249,14 +250,23 @@ private void initTheme(@ColorInt int themeColor, @DrawableRes int topResId, @Col
249250
* @param heightRatio 高和屏幕的比例
250251
*/
251252
private void setDialogTheme(int themeColor, int topResId, int buttonTextColor, float widthRatio, float heightRatio) {
252-
mIvTop.setImageResource(topResId);
253+
Drawable topDrawable = _XUpdate.getTopDrawable(mPromptEntity.getTopDrawableTag());
254+
if (topDrawable != null) {
255+
mIvTop.setImageDrawable(topDrawable);
256+
} else {
257+
mIvTop.setImageResource(topResId);
258+
}
253259
DrawableUtils.setBackgroundCompat(mBtnUpdate, DrawableUtils.getDrawable(UpdateUtils.dip2px(4, getContext()), themeColor));
254260
DrawableUtils.setBackgroundCompat(mBtnBackgroundUpdate, DrawableUtils.getDrawable(UpdateUtils.dip2px(4, getContext()), themeColor));
255261
mNumberProgressBar.setProgressTextColor(themeColor);
256262
mNumberProgressBar.setReachedBarColor(themeColor);
257263
mBtnUpdate.setTextColor(buttonTextColor);
258264
mBtnBackgroundUpdate.setTextColor(buttonTextColor);
259265

266+
initWindow(widthRatio, heightRatio);
267+
}
268+
269+
private void initWindow(float widthRatio, float heightRatio) {
260270
Window window = getWindow();
261271
if (window == null) {
262272
return;

xupdate-lib/src/main/java/com/xuexiang/xupdate/widget/UpdateDialogActivity.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Intent;
77
import android.content.pm.PackageManager;
88
import android.graphics.Color;
9+
import android.graphics.drawable.Drawable;
910
import android.os.Bundle;
1011
import android.util.DisplayMetrics;
1112
import android.view.Gravity;
@@ -234,7 +235,12 @@ private void initTheme(@ColorInt int themeColor, @DrawableRes int topResId, @Col
234235
* @param topResId 图片
235236
*/
236237
private void setDialogTheme(int themeColor, int topResId, int buttonTextColor) {
237-
mIvTop.setImageResource(topResId);
238+
Drawable topDrawable = _XUpdate.getTopDrawable(mPromptEntity.getTopDrawableTag());
239+
if (topDrawable != null) {
240+
mIvTop.setImageDrawable(topDrawable);
241+
} else {
242+
mIvTop.setImageResource(topResId);
243+
}
238244
DrawableUtils.setBackgroundCompat(mBtnUpdate, DrawableUtils.getDrawable(UpdateUtils.dip2px(4, this), themeColor));
239245
DrawableUtils.setBackgroundCompat(mBtnBackgroundUpdate, DrawableUtils.getDrawable(UpdateUtils.dip2px(4, this), themeColor));
240246
mNumberProgressBar.setProgressTextColor(themeColor);

xupdate-lib/src/main/java/com/xuexiang/xupdate/widget/UpdateDialogFragment.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.content.pm.PackageManager;
2323
import android.content.res.Configuration;
2424
import android.graphics.Color;
25+
import android.graphics.drawable.Drawable;
2526
import android.os.Build;
2627
import android.os.Bundle;
2728
import android.util.DisplayMetrics;
@@ -321,7 +322,12 @@ private void initTheme(@ColorInt int themeColor, @DrawableRes int topResId, @Col
321322
* @param topResId 图片
322323
*/
323324
private void setDialogTheme(int themeColor, int topResId, int buttonTextColor) {
324-
mIvTop.setImageResource(topResId);
325+
Drawable topDrawable = _XUpdate.getTopDrawable(mPromptEntity.getTopDrawableTag());
326+
if (topDrawable != null) {
327+
mIvTop.setImageDrawable(topDrawable);
328+
} else {
329+
mIvTop.setImageResource(topResId);
330+
}
325331
DrawableUtils.setBackgroundCompat(mBtnUpdate, DrawableUtils.getDrawable(UpdateUtils.dip2px(4, getContext()), themeColor));
326332
DrawableUtils.setBackgroundCompat(mBtnBackgroundUpdate, DrawableUtils.getDrawable(UpdateUtils.dip2px(4, getContext()), themeColor));
327333
mNumberProgressBar.setProgressTextColor(themeColor);

0 commit comments

Comments
 (0)