Hello there!
I'm trying to understand some basic android animation programming using Android Studio, and I have followed some tutorials on the internet.
I already know some basic java programming, but I have a problem with my "copy/paste app" I made from youtube.

This is my MainActivity:

public class MainActivity extends ActionBarActivity {

    private Button play;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        play = (Button) findViewById(R.id.play_button);

        play.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent startGame = new Intent("com.abc.test.DRAWGAME2");
                startActivity(startGame);

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

This is DrawGame2.java:

public class DrawGame2 extends Activity implements View.OnTouchListener {

    MyView surfaceView;
    float y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surfaceView = new MyView(this);
        surfaceView.setOnTouchListener(this);
        setContentView(surfaceView);
    }

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

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

    @Override
    public boolean onTouch(View v, MotionEvent me) {

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        switch (me.getAction()){
            case MotionEvent.ACTION_DOWN:
                y = me.getY();
                break;
            case MotionEvent.ACTION_UP:
                y = me.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                y = me.getY();
                break;
        }


        return true;
    }

    public class MyView extends SurfaceView implements Runnable {

        Thread thread = null;
        SurfaceHolder holder;
        boolean isRunning = false;



        public MyView(Context context) {
            super(context);
            holder = getHolder();
            y = 0;
        }

        public void run() {
            while (isRunning) {
                if (!holder.getSurface().isValid()) {
                    continue;
                }
                Canvas canvas = holder.lockCanvas();
                canvas.drawRGB(02,02,150);



                if (y != 0){
                    Bitmap test = BitmapFactory.decodeResource(getResources(),R.drawable.greenball);
                    canvas.drawBitmap(test, canvas.getWidth()/2 - test.getWidth()/2, y, null);
                }



                holder.unlockCanvasAndPost(canvas);
            }
        }

        public void pause() {
            isRunning = false;
            while (true) {
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            }
            thread = null;
        }

        public void resume() {
            isRunning = true;
            thread = new Thread(this);
            thread.start();
        }

    }


}

The problem with this app is that the animation is really laggy, and I can't figure out why!
The problem isn't the OnTouchListener, because I've tried to just increase the y value by 1 with each loop in the run method (while disabling the OnTouchListener), but it is still lagging.
I have tried with different emulators (Lollipop, Kitkat) and on my Samsung Galaxy S3 (Jelly Bean).

Are there any wise people out there that can help me with this problem? :)

Have a great day!

Did you try in an real android device?
For me, even without graphics, simulators were always laggy and slow.

About the graphics itself, I can't say much cause I've never used it.
Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.