Android Open Source - Planets-App Sun G L Activity






From Project

Back to project page Planets-App.

License

The source code is released under:

Apache License

If you think the Android project Planets-App listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.andrewq.planets;
// w ww  .j a  va 2 s.  c o  m
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.VelocityTracker;
import android.view.WindowManager;

/**
 * Created by Andrew Quebe on 4/8/14.
 */
public class SunGLActivity extends Activity {
    private final Handler mHandler = new Handler();
    private SunSurfaceView mGLView = null;
    private final Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            if (mGLView != null) {
                if (mGLView.mRenderer != null) {
                    float fps = mGLView.mRenderer.fps;
                    setTitle(String.format("%.1f FPS", fps));
                }
            }

            mHandler.postDelayed(this, 1000);
        }
    };
    private ActionBar mActionbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActionbar = getActionBar();

        mActionbar.hide();

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mGLView = new SunSurfaceView(this);
        setContentView(mGLView);

        mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 1000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mGLView != null)
            mGLView.onPause();
        mHandler.removeCallbacks(mUpdateTimeTask);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mGLView != null)
            mGLView.onResume();
        mHandler.removeCallbacks(mUpdateTimeTask);
        mHandler.postDelayed(mUpdateTimeTask, 1000);
    }
}

class SunSurfaceView extends GLSurfaceView {
    public SunRenderer mRenderer = null;
    private ScaleGestureDetector scaleDetector = null;
    private VelocityTracker velocityTracker = null;
    private boolean isScale = false;
    private float mPreviousX = 0;
    private float mPreviousY = 0;

    public SunSurfaceView(Context context) {
        super(context);

        setEGLContextClientVersion(2);
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);

        mRenderer = new SunRenderer(context);
        setRenderer(mRenderer);

        scaleDetector = new ScaleGestureDetector(context, new MyGestureListener());
        velocityTracker = VelocityTracker.obtain();
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        velocityTracker.addMovement(e);

        float x = e.getX(0);
        float y = e.getY(0);

        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isScale = false;
                mRenderer.rotateSpeed = 0;
                mRenderer.tiltSpeed = 0;
                break;

            case MotionEvent.ACTION_UP:
                velocityTracker.computeCurrentVelocity(1000);
                mRenderer.rotateSpeed = -velocityTracker.getXVelocity() / (mRenderer.screenWidth * mRenderer.ratioX * mRenderer.scaleFactor * Math.PI);
                mRenderer.tiltSpeed = velocityTracker.getYVelocity() * 0.5f / (mRenderer.screenHeight * mRenderer.ratioY * mRenderer.scaleFactor);
                break;

            case MotionEvent.ACTION_MOVE:
                if (!isScale) {
                    float dx = x - mPreviousX;
                    float dy = y - mPreviousY;
                    mRenderer.rotateAngle -= dx / (mRenderer.screenWidth * mRenderer.ratioX * mRenderer.scaleFactor * Math.PI);
                    mRenderer.tiltAngle += dy * 0.5f / (mRenderer.screenHeight * mRenderer.ratioY * mRenderer.scaleFactor);
                }
                break;
        }

        mPreviousX = x;
        mPreviousY = y;

        return scaleDetector.onTouchEvent(e);
    }

    private class MyGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mRenderer.scaleFactor *= detector.getScaleFactor();
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            isScale = true;
        }
    }
}




Java Source Code List

com.andrewq.planets.CustomAdapter.java
com.andrewq.planets.Deimos.java
com.andrewq.planets.EclairMotionEvent.java
com.andrewq.planets.FragmentA.java
com.andrewq.planets.FragmentB.java
com.andrewq.planets.FragmentC.java
com.andrewq.planets.FragmentD.java
com.andrewq.planets.FragmentE.java
com.andrewq.planets.FragmentF.java
com.andrewq.planets.FragmentG.java
com.andrewq.planets.FragmentH.java
com.andrewq.planets.FragmentHome.java
com.andrewq.planets.FragmentI.java
com.andrewq.planets.FragmentPlanets.java
com.andrewq.planets.ImageGallery.java
com.andrewq.planets.Phobos.java
com.andrewq.planets.RandomQuotes.java
com.andrewq.planets.RowItem.java
com.andrewq.planets.SatelliteEarth.java
com.andrewq.planets.Settings.java
com.andrewq.planets.SunGLActivity.java
com.andrewq.planets.SunRenderer.java
com.andrewq.planets.TouchImageView.java
com.andrewq.planets.VenusRenderer.java
com.andrewq.planets.WrapMotionEvent.java
com.obsidian.planets.Main.java