Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android animation

I wrote a suduko game for android, and want to animate tile, if the user insert wrong number. But I don't figure how to do this? The problem is that, I have one big rectangel wich cover hole screen, and devide this rectangle in tiles, simply by drawnig lines. And now I can't figure how to animate tiles.

Hop I could explain my problem.

Sorry for bad english, and thaks for answers)

like image 548
Vahag Vardanyan Avatar asked Dec 17 '22 11:12

Vahag Vardanyan


2 Answers

You should not draw your board like that. I would recommend to implement one tile as a subclass of View so you can then animate each one individually.

Try this to see how animation works:

Tile class

public class Tile extends View {

private RectF mRect;
private Paint mPaint;

public Tile(Context context) {
        super(context);
        init();
}

public Tile(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
}

public Tile(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
}

private void init(){
        mRect  = new RectF(0, 0, 100, 100);
        mPaint = new Paint();
        mPaint.setStyle( Paint.Style.STROKE );
        mPaint.setColor( Color.BLUE );
}

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(mRect, mPaint);
    }
}

Main activity

public class MainActivity extends Activity implements OnClickListener {

private Tile mTile;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout( this );
        layout.setBackgroundColor( Color.WHITE );
        layout.setPadding(50, 50, 50, 50);

        Button btn = new Button( this );
        btn.setText( "Click Me" );
        btn.setOnClickListener( this );
        layout.addView( btn );

        mTile = new Tile( this );
        layout.addView( mTile );

        setContentView( layout );
    }

@Override
public void onClick(View v) {
    Animation scaleAnim = AnimationUtils.loadAnimation(this, R.anim.scale);
    mTile.startAnimation( scaleAnim );      
}
}

Animation definition (This file should be named scale.xml and placed under directory /res/anim )

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="2.0"
        android:fromYScale="1.0"
        android:toYScale="2.0"
        android:fillAfter="false"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:duration="700" />
</set>

Learn more about animations here. To make your own animation have a look here here.

Hope this keeps you going.

like image 178
Xavi Gil Avatar answered Jan 03 '23 14:01

Xavi Gil


If you use standard views, you could look into tween animation, i.e. define a set of animations, load them from the resources and attach them to your views / start the animation.

Have a look at the spaceship jump example here.

If you do custom drawing, I'm afraid you also need to do custom animation.

like image 36
Thomas Keller Avatar answered Jan 03 '23 16:01

Thomas Keller