Sample之MediaEffects

Sample源码阅读学习

Google地址:https://developer.android.com/samples/MediaEffects/project.html

我的注释代码:https://github.com/CL-window/Media-Effects-Framework.git

本项目是 展示OpenGL ES 2.0的纹理图像帧的各种滤镜效果,

主要是 Effect,GLSurfaceView,GLES20的使用,具体都在代码的注释里,贴上主要的代码


// https://code.tutsplus.com/tutorials/how-to-use-android-media-effects-with-opengl-es--cms-23650
public class MediaEffectsFragment extends Fragment implements GLSurfaceView.Renderer {

    private static final String STATE_CURRENT_EFFECT = "current_effect";

    private GLSurfaceView mEffectView;
    private int[] mTextures = new int[2];
    // Media Effects Framework
    private EffectContext mEffectContext; // 用于 getFactory()  EffectFactory
    private Effect mEffect;
    private TextureRenderer mTexRenderer = new TextureRenderer();
    private int mImageWidth;
    private int mImageHeight;
    private boolean mInitialized = false;
    private int mCurrentEffect;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);// 使用菜单
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_media_effects, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mEffectView = (GLSurfaceView) view.findViewById(R.id.effectsview);
        mEffectView.setEGLContextClientVersion(2);// 用OpenGLES 2.0
        mEffectView.setRenderer(this);// Renderer
        mEffectView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);// 只在绘制数据发生改变时才绘制view
        // 下面这段代码处理横竖屏时,会重新 onCreate,配合onSaveInstanceState
        if (null != savedInstanceState && savedInstanceState.containsKey(STATE_CURRENT_EFFECT)) {
            setCurrentEffect(savedInstanceState.getInt(STATE_CURRENT_EFFECT));
        } else {
            setCurrentEffect(R.id.none);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.media_effects, menu);// 菜单view的初始化
    }

    // 菜单的item点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        setCurrentEffect(item.getItemId());
        mEffectView.requestRender();// 手动调用 onDrawFrame
        return true;
    }

    // fragment销毁前,保存需要保存的信息,当前选择的预览模式
    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_CURRENT_EFFECT, mCurrentEffect);
    }

    // Renderer
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
        // Nothing to do here 仅调用一次,用于设置view的OpenGLES环境
    }

    // 如果view的几何形状发生变化了就调用,例如当竖屏变为横屏时
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        if (mTexRenderer != null) {
            mTexRenderer.updateViewSize(width, height);
        }
    }

    // 每次View被重绘时被调用,重点
    @Override
    public void onDrawFrame(GL10 gl) {
        if (!mInitialized) {
            //Only need to do this once  初始化
            mEffectContext = EffectContext.createWithCurrentGlContext();
            mTexRenderer.init();
            loadTextures();// 加载图片文理
            mInitialized = true;
        }
        if (mCurrentEffect != R.id.none) {
            //if an effect is chosen initialize it and apply it to the texture
            initEffect();
            applyEffect();
        }
        renderResult();
    }

    private void setCurrentEffect(int effect) {
        mCurrentEffect = effect;
    }

    private void loadTextures() {
        // Generate textures
        GLES20.glGenTextures(2, mTextures, 0);//  initialize the array

        // Load input bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.puppy);
        mImag
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值