I am creating a boardgame for Android and I have an animation that I wish to do when a specific motion event occurs.
I have a Boardview class which draws on a surfaceview with bitmaps and this shows the user the board.
When a motion event happens, this code executes
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() 开发者_开发百科== MotionEvent.ACTION_DOWN) {
Intent animate = new Intent(Tempboardview.this, Moving.class);
animate.putExtra("xOld", oldStones.get(i).getx());
animate.putExtra("yOld", oldStones.get(i).gety());
animate.putExtra("xNew", newStones.get(i).getx());
animate.putExtra("yNew", newStones.get(i).gety());
animate.putExtra("color", 0);
startActivity(animate);
}
}
where newStones and oldStones are simply ArrayList objects and gety() and getx() return doubles.
This is my Moving class
public class Moving extends Activity {
private static final String TAG = "Animation";
double xOld = 0, yOld = 0, xNew = 0, yNew = 0, color = 0;
/** Called when the activity is first created. */@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LayoutInflater inflater = getLayoutInflater();
addContentView(inflater.inflate(R.layout.move, null),
new ViewGroup.LayoutParams(800, 480));
final ImageView image = (ImageView) findViewById(R.id.stoneimager);
image.setVisibility(View.GONE);
image.bringToFront();
Bundle extras = getIntent().getExtras();
if (extras != null) {
xOld = extras.getDouble("xOld");
yOld = extras.getDouble("yOld");
xNew = extras.getDouble("xNew");
yNew = extras.getDouble("yNew");
color = extras.getDouble("color");
}
Animation animate = new TranslateAnimation((float) xOld, (float) xNew, (float) yOld, (float) yNew);
animate.setDuration(1000);
animate.setStartOffset(0);
animate.setInterpolator(new LinearInterpolator());
animate.setRepeatCount(0);
animate.setFillAfter(true);
animate.setZAdjustment(Animation.ZORDER_TOP);
Animation.AnimationListener listener = new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.d("boardtag", "animation started");
image.setVisibility(View.VISIBLE);
image.bringToFront();
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.setVisibility(View.GONE);
finish();
}
};
animate.setAnimationListener(listener);
Log.d("boardtag", "animation started");
image.startAnimation(animate);
}
}
The problem is the animation is not able to take place while the board is still displayed. I have used the android manifest to change the theme of .Moving to Transparent, but this did not work.
Any advice would be appreciated.
This is probably going to come off more harsh than I intend, but I think you are completely abusing the Intent/Activity features. You need to seriously consider redesigning your UI layer to be more closely coupled with the active Activity.
What you have above should be painfully slow since motion events are common and Activity creation and Intent dispatching is expensive (relatively). What you need is a single main "game" Activity that is tightly coupled with a SurfaceView (probably OpenGL since this has animations and it's a game). This activity would take user input (UI events) and translate them into drawing commands for your surface view directly.
精彩评论