Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
IsKenKenYa opened this issue Apr 13, 2023 · 0 comments
Open

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

IsKenKenYa opened this issue Apr 13, 2023 · 0 comments
Assignees

Comments

@IsKenKenYa
Copy link

在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;
}
@cy745 cy745 self-assigned this May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants