import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.opengl.GLException;
import android.opengl.GLSurfaceView;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import java.nio.IntBuffer;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.opengles.GL10;
public class GLSurfaceViewBlur {
public interface BlurCallback {
public void onBlur();
}
/**
* @param context 上下文
* @param glSurfaceView 底层view控件
* @param upperView 需要模糊背景的控件
*/
public static void doBlur(final Context context, final GLSurfaceView glSurfaceView, final ImageView upperView, BlurCallback callback) {
glSurfaceView.queueEvent(new Runnable() {
@Override
public void run() {
EGL10 egl = (EGL10) EGLContext.getEGL();
GL10 gl = (GL10) egl.eglGetCurrentContext().getGL();
Bitmap snapshotBitmap = createBitmapFromGLSurface(0, 0, glSurfaceView.getWidth(), glSurfaceView.getHeight(), gl);
upperView.post(new Runnable() {
@Override
public void run() {
Glide.with(context)
.load(snapshotBitmap).apply(new RequestOptions()
.transforms(new BlurTransformation(context, 25)))
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
upperView.setImageDrawable(resource);
if (callback != null) {
callback.onBlur();
}
}
});
}
});
}
});
}
private static Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
throws OutOfMemoryError {
int bitmapBuffer[] = new int[w * h];
int bitmapSource[] = new int[w * h];
IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
intBuffer.position(0);
try {
Android GLSurfaceView模糊效果
最新推荐文章于 2022-06-13 20:35:23 发布
本文探讨了Android中使用GLSurfaceView时遇到的部分手机出现黑屏的问题,并邀请读者分享更好的解决办法,关注GLSurfaceView的显示优化和效果调整。
摘要由CSDN通过智能技术生成