OpenGL ES2.0实现按住屏幕平移/拖拽视图Android

本篇博文主要是实现用手指拖动OpenGL ES2.0绘图的屏幕,在使用了正摄投影与平移矩阵相结合,捕捉视图的move事件,将手指滑过的屏幕坐标转为绘图坐标系坐标,根据差值,对OpenGL ES绘图坐标系进行平移,从而得到需要的结果。效果图如下图所示:

核心代码如下:

1主要是计算点击事件的坐标差值,最后传递给Render

 @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent != null) {
            pointerCount = motionEvent.getPointerCount();
            final float normalizedX =
                    (motionEvent.getX() / (float) view.getWidth()) * 2 - 1;
            final float normalizedY =
                    -((motionEvent.getY() / (float) view.getHeight()) * 2 - 1);
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.e(TAG, "点击事件");

                    X = normalizedX;
                    Y = normalizedY;
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.e(TAG, "移动事件");

                    move(normalizedX - X, normalizedY - Y);
                    X = normalizedX;
                    Y = normalizedY;
                    break;
                case MotionEvent.ACTION_UP:
                    Log.e(TAG, "抬起事件");
                    break;
                case MotionEvent.ACTION_SCROLL:
                    Log.e(TAG, "滚动事件");
                    break;
                default:
                    break;
            }
            return true;
        } else {
            return false;
        }

    }

2.进行计算与渲染

package com.map.core;

import android.opengl.GLSurfaceView;
import android.util.Log;

import com.map.brush.Brush;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;
import static android.opengl.Matrix.multiplyMM;
import static android.opengl.Matrix.orthoM;
import static android.opengl.Matrix.setIdentityM;
import static android.opengl.Matrix.translateM;

public final class MapRender implements GLSurfaceView.Renderer {
    private final static String TAG="地图渲染器";

    private final float[] projectionMatrix = new float[16];
    private final float[] modelMatrix = new float[16];
    private List<Brush> brushes;//绘制任务



    public MapRender() {
        this.brushes = new ArrayList<>();
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int width, int height) {
        glViewport(0, 0, width, height);
        //设置支持横竖屏转换,正交投影矩阵
        final float aspectRatio = width > height ?
                (float) width / (float) height :
                (float) height / (float) width;

        if (width > height) {
            // Landscape
            orthoM(projectionMatrix, 0, -aspectRatio, aspectRatio, -1f, 1f, -1f, 1f);
        } else {
            // Portrait or square
            orthoM(projectionMatrix, 0, -1f, 1f, -aspectRatio, aspectRatio, -1f, 1f);
        }
        //初始化平移矩阵为单位矩阵
        setIdentityM(modelMatrix,0);//建立单位矩阵
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        glClear(GL_COLOR_BUFFER_BIT);

        //平移支持
        //两个矩阵相乘
        final float[] temp = new float[16];
        multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);

        Log.e(TAG,"重新绘制");
        for(int i=0;i<brushes.size();i++){
            Brush brush=brushes.get(i);
            brush.draw(temp);
        }
    }

    /**
     * 添加新的绘制任务
     * @param brush 绘制任务
     */
    public void addBrush(Brush brush){
        brushes.add(brush);
    }

    /**
     * 改变绘图坐标系的偏移值
     * @param dx
     * @param dy
     */
    public void move(float dx, float dy){
        translateM(modelMatrix,0,dx,dy,0);//添加平移参数
    }

}

完整的代码在码云https://gitee.com/GISuser/MoveView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GIS开发者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值