最近在做一个关于应用程序叫BATTLE FARTS,呵呵,和放屁有关的项目,大体就是根据录进去的声音的大小和时长,通过一个算法,算出分数。里面要实现一个开机动画自动跳转。因为给了三张图 片,要实现闪烁的功能,所以我就想到了FRAME ANIMATION。之前没用过,但是API文档里说了,start()方法不能写在ONCREATE()方法里,因为在ONCREATE()里还没有和 IMAGEVIEW绑定,所以只能写在拖拽等事件中。无奈,只能写在触发事件里写。但是,原来的程序是要实现开机自动运行动画的。技术高手可以指点一下 我。然后动画运行完,画面要跳转,想了很多方法,没有找到在哪里写。后来想到了,可以用一个定时器,于是查了一些资料,说是用TIMER和 HANDLER.写惯了JAVA的人都想直接用TIMER,但是其实在ANDROID里是不行的,这和ANDROID的安全性有关。只能用TIMER与 HANDLER实现,我要实现的是,五秒后自动,跳到下一个ACTIVITY.代码如下:
- package com.battlefart;
- import android.app.Activity;
-
- import java.util.Timer;
- import java.util.TimerTask;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.drawable.AnimationDrawable;
- import android.view.KeyEvent;
- import android.os.Bundle;
- import android.widget.ImageView;
- import android.os.Handler;
- import android.os.Message;
- public class Bf_mech_lpener_1 extends Activity {
- private ImageView OpenFlash;
- private String strPlayerName;
- private AnimationDrawable Open_animation;
-
-
- Timer timer = new Timer();
- Handler handler = new Handler(){
-
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- Intent intent=new Intent();
- intent.setClass(Bf_mech_lpener_1.this, bf_mech_1_1opener.class);
- startActivity(intent);
- Bf_mech_lpener_1.this.finish();
- break;
- }
- super.handleMessage(msg);
- }
-
- };
- TimerTask task = new TimerTask(){
-
- public void run() {
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- }
-
- };
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- SharedPreferences sp = getSharedPreferences("player_information",0);
- strPlayerName=sp.getString("Player_name", "");
- if(strPlayerName.equals(""))
- {
- setContentView(R.layout.bf_mech_opener_one);
- OpenFlash = (ImageView) findViewById(R.id.bf_mech_open_light1);
- OpenFlash.setBackgroundResource(R.anim.open_flash);
- Open_animation = (AnimationDrawable) OpenFlash.getBackground();
-
-
- }
- else {
- Intent intent=new Intent(Bf_mech_lpener_1.this,Home_2.class);
- startActivity(intent);
- Bf_mech_lpener_1.this.finish();
- }
-
- }
-
- public boolean onKeyUp(int KeyCode,KeyEvent event)
- {
- switch(KeyCode)
- {
- case KeyEvent.KEYCODE_DPAD_UP:
- Open_animation.start();
- break;
- }
- return true;
-
- }
- }
(tquding) |