LMusic icon indicating copy to clipboard operation
LMusic copied to clipboard

新增类似Apple Music的模糊歌词效果

Open IsKenKenYa opened this issue 2 years ago • 0 comments

在Android上实现类似于Apple Music的模糊歌词效果,可以使用高斯模糊算法。高斯模糊是一种图片产生模糊效果的算法,它使用正态分布来平滑数据1。

在Android 12中,您可以更轻松地将常用图形效果应用于View上。View中增加了setRenderEffect接口,可以将模糊、色彩滤镜等特效直接应用于View上1。

如果您的应用程序运行在Android 12以下的版本上,您可以使用RenderScript库来实现高斯模糊。它提供了Java层调用的API,实际上是在c/c++层来处理的,所以它的效率和性能通常是最高的2。 —————————— Android 12及以上版本上,您可以使用View中新增的setRenderEffect接口来实现高斯模糊效果。例如,您可以使用以下代码将高斯模糊效果应用于imageView:

imageView.setRenderEffect(RenderEffect.createBlurEffect(3F, 3F, Shader.TileMode.CLAMP)); 如果您的应用程序运行在Android 12以下的版本上,您可以使用RenderScript库来实现高斯模糊。例如,您可以使用以下代码获取模糊后的图片,并将其设置到对应的View中:

private Bitmap getBlurBitmap(int radius, Bitmap bitmap) {
    RenderScript renderScript = RenderScript.create(this);
    Allocation input = Allocation.createFromBitmap(renderScript, bitmap);
    Allocation output = Allocation.createTyped(renderScript, input.getType());
    ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
    scriptIntrinsicBlur.setRadius(radius);
    scriptIntrinsicBlur.setInput(input);
    scriptIntrinsicBlur.forEach(output);
    output.copyTo(bitmap);
    return bitmap;
}

IsKenKenYa avatar Apr 13 '23 00:04 IsKenKenYa