最近回看擼擼的代碼,有一些自定義的view寫法很不錯,下面封裝出來,但願能幫到你們:
java
1.毛玻璃效果:BitmapUtilsandroid
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使用方法:
git
Bitmap shot = BitmapUtils.takeScreenShot(activity.getWindow().getDecorView()); Bitmap blur = BitmapUtils.blur(activity, shot);
2.動態生成二維碼:QrCodeUtilgithub
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(); } }
使用方法:
ide
QrCodeUtil.createQRCode(pc_iv1, str, 300);
3.增大點擊熱區:ExpandViewRectUtils工具
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); } } }); } }
使用方法:
post
ExpandViewRectUtils.expandViewTouchDelegate(tv1, 10, 10, 10, 10);
效果以下圖:
ui
地址:https://github.com/geeklx/MyApplication/tree/master/p030_popbgqcode google
另附圖:
spa