Android Studio - 第四十七期 毛玻璃效果以及动态生成二维码以及增大点击热区
最近回看撸撸的代码,有一些自定义的view写法很不错,下面封装出来,希望能帮到大家:
1.毛玻璃效果:BitmapUtils
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
|
package com.example.p030_popbgqcode.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.View;
public class BitmapUtils {
/**
* 截图
* @param view
* @return
*/
public static Bitmap takeScreenShot(View view) {
view.setDrawingCacheEnabled( true );
view.buildDrawingCache( true );
Bitmap res = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled( false );
return res;
}
/**
* 模糊
* @param context
* @param src
* @return
*/
public static Bitmap blur(Context context, Bitmap src) {
Bitmap out = Bitmap.createBitmap(src);
// 创建RenderScript内核对象
RenderScript script = RenderScript.create(context);
// 创建一个模糊效果的RenderScript的工具对象
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(script, Element.U8_4(script));
// 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间。
// 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去。
Allocation inAllo = Allocation.createFromBitmap(script, src);
Allocation outAllo = Allocation.createFromBitmap(script, out);
// 设置渲染的模糊程度, 25f是最大模糊度
blur.setRadius(25f);
// 设置blurScript对象的输入内存
blur.setInput(inAllo);
// 将输出数据保存到输出内存中
blur.forEach(outAllo);
// 将数据填充到Allocation中
outAllo.copyTo(out);
return out;
}
} |
PopWindows使用方法:
1
2
|
Bitmap shot = BitmapUtils.takeScreenShot(activity.getWindow().getDecorView()); Bitmap blur = BitmapUtils.blur(activity, shot); |
2.动态生成二维码:QrCodeUtil
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
|
package com.example.p030_popbgqcode.utils;
import android.graphics.Bitmap;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.util.Hashtable;
public class QrCodeUtil {
private static int IMAGE_HALFWIDTH = 50 ; //宽度值,影响中间图片大小
private static final int DEFAULT_SIZE = 500 ;
/**
* 生成二维码,默认大小为500*500
*
* @param text 需要生成二维码的文字、网址等
* @return bitmap
*/
public static void createQRCode(ImageView iv, String text) {
createQRCode(iv, text, DEFAULT_SIZE);
}
/**
* 生成二维码
*
* @param text 需要生成二维码的文字、网址等
* @param size 需要生成二维码的大小()
* @return bitmap
*/
public static void createQRCode( final ImageView iv, final String text, final int size) {
new Thread() {
@Override
public void run() {
super .run();
try {
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8" );
BitMatrix bitMatrix = new QRCodeWriter().encode(text,
BarcodeFormat.QR_CODE, size, size, hints);
int [] pixels = new int [size * size];
for ( int y = 0 ; y < size; y++) {
for ( int x = 0 ; x < size; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000 ;
} else {
pixels[y * size + x] = 0xffffffff ;
}
}
}
sleep( 500 );
final Bitmap bitmap = Bitmap.createBitmap(size, size,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0 , size, 0 , 0 , size, size);
iv.post( new Runnable() {
@Override
public void run() {
if (iv != null ) {
iv.setImageBitmap(bitmap);
}
}
});
} catch (WriterException e) {
e.printStackTrace();
ToastUtil.showToastShort( "creat code err" );
} catch (InterruptedException e) {
ToastUtil.showToastShort( "creat code err" );
e.printStackTrace();
}
}
}.start();
}
} |
使用方法:
1
|
QrCodeUtil.createQRCode(pc_iv1, str, 300 );
|
3.增大点击热区:ExpandViewRectUtils
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
|
package com.example.p030_popbgqcode.utils;
import android.graphics.Rect;
import android.view.TouchDelegate;
import android.view.View;
public class ExpandViewRectUtils {
/**
* 增大反应热区
* @param view view
* @param top 增大上部热区
* @param bottom 增大下部热区
* @param left 增大左部热区
* @param right 增大右部热区
*/
public static void expandViewTouchDelegate( final View view, final int top, final int bottom, final int left, final int right) {
((View) view.getParent()).post( new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
view.setEnabled( true );
view.getHitRect(bounds);
bounds.top -= top;
bounds.bottom += bottom;
bounds.left -= left;
bounds.right += right;
TouchDelegate touchDelegate = new TouchDelegate(bounds, view);
if (View. class .isInstance(view.getParent())) {
((View) view.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
/**
* 还原View的触摸和点击响应范围,最小不小于View自身范围
*
* @param view
*/
public static void restoreViewTouchDelegate( final View view) {
((View) view.getParent()).post( new Runnable() {
@Override
public void run() {
Rect bounds = new Rect();
bounds.setEmpty();
TouchDelegate touchDelegate = new TouchDelegate(bounds, view);
if (View. class .isInstance(view.getParent())) {
((View) view.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
} |
使用方法:
1
|
ExpandViewRectUtils.expandViewTouchDelegate(tv1, 10 , 10 , 10 , 10 );
|
效果如下图:
地址:https://github.com/geeklx/MyApplication/tree/master/p030_popbgqcode
另附图:
本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1954841,如需转载请自行联系原作者