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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With