0
\$\begingroup\$

Below are 5 activities that form my simple app; I still can't figure out why it doesn't show anything(only a black empty screen):

I have another question: Is there any issue with using time in animation function foa an android game: in my case x changes according to cos(10*elapsed)? 1)Main

'''
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        GameView.Screen_WH=dm.widthPixels;
        GameView.Screen_HI=dm.heightPixels;

        setContentView(new GameView(this));
    }
}
'''

2)GameView

'''
import android.content.Context;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GameView extends SurfaceView implements SurfaceHolder.Callback {
    private MainThread thread;
    public OnSwipeTouchListener det;
    public static int Screen_WH;
    public static int Screen_HI;
    private Player player;
    public GameView(Context context) {
        super(context);

        getHolder().addCallback(this);

        thread=new MainThread(getHolder(),this);

        setFocusable(true);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread=new MainThread(getHolder(),this);

        thread.setRunning(true);
        thread.start();

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry=true;
        while(true){
            try {
                thread.setRunning(false);
                thread.join();


            }catch (Exception e) {e.printStackTrace();}
            retry=false;
        }

    }

    //public boolean OnTouchEv


    public void update(){

    }
    @Override
    public  void draw(Canvas canvas) {

        super.draw(canvas);
        player.draw(canvas);
    }
}

'''

3)MainThread

'''
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class MainThread extends Thread {
    public static final int max_fps=30;
    private double av_fps;
    private SurfaceHolder surfaceHolder;
    private GameView gameView;
    private boolean running;
    public static Canvas canvas;
    public void setRunning(boolean running){
        this.running=running;
    }

    public MainThread(SurfaceHolder surfaceHolder,GameView gameView){
        super();
        this.surfaceHolder=surfaceHolder;
        this.gameView=gameView;

    }
    public void run(){
        long startTime;
        long timeMill=1000/max_fps;
        long waitTime;
        long frameCount=0;
        long totalTime=0;
        long TargetTime=1000/max_fps;
        while (running) {
            canvas = null;
            startTime=System.nanoTime();
//ry and catch meanwhile are simply requirements of Java that show we’re willing to try and handle exceptions (errors) that might occur if the canvas isn’t ready etc
            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized (surfaceHolder) {
                    this.gameView.update();
                    this.gameView.draw(canvas);
                }
            } catch (Exception e) {
            } finally {
                if (canvas != null) {
                    try {
                        surfaceHolder.unlockCanvasAndPost(canvas);


                    }catch (Exception e) {e.printStackTrace();}




                }
            }
            timeMill=(System.nanoTime()-startTime)/100000;
            waitTime = (TargetTime - timeMill);
            try {
                if (waitTime>0){
                    this.sleep(waitTime);


                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            totalTime+=System.nanoTime()-startTime;
            frameCount++;
            if(frameCount==max_fps){
                av_fps=1000/((totalTime/frameCount)/100000);
                frameCount=0;
                totalTime=0;
                System.out.println(av_fps);
            }

        }

}
}
'''

4)onSwipeTouchListener

'''
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
class OnSwipeTouchListener implements View.OnTouchListener {
    public boolean onSwipeRight=false;
    public boolean onSwipeLeft=false;
    private GestureDetector gestureDetector;

    OnSwipeTouchListener(Context c) {
        gestureDetector = new GestureDetector(c, new GestureListener());
    }

    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }

    private final class GestureListener extends
            GestureDetector.SimpleOnGestureListener {
        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight=true;
                        } else {
                            onSwipeLeft=true;
                        }
                    }
                }


            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return false;
        }
    }
'''

5)Player

'''
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;
import androidx.core.view.GestureDetectorCompat;

public class Player  {

    private OnSwipeTouchListener det;
    private  long startTime;
    private  Context context;
    private double x=400;
    private double  y=500;
    public Player(double x, double y){
        startTime=System.currentTimeMillis();
        this.x=x;
        this.y=y;
    }



    public void draw(Canvas canvas){
        Drawable pl=ContextCompat.getDrawable(context, R.drawable.mario);
        pl.setBounds((int)x,(int)y,(int)x+20,(int)y-40);//LEFT TOP R B
        pl.draw(canvas);

    }
    public void update(){
        int elapsedTime=(int)(System.currentTimeMillis()-startTime);
        startTime=System.currentTimeMillis();
        if( det.onSwipeRight ){
            x*=Math.abs(Math.cos(elapsedTime*10));
            y*=Math.abs(Math.sin(elapsedTime*10));

        }else if(det.onSwipeLeft){
            x*=Math.abs(Math.cos(elapsedTime*10));
            y*=Math.abs(Math.sin(elapsedTime*10));

        }
    }


    public double  getx(){
        return x;
    }
    public double  gety(){
        return y;
    }



}
'''

XML:

'''
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@android:string/ok"
        android:scaleType="centerCrop"
        android:src="@drawable/cin"
        android:visibility="visible" />

    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#00000000"
        android:visibility="visible" />

</RelativeLayout>
'''

Thank you.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.