DisplayUtils.java
6.47 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.share.mvpsdk.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.share.mvpsdk.R;
import java.io.File;
import jp.wasabeef.glide.transformations.BlurTransformation;
/**
* Created by Horrarndoo on 2017/8/31.
* <p>
* 显示相关工具类
*/
public class DisplayUtils {
/**
* 将px值转换为dp值
*/
public static int px2dp(float pxValue) {
final float scale = AppUtils.getContext().getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将dp值转换为px值
*/
public static int dp2px(float dpValue) {
final float scale = AppUtils.getContext().getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 将px值转换为sp值
*/
public static int px2sp(float pxValue) {
final float scale = AppUtils.getContext().getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将sp值转换为px值
*/
public static int sp2px(float dpValue) {
final float scale = AppUtils.getContext().getResources().getDisplayMetrics().scaledDensity;
return (int) (dpValue * scale + 0.5f);
}
/**
* 获取屏幕宽度
*/
public static int getScreenWidthPixels(Activity context) {
DisplayMetrics metric = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.widthPixels;
}
/**
* 获取屏幕高度
*/
public static int getScreenHeightPixels(Activity context) {
DisplayMetrics metric = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.heightPixels;
}
/**
* 将一个view转换成bitmap位图
*
* @param view 要转换的View
* @return view转换的bitmap
*/
public static Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
view.draw(new Canvas(bitmap));
return bitmap;
}
/**
* 获取模糊虚化的bitmap
*
* @param context
* @param bitmap 要模糊的图片
* @param radius 模糊等级 >=0 && <=25
* @return
*/
public static Bitmap getBlurBitmap(Context context, Bitmap bitmap, int radius) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return blurBitmap(context, bitmap, radius);
}
return bitmap;
}
/**
* android系统的模糊方法
*
* @param bitmap 要模糊的图片
* @param radius 模糊等级 >=0 && <=25
*/
public static Bitmap blurBitmap(Context context, Bitmap bitmap, int radius) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap
.Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(context);
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(radius);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
//recycle the original bitmap
bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
} else {
return bitmap;
}
}
/**
* 显示网络虚化图片
*
* @param context context
* @param imgUrl 图片url
* @param imageView 要显示的imageview
*/
public static void displayBlurImg(Context context, final String imgUrl, ImageView imageView) {
// "23":模糊度;"4":图片缩放4倍后再进行模糊
Glide.with(context)
.load(imgUrl)
.error(R.drawable.stackblur_default)
.placeholder(R.drawable.stackblur_default)
.crossFade(300)
.bitmapTransform(new BlurTransformation(context, 23, 4))
.into(imageView);
}
/**
* 显示本地虚化图片
*
* @param context context
* @param file 本地图片file
* @param imageView 要显示的imageview
*/
public static void displayBlurImg(Context context, File file, ImageView imageView) {
// "23":模糊度;"4":图片缩放4倍后再进行模糊
Glide.with(context)
.load(file)
.error(R.drawable.stackblur_default)
.placeholder(R.drawable.stackblur_default)
.crossFade(300)
.bitmapTransform(new BlurTransformation(context, 23, 4))
.into(imageView);
}
/**
* 显示资源虚化图片
*
* @param context context
* @param resourceId 图片资源id
* @param imageView 要显示的imageview
*/
public static void displayBlurImg(Context context, Integer resourceId, ImageView imageView) {
// "23":模糊度;"4":图片缩放4倍后再进行模糊
Glide.with(context)
.load(resourceId)
.error(R.drawable.stackblur_default)
.placeholder(R.drawable.stackblur_default)
.crossFade(300)
.bitmapTransform(new BlurTransformation(context, 23, 4))
.into(imageView);
}
}