我有一个游戏,我需要让苹果掉下来,但我有一个问题。 (动画相同)当我点击按钮1苹果掉落,但是当我再次点击按钮时,1个苹果擦除,然后再次掉落。当我点击按钮时我怎么能做到这一点我已经掉了aple,当再次点击时,第一个苹果不能擦除。现在我有了这段代码
public void limonplus(View v){
final ImageView floatingImage = (ImageView)findViewById(R.id.imageView1);
Random random = new Random();
TranslateAnimation anim = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, random.nextFloat(),
Animation.RELATIVE_TO_PARENT, random.nextFloat(),
Animation.RELATIVE_TO_PARENT, 0f,
Animation.RELATIVE_TO_PARENT, 1f);
anim.setDuration(1800);
floatingImage.startAnimation(anim);
}
答案 0 :(得分:0)
工作原理: 首先,创建ImageView的新实例。然后开始动画并完成动画 - 从父版面中删除图像。 以下是Activity的代码:
public class MyActivity extends Activity implements View.OnClickListener {
Button button;
FrameLayout fl;
ImageView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button_drop);
button.setOnClickListener(this);
}
public void limonplus(){
view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.image_resource);
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
fl = (FrameLayout)findViewById(R.id.frameLayout1);
fl.addView(view);
Random random = new Random();
TranslateAnimation anim = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, random.nextFloat(),
Animation.RELATIVE_TO_PARENT, random.nextFloat(),
Animation.RELATIVE_TO_PARENT, 0f,
Animation.RELATIVE_TO_PARENT, 1f);
anim.setDuration(1800);
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation paramAnimation) { }
public void onAnimationRepeat(Animation paramAnimation) { }
public void onAnimationEnd(Animation paramAnimation) {
fl.post(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
fl.removeAllViews();
}
});
}
});
}
});
view.startAnimation(anim);
}
@Override
public void onClick(View view) {
Log.d("myLog","Dropped");
limonplus();
}
}
以下是main.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Drop"
android:id="@+id/button_drop" android:layout_gravity="left|top" android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp" android:layout_alignParentTop="true" android:layout_marginTop="0dp"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/frameLayout1">
</FrameLayout>
</RelativeLayout>
希望,它解决了你的问题:)