2 Replies - 9113 Views - Last Post: 27 February 2013 - 08:23 PM

#1 redpepper303   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-August 12

How to make android app (animation) run faster/smoother?

Posted 27 February 2013 - 11:33 AM

Hello :)/>, i'm an "ok" female java programmer, though relatively new to android. I've started the development of a guitar application, and am stuck on a graphical aspect. My screen contains a background image, which you can move with 2 fingers, by sliding up or down. My background image, a bitmap, moves at low fps on an android device. I'm looking to see how to make it run smoother.

Code details:
I have 3 classes: one main activity, a second for graphics, and a third as a splash screen (I have't included the last one as it has nothing to do with my problems).


    package com.example.androidapplication;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    public class MainActivity extends Activity implements OnTouchListener{
    	FretBoard ourView;
    	float y1, y2, dy;
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		ourView = new FretBoard(this);
    	//Method to be used in future
    	protected void onpause() {
    		// TODO Auto-generated method stub
    	//Method to be used in future
    	protected void onresume() {
    		// TODO Auto-generated method stub
    	public boolean onTouch(View v, MotionEvent event){
    		//For debugging
    		if(event!= null) ourView.fingerCount = event.getPointerCount();
    			case MotionEvent.ACTION_DOWN:
    				//Save first y position when finger touches screen
    				y1 = event.getY();
    			case MotionEvent.ACTION_MOVE:
    				if(event != null && event.getPointerCount() == 2){
    					//Save second y position when finger moves
    					y2 = event.getY();
    					//Total distance moved
    					dy = y2 - y1;
    					if((ourView.bgY + dy)<0) ourView.bgY += dy/2;
    					//For Debugging
    					System.out.println("Y1 : " + y1 + "     Y2 : " + y2                +   "   DY : " + dy);
    					//Redraw the Screen 
    					//Reset y1 
    					y1 = y2;
    		return true;


    package com.example.androidapplication;
    import java.io.InputStream;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    public class FretBoard extends SurfaceView implements Runnable{
    	//Background image variables
    	Bitmap background;
    	InputStream is = getResources().openRawResource(R.drawable.fret_board);
    	//Debugger text variables
    	Paint mPaint = new Paint();
    	String string = "Fingers: ";
    	int fingerCount = 0;
    	//Position Variables
    	int width, height;
    	float bgY = 0, bgX = 0;
    	//Holder variable
    	SurfaceHolder ourHolder;
    	//To be used in future
    	Thread ourThread = null;
    	Boolean isRunning = false;
    	public FretBoard(Context context) {
    		// TODO Auto-generated constructor stub
    		ourHolder = getHolder();
    		ourThread = new Thread(this);
    	//To be used in the future
    	public void pause(){
    		isRunning = false;
    			try {
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    		ourThread = null;
    	//To be used in the future
    	public void resume(){
    		isRunning = true;
    		ourThread = new Thread(this);
    	//Creates a InputStream to set width, then converts to bitmap to be
    	//drawn as background on the canvas.
    	public Bitmap bmScale(InputStream is){
    	   BitmapFactory.Options o = new BitmapFactory.Options();
    	   o.inSampleSize = 2;
    	   Bitmap bit = BitmapFactory.decodeStream(is, null, o);
    	   background = Bitmap.createScaledBitmap(bit, width, 2300, true);
    	   return background;

        //This is the draw method where I need help
    	public void run() {
    		// TODO Auto-generated method stub
    			if(!ourHolder.getSurface().isValid()) continue;
    			//Lock canvas to draw 
    			Canvas canvas = ourHolder.lockCanvas();
    			//Do Things Here:
    			width = getWidth();
    			height = getHeight();
    			//Collision detection for the background
    			if(bgY > 0 ) bgY = 0;
    			//Draws the background
    			canvas.drawBitmap(background, bgX, bgY, null);
    			//For debugging, displays the number of fingers on the screen
    			canvas.drawText(string + fingerCount, 50, 50, mPaint);
    			//Unlock canvas to show 

Is This A Good Question/Topic? 0
  • +

Replies To: How to make android app (animation) run faster/smoother?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon

Reputation: 12691
  • View blog
  • Posts: 45,880
  • Joined: 27-December 08

Re: How to make android app (animation) run faster/smoother?

Posted 27 February 2013 - 11:35 AM

Moved to Android.
Was This Post Helpful? 0
  • +
  • -

#3 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: How to make android app (animation) run faster/smoother?

Posted 27 February 2013 - 08:23 PM

Your run() is running full-speed. You need to slow that down. No wonder things are so slow. You're basically flooding the Thread, not giving it time to actually do anything. Throw in a Thread.sleep(15) in there and see what that does. Learn more about game loops. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1