This is just a simple app. All it's suppose to do is move the dice around the screen when the user swipes up or down. It will move 2 dice (image drawables) to a position on the screen depending whether they swiped up or down.
How can I make my code so it's more object-oriented? At the moment it isn't.
Main.java
public class Main extends ActionBarActivity {
public final int[] die = {R.drawable.die1, R.drawable.die2,R.drawable.die3,R.drawable.die4,
R.drawable.die5, R.drawable.die6};
float x1,x2;
float y1, y2;
public static ImageView dice1;
public static ImageView dice2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dice1 = (ImageView) findViewById(R.id.die1);
dice2 = (ImageView) findViewById(R.id.die2);
}
public boolean onTouchEvent(MotionEvent touchevent)
{
switch (touchevent.getAction())
{
// when user first touches the screen we get x and y coordinate
case MotionEvent.ACTION_DOWN:
{
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP:
{
x2 = touchevent.getX();
y2 = touchevent.getY();
//if left to right sweep event on screen
if (x1 < x2)
{ /*Not implemented yet*/ }
// if right to left sweep event on screen
if (x1 > x2)
{ /*Not implemented yet*/ }
// if UP to Down sweep event on screen
if (y1 < y2)
{
//Swipe down:
Toast.makeText(this, "Swiped Down", Toast.LENGTH_LONG).show();
dice diceCall = new dice(1, dice1, dice2);
diceCall.swipe();
}
//if Down to UP sweep event on screen
if (y1 > y2)
{
//Swipe up:
Toast.makeText(this, "Swiped Up", Toast.LENGTH_LONG).show();
dice diceCall = new dice(2, dice1, dice2);
dice.swipe();
}
break;
}
}
return false;
}
Dice.java
This class holds the animation events. In the constructor, type = 1 is for swiped DOWN and 2 for swiped UP.
public class dice {
private static int type;
private static ImageView die1;
private static ImageView die2;
public dice(int type, ImageView die1, ImageView die2) {
this.type = type;
this.die1 = die1;
this.die2 = die2;
}
public static void swipe() {
if(type == 1){
animateTile();
moveDiceOnSwipeDown();
} else if(type == 2){
moveDiceOnSwipeUp();
}
//Swipe right:
}
public static void animateTile(){
RotateAnimation anim = new RotateAnimation(0f, 310f, 17f, 11f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
die1.startAnimation(anim);
die2.startAnimation(anim);
}
//Move dice from x to yy | Dice down
public static void moveDiceOnSwipeDown(){
//Die1
TranslateAnimation moveDie1 = new TranslateAnimation(0, 100, 0, 200); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
moveDie1.setDuration(2000); // animation duration
//Die2
TranslateAnimation moveDie2 = new TranslateAnimation(0, -100, 0, 400);
moveDie2.setDuration(2000);
moveDie1.setFillAfter(true);
moveDie2.setFillAfter(true);
//Start the animations
die1.startAnimation(moveDie1); // start animation
die2.startAnimation(moveDie2); // start animation
}
//Move dice UP
public static void moveDiceOnSwipeUp(){
//Die1
TranslateAnimation moveDie1 = new TranslateAnimation(0, -100, 0, -200); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
moveDie1.setDuration(2000); // animation duration
//Die2
TranslateAnimation moveDie2 = new TranslateAnimation(0, -180, 0, -400);
moveDie2.setDuration(2000);
moveDie1.setFillAfter(true);
moveDie2.setFillAfter(true);
//Start the animations
die1.startAnimation(moveDie1); // start animation
die2.startAnimation(moveDie2); // start animation
}
}