其他分享
首页 > 其他分享> > 利用Android原生RenderScript实现仿网易云、QQ音乐播放界面效果

利用Android原生RenderScript实现仿网易云、QQ音乐播放界面效果

作者:互联网

国际惯例先上图:
QQ音乐效果图:
qq.png
Demo效果图:
c.png

高斯模糊工具类:

/**
 * <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~!.

标签:QQ,mContext,1.0,int,RenderScript,param,bitmap,Single,Android
来源: https://blog.csdn.net/qq_20613149/article/details/100373212