opengl.GLSurfaceView捏缩放

时间:2014-08-27 04:34:32

标签: android opengl-es pinchzoom jpct

这是我的类文件,用于加载3d模型。我正在使用jpct lib加载3d对象。我想将夹点缩放应用到GLSurfaceView可以任何人告诉我该怎么办?我没有找到任何缩放的代码/代码..

public class Model extends Activity {

// Used to handle pause and resume...
private static Model master = null;
ProgressDialog pDialog;
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer fb = null;
static World world = null;
// private RGBColor back = new RGBColor(0, 50, 100);
Connection_Detector cd = new Connection_Detector(this);
private float touchTurn = 0;
private float touchTurnUp = 0;
private float xpos = -1;
private float ypos = -1;
private Object3D cube = null;
private int fps = 0;
private Light sun = null;
Camera mCamera;
SurfaceView mPreview;
String company, model, pid, reponse;
TextView name;
RelativeLayout call, info, email, tryit;
private AsyncTask<Void, Void, Void> details;

boolean IsInternetPresent;
TextureManager tm;
SharedPreferences atPrefs;
ProgressBar pb;
ScaleGestureDetector mScaleDetector;

protected void onCreate(Bundle savedInstanceState) {

    Logger.log("onCreate");

    if (master != null) {
        copy(master);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.model);
    Bundle bundle = getIntent().getExtras();

    ActionBar ab = getActionBar();

    ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f9564b")));
    ab.setDisplayHomeAsUpEnabled(true);
    atPrefs = PreferenceManager.getDefaultSharedPreferences(Model.this);
    mGLView = (GLSurfaceView) findViewById(R.id.glsurfaceview);
    mGLView.setZOrderOnTop(true);
    mScaleDetector = new ScaleGestureDetector(getApplicationContext(),
            new ScaleListener());
    // set bg
    mGLView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    mGLView.getHolder().setFormat(PixelFormat.RGBA_8888);
    renderer = new MyRenderer();
    mGLView.setRenderer(renderer);
    pb = (ProgressBar) findViewById(R.id.progressBar1);
    // pb.setVisibility(View.VISIBLE);
    name = (TextView) findViewById(R.id.name);
    call = (RelativeLayout) findViewById(R.id.callrel);
    info = (RelativeLayout) findViewById(R.id.inforel);
    email = (RelativeLayout) findViewById(R.id.emailrel);
    tryit = (RelativeLayout) findViewById(R.id.tryel);
    name.setText(company + " " + model);
    call.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

        }
    });
    email.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });

    info.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });
    tryit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


        }
    });
}

// public Model() {
// // TODO Auto-generated constructor stub
// mScaleDetector = new ScaleGestureDetector(getApplicationContext(),
// new ScaleListener());


@Override
protected void onPause() {
    super.onPause();
    mGLView.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    mGLView.onResume();
}

@Override
protected void onStop() {
    super.onStop();
}

private void copy(Object src) {
    try {
        Logger.log("Copying data from master Activity!");
        Field[] fs = src.getClass().getDeclaredFields();
        for (Field f : fs) {
            f.setAccessible(true);
            f.set(this, f.get(src));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float mScaleFactor = detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 10.0f));

        // pass it to your renderer
        renderer.setZoom(mScaleFactor);
        Log.d("SCALE", Float.toString(mScaleFactor));
        return true;
    }
}

public boolean onTouchEvent(MotionEvent me) {
    mScaleDetector.onTouchEvent(me);
    // if (me.getAction() == MotionEvent.ACTION_DOWN) {
    // xpos = me.getX();
    // ypos = me.getY();
    // return true;
    // }
    //
    // if (me.getAction() == MotionEvent.ACTION_UP) {
    // xpos = -1;
    // ypos = -1;
    // touchTurn = 0;
    // touchTurnUp = 0;
    // return true;
    // }
    //
    // if (me.getAction() == MotionEvent.ACTION_MOVE) {
    // float xd = me.getX() - xpos;
    // float yd = me.getY() - ypos;
    //
    // xpos = me.getX();
    // ypos = me.getY();
    //
    // touchTurn = xd / -100f;
    // touchTurnUp = yd / -100f;
    // return true;
    // }

    try {
        Thread.sleep(15);
    } catch (Exception e) {
    }

    return super.onTouchEvent(me);
}

protected boolean isFullscreenOpaque() {
    return true;
}

class MyRenderer implements GLSurfaceView.Renderer {

    private long time = System.currentTimeMillis();

    public MyRenderer() {
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        if (fb != null) {
            fb.dispose();
        }

        fb = new FrameBuffer(gl, w, h);
        if (master == null) {

            world = new World();
            world.setAmbientLight(20, 20, 20);

            sun = new Light(world);
            sun.setIntensity(250, 250, 250);
            File sdRoot = Environment.getExternalStorageDirectory();
            File imgFile = new File(sdRoot + "/folder/1.obj");
            InputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            File imgFile = new File(sdRoot + "/folder/1.jpg");
            cube = Object3D.mergeAll(Loader.loadOBJ(inputStream, null, 5f));

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                    .getAbsolutePath());
            tm = TextureManager.getInstance();

            Texture test = new Texture(myBitmap);
            tm.addTexture("text", test);
            // load text
            TextureInfo ti = new TextureInfo(TextureManager.getInstance()
                    .getTextureID("text"));

            cube.setTexture(ti);

            cube.build();
            cube.strip();
            myBitmap.recycle();
            world.addObject(cube);

            //
            Camera cam = world.getCamera();
            cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
            cam.lookAt(cube.getTransformedCenter());
            // runOnUiThread(new Runnable() {
            // @Override
            // public void run() {
            //
            // pb.setVisibility(View.GONE);
            //
            // }
            // });
            SimpleVector sv = new SimpleVector();
            sv.set(cube.getTransformedCenter());
            sv.y -= 100;
            sv.z -= 100;
            sun.setPosition(sv);
            MemoryHelper.compact();

            if (master == null) {
                Logger.log("Saving master Activity!");
                master = Model.this;
            }
        }
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    }

    public void onDrawFrame(GL10 gl) {
        if (touchTurn != 0) {
            cube.rotateY(touchTurn);
            touchTurn = 0;
        }

        if (touchTurnUp != 0) {
            cube.rotateX(touchTurnUp);
            touchTurnUp = 0;
        }

        // fb.clear(back);
        world.renderScene(fb);
        world.draw(fb);
        fb.display();

        if (System.currentTimeMillis() - time >= 1000) {
            Logger.log(fps + "fps");
            fps = 0;
            time = System.currentTimeMillis();
        }
        fps++;
    }

    public void setZoom(float scale) {
        cube.setScale(scale);
    }

}

@Override
public void onBackPressed() {
    Intent back = new Intent(getApplicationContext(), HomeScreen.class);
    startActivity(back);
    // reset 3d model data and text
    master = null;
    tm.removeTexture("text");
    finish();

    super.onBackPressed();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        onBackPressed();

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

 }xml file 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.opengl.GLSurfaceView
    android:id="@+id/glsurfaceview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</android.opengl.GLSurfaceView>

2 个答案:

答案 0 :(得分:0)

在ImageTarget.java上使用scale listener

//imageTragets.java 

    ImageTarget()
    {

    ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());

    }
//in onTouch function 
    onTouch()
    {

    mScaleDetector.onTouchEvent(event);

    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
              float  mScaleFactor *= detector.getScaleFactor();

                // Don't let the object get too small or too large.
                mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 4.0f));

    //pass it to your renderer
                Renderer.setZoom(mScaleFactor);
                Log.d("SCALE",Float.toString(mScaleFactor));
                return true;
            }

        }

获取ImageTargetsRenderer.java中的float值并将其设置为对象

public void setZoom(float scale)
    {
        obj.setScale(scale);
    }

多数民众赞成你可以放大和缩小对象..希望这是你想要的......

答案 1 :(得分:0)

imageTarget.java - 是agument类。 imagetargetRender.java - 它是渲染器

我使用这些类名,所以我给他们了..

对于上面的示例,HelloWorld.java是imageTarget.java,而MyRenderer.java是imageTargetRenderer.java