[Java for Android App Development] hiphop12ism:
Hey fellas, i have just started learning android developing. But i am having some problem in understanding the concept of animation.

While learning some android game development, i stumbled upon this sweet piece of code! I am able to understand 3/4th of it. But still i want to learn it fully. So ill just paste the code. And the highlight some of my problems. Ill be very glad if you people help me. :D :cowboy:

It is a kind of game, which basically throws a greenball, with the users swype. Ill be pasting the class here..

package com.example.done;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class GFXsurface extends Activity implements OnTouchListener{

MyBringBackSurface ourSurfaceView;
float x, y, sX, sY, fX, fY, dX, dY, aniX, aniY, scaledX, scaledY;//ani for animaton
Bitmap test, plus;

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ourSurfaceView = new MyBringBackSurface(this);
ourSurfaceView.setOnTouchListener(this);
x = 0;
y = 0;
sX = 0;
sY = 0;
fX = 0;
fY = 0;
dX = dY = aniX = aniY = scaledX = scaledY = 0; //they are equal to 0
test = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
plus = BitmapFactory.decodeResource(getResources(), R.drawable.plus);
setContentView(ourSurfaceView);
}

[MENTION=439709]override[/MENTION]
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSurfaceView.pause();
}

[MENTION=439709]override[/MENTION]
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ourSurfaceView.resume();
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

x = event.getX();
y = event.getY();

switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
sX = event.getX();
sY = event.getY();
dX = dY = aniX = aniY = scaledX = scaledY = fX = fY = 0;
break;
case MotionEvent.ACTION_UP:
fX = event.getX();
fY = event.getY();
dX = fX-sX; //dx is the change in direction final x- starting x
dY = fY-sY;
scaledX = dX/30;
scaledY = dY/30;
x = y =0;
break;
}
return true;
}
public class MyBringBackSurface extends SurfaceView implements Runnable {

SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;

public MyBringBackSurface(Context context) {
super(context);
ourHolder = getHolder();
}

public void pause(){
isRunning = false;
while(true){
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}

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

public void run() {
// TODO Auto-generated method stub
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;

Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);

if (x != 0 && y != 0){
canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.getHeight()/2), null);
}
if (sX != 0 && sY != 0){
canvas.drawBitmap(plus, sX-(plus.getWidth()/2), sY-(plus.getHeight()/2), null);
}
if (fX != 0 && fY != 0){
canvas.drawBitmap(test, fX-(test.getWidth()/2)-aniX, fY-(test.getHeight()/2)-aniY, null);
canvas.drawBitmap(plus, fX-(plus.getWidth()/2), fY-(plus.getHeight()/2), null);
}
aniX = aniX + scaledX;
aniY = aniY + scaledY;

ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}

This are some of the codes, whose purpose are unknown to me:

scaledX = dX/30;
scaledY = dY/30; //why is this being divided by 30?

_________________________________

aniX = aniX + scaledX;
aniY = aniY + scaledY;

___________________________

I can you please give me an overview of the whole code.

How the whole stuff is working in order.

Thanks in advance!