gddhy

_(:з」∠)_ 加载中...
  • 主页
  • 归档
  • 工具
  • 关于
所有文章 友链

gddhy

_(:з」∠)_ 加载中...

  • 主页
  • 归档
  • 工具
  • 关于

气泡吐司

2020-12-19
字数统计:857字 阅读时长≈4分

  开始看到吐司提示,是在添加在线卡面时,看到 奶油话梅糖 介绍里带了自己的博客,好奇进去看了下,看到这个感觉不错就收藏了

  当时想到了一些用法,但也没急着用,看到设置随机壁纸时只有冷冰冰的”设置中”提示,感觉不太好,就用在这里了,将读到的图片设置壁纸的同时提示一下
  一直没时间改代码,一直拖到最近才弄了下,在 奶油话梅糖 的基础上添加图片提示

  实例程序随机壁纸 ,下面是源码


DiyToast.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
public class DiyToast {
public static void showToast(Context context, String string, Uri bitmapUri) {
showToast(context,string,MainActivity.getBitmapFromUri(bitmapUri,context));
}

public static void showToast(Context context, String string){
View toastRoot = LayoutInflater.from(context).inflate(
R.layout.my_toast, null, false);
TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
Toast toast = Toast.makeText(context, string, Toast.LENGTH_LONG);// 展示Toast
toast.setView(toastRoot);
tv.setText(string);
toast.show();
}

public static void showToast(Context context, String string, final Bitmap bitmap) {
View toastRoot = LayoutInflater.from(context).inflate(
R.layout.my_toast, null, false);
TextView tv = (TextView) toastRoot.findViewById(R.id.TextViewInfo);
LinearLayout linearLayout =toastRoot.findViewById(R.id.rootLayout);
Toast toast = Toast.makeText(context, string, Toast.LENGTH_SHORT);// 展示Toast
toast.setView(toastRoot);
tv.setText(string);
tv.setTextColor(getBitmapColorIsDark(bitmap)?Color.WHITE:Color.BLACK);

int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 设置想要的大小
int newWidth;
int newHeight;
if(width>height){
newWidth = 720;
newHeight = height*720/width;
} else {
newWidth = width*720/height;
newHeight = 720;
}

// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap mbitmap = Bitmap.createBitmap(toRoundCorner(bitmap,50), 0, 0, width, height, matrix, true);

Resources resources = context.getResources();
BitmapDrawable drawable = new BitmapDrawable(resources, mbitmap);

linearLayout.setBackground(drawable);

toast.show();
}


//http://hautxsh.iteye.com/blog/1495012
private static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}


//获取图片字体颜色 黑false 白true
private static boolean getBitmapColorIsDark (Bitmap bitmap){
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] color = new int[5];
color[0] = bitmap.getPixel(w / 2, h / 2);
color[1] = bitmap.getPixel(w / 4, h / 4);
color[2] = bitmap.getPixel(w / 4, h * 3 / 4);
color[3] = bitmap.getPixel(w * 3 / 4, h / 4);
color[4] = bitmap.getPixel(w * 3 / 4, h * 3 / 4);
int c = 0;
for (int i = 0; i <= 4; i++) {
if (isDark(color[i])) c++;
else c--;
}
return c >= 0;
}

//判断颜色深浅
private static boolean isDark(int color) {
Double r,g,b;
r = (double) Color.red(color);
g = (double) Color.green(color);
b = (double) Color.blue(color);
return isDark(r,g,b);
}

/**
* 根据RGB值判断 深色与浅色
* @param r
* @param g
* @param b
* @return
*/
private static boolean isDark(Double r, Double g, Double b){
return !(r * 0.299 + g * 0.578 + b * 0.114 >= 192);
}

}

res/layout/my_toast.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shaper_toast_show_back"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15sp"
android:gravity="bottom|center"
android:orientation="vertical" >
<TextView
android:id="@+id/TextViewInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15sp"
android:layout_marginTop="15sp"
android:text="text"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

res/drawable/shaper_toast_show_back.xml

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="15sp" />
<solid android:color="#3399FF" />
<stroke
android:width="8sp"
android:color="#70FFFFFF" />
</shape>

源程序来自短代码-带有背景的Toast - 话梅糖の随笔 有修改

赏

谢谢你请我吃糖果

微信
  • Android
  • Java

扫一扫,分享到微信

微信分享二维码
老毛子固件允许同级设备访问smb
浏览器UA检测
目录,不存在的…
留言已关闭
:gddhy
© gddhy
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链

tag:

  • Android
  • 软件分享
  • game
  • Hexo
  • JavaScript
  • 旧机博物馆
  • MIUI
  • Java
  • git
  • Termux
  • mtk
  • 原神
  • Win
  • Html
  • 安卓学习笔记

    缺失模块

  • Luminous' Home
  • 影子博客
  • 四次元领域
  • 初之音
  • Mr.Pumpkin
  • ZhaoQuinn 's Blog