setRepeatMode和setRepeatCount无效的问题


AnimationSet set = new AnimationSet(true);
AlphaAnimation alpha = new AlphaAnimation(0, 1); // 由完全显示 --> 完全透明
alpha.setDuration(3000); // 3秒完成动画
alpha.setRepeatCount(500);
alpha.setRepeatMode(Animation.REVERSE);
set.addAnimation(alpha); // 增加动画
set.startNow();
Welcome.this.pressAnyKey.startAnimation(set); // 启动动画


应当由alpha来设定重复次数,而并非set
制作一个最简单的小游戏可以帮助你了解Android Studio的基本操作和Android应用程序的开发流程。下面是一个简单的示例游戏,您可以跟随步骤操作: 1.创建一个新的Android Studio项目。选择“Empty Activity”模板。 2.在项目的res/layout文件夹中创建一个新的XML布局文件,命名为activity_game.xml。在此文件中添加一个ImageView组件作为游戏的主要视图。 ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/game_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".GameActivity"> <ImageView android:id="@+id/game_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> ``` 3.在res/drawable文件夹中创建一个新的XML文件,命名为game_background.xml。在此文件中添加一个形状元素作为游戏的背景。 ```xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#000000" /> </shape> ``` 4.在res/drawable文件夹中创建一个新的PNG图片文件,命名为game_sprite.png。这将是游戏主角的图片。 5.在GameActivity.java文件中,为ImageView组件设置背景和主角图片。还要添加一些简单的动画代码来移动主角。 ```java import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; public class GameActivity extends AppCompatActivity { private ImageView gameView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); gameView = findViewById(R.id.game_view); //设置背景 gameView.setBackgroundResource(R.drawable.game_background); //设置主角 gameView.setImageDrawable(getResources().getDrawable(R.drawable.game_sprite)); //创建动画 Animation animation = new TranslateAnimation(0, 500, 0, 0); animation.setDuration(5000); animation.setRepeatCount(-1); animation.setRepeatMode(Animation.REVERSE); //启动动画 gameView.startAnimation(animation); } } ``` 6.运行程序,您将看到一个在屏幕上移动的小图片。您可以在代码中更改动画的参数来自定义动画效果。 这是一个最简单的游戏示例,但您可以根据自己的需求和兴趣来扩展它。希望这个教程对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值