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).

MainActivity.java

    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;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		
    		ourView = new FretBoard(this);
    		ourView.setOnTouchListener(this);
    		setContentView(ourView);
    	
    	}
    
    	//Method to be used in future
    	@Override
    	protected void onpause() {
    		// TODO Auto-generated method stub
    		super.onpause();
    		ourView.pause();
    	}
    
    	//Method to be used in future
    	@Override
    	protected void onresume() {
    		// TODO Auto-generated method stub
    		super.onresume();
    		ourView.resume();
    	}
    	
    	
    	
    	@Override
    	public boolean onTouch(View v, MotionEvent event){
    		
    		//For debugging
    		if(event!= null) ourView.fingerCount = event.getPointerCount();
    		
    		switch(event.getAction()){
    		
    			case MotionEvent.ACTION_DOWN:
    				
    				//Save first y position when finger touches screen
    				y1 = event.getY();
    				
    				break;
    				
    			
    			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 
    					ourView.invalidate();
    					
    					//Reset y1 
    					y1 = y2;
    				}
    				
    				break;
    		}
    		return true;
    	}
    
    }





FretBoard.java


    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) {
    		super(context);
    		// TODO Auto-generated constructor stub
    		ourHolder = getHolder();
    		ourThread = new Thread(this);
    		ourThread.start();
    		
    	}
    
    	//To be used in the future
    	public void pause(){
    		isRunning = false;
    		while(true){
    			try {
    				ourThread.join();
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			break;
    		}
    		ourThread = null;
    	}
    	
    	//To be used in the future
    	public void resume(){
    		isRunning = true;
    		ourThread = new Thread(this);
    		ourThread.start();
    	}
    	
    	
    	//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);
    	   bit.recycle();
    	   return background;
    	}
    

        //This is the draw method where I need help
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		while(isRunning){
    			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
    			bmScale(is);
    			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 
    			ourHolder.unlockCanvasAndPost(canvas);
    		}
    	}	
    }




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