国际惯例先上图:
QQ音乐效果图:
Demo效果图:
高斯模糊工具类:
/** * <pre> * author : Hansel * e-mail : oysqloveyou@163.com * desc : 毛玻璃工具类 * version: 1.0 * </pre> */public class BlurUtil { /** * 获取高斯模糊Drawable * * @param bitmap bitmap * @param scale 缩放比例 * @param radius 缩放半径 * @param mContext mContext * @return Single<Drawable> */ public static Single<Drawable> getForegroundDrawable(Bitmap bitmap, int scale, int radius, Context mContext) { return Single.create((Single.OnSubscribe<Drawable>) ex -> { /*得到屏幕的宽高比,以便按比例切割图片一部分*/ final float wIDthHeightSize = (float) (ScreenUtils.getScreenWIDth(mContext) * 1.0 / ScreenUtils.getScreenHeight(mContext) * 1.0); int cropBitmapWIDth; if (wIDthHeightSize <= 1) { cropBitmapWIDth = (int) (wIDthHeightSize * bitmap.getHeight()); } else { cropBitmapWIDth = (int) (bitmap.getHeight() / wIDthHeightSize); } int cropBitmapWIDthX = (int) ((bitmap.getWIDth() - cropBitmapWIDth) / 2.0); /*切割部分图片*/ Bitmap cropBitmap = Bitmap.createBitmap(bitmap, cropBitmapWIDthX, 0, cropBitmapWIDth, bitmap.getHeight()); /*缩小图片*/ Bitmap scaleBitmap = Bitmap.createScaledBitmap(cropBitmap, bitmap.getWIDth() / scale, bitmap .getHeight() / scale, false); /*模糊化*/ final Bitmap blurBitmap = blurBitmap(scaleBitmap, radius, mContext); final Drawable foregroundDrawable = new BitmapDrawable(null, blurBitmap); /*加入灰色遮罩层,避免图片过亮影响其他控件*/ foregroundDrawable.setcolorFilter(color.GRAY, PorterDuff.Mode.MulTIPLY); ex.onSuccess(foregroundDrawable); }) .compose(RxUtil.ioHelper()); } /** * 高斯模糊 * * @param bitmap bitmap * @param radius radius range 0 < radius <= 25 * @param context context * @return Bitmap */ private static Bitmap blurBitmap(Bitmap bitmap, float radius, Context context) { //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 androID.renderscript.RenderScript rs = androID.renderscript.RenderScript.create(context); //Create an Intrinsic Blur Script using the Renderscript androID.renderscript.ScriptIntrinsicBlur blurScript = androID.renderscript.ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps androID.renderscript.Allocation allin = androID.renderscript.Allocation.createFromBitmap(rs, bitmap); androID.renderscript.Allocation allOut = androID.renderscript.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; }}备注:Single是RxAndroID类,ScreenUtils.getScreenWIDth是自己写的获取屏幕宽度类,自己写很简单~,注释应该很详细了,高斯模糊so easy~!.
总结 以上是内存溢出为你收集整理的利用Android原生RenderScript实现仿网易云、QQ音乐播放界面效果全部内容,希望文章能够帮你解决利用Android原生RenderScript实现仿网易云、QQ音乐播放界面效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)