我为Android写了一个数独游戏,并希望在用户插入错误数字时对方块进行动画处理。但我不知道该怎么做?问题在于,我有一个覆盖整个屏幕的大矩形,并通过绘制线条将此矩形分成方块。现在我不知道如何对这些方块进行动画处理。
希望我能解释清楚我的问题。
抱歉我的英文不好,谢谢回答!
希望我能解释清楚我的问题。
抱歉我的英文不好,谢谢回答!
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);
}
}
主要活动
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 );
}
}
动画定义(此文件应命名为scale.xml
,并放置在目录/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>
想了解有关动画的更多信息,请点击这里。
要制作自己的动画,请点击这里。
希望这能帮到您。
在Honeycomb之前,与任何动画相关的工作都非常繁琐。我建议下载NineOldAndroids库(http://nineoldandroids.com/)并将其导入,这样您就可以在所有版本的Android上使用Honeycomb动画API(甚至是1.0版本)。