显示5秒的ImageView数组

时间:2012-07-05 03:56:42

标签: android android-imageview

我有一个带有ImageView数组的TableLayout然后当我点击menuInflater时它会出现五秒钟然后隐藏并倒计时开始。

public boolean onOptionsItemSelected(MenuItem item) {

super.onOptionsItemSelected(item);

switch (item.getItemId()){

case R.id.txt1:
    handler = new Handler (this);
                Worker w = new Worker (handler);
               Thread t = new Thread (w);
               t.start();
     return true;

  protected int [] imgIds = {
    R.id.img_1,R.id.img_2,R.id.img_3,R.id.img_4,R.id.img_5,R.id.img_6,
    R.id.img_7,R.id.img_8,R.id.img_9,R.id.img_10,R.id.img_11,R.id.img_12,
   };

public class Worker implements Runnable{

private Handler h;

public Worker (Handler h)
{
this.h = h;
}

@Override
public void run() {

for(int i=0; i<10; i++)
{
    imgIds.setsetVisibility(View.INVISIBLE)

    try{
        Thread.sleep(1000);
    }catch (InterruptedException e){
        e.printStackTrace();
    }
}
h.postDelayed(Worker,1000);
 }   
 }

然后ImageView隐藏并启动游戏。

此代码有误,所以如果有人可以帮助我。

2 个答案:

答案 0 :(得分:6)

Frame Animations会帮助您

以下代码段将指导您。

package com.org.sampleproject;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AnimationDrawable animation = new AnimationDrawable();
        animation.addFrame(getResources().getDrawable(R.drawable.img1), 1000); //1 Sec
        animation.addFrame(getResources().getDrawable(R.drawable.img2), 1000); //1 Sec 
        animation.addFrame(getResources().getDrawable(R.drawable.img3), 1000); //1 Sec 
        animation.addFrame(getResources().getDrawable(R.drawable.img4), 1000); //1 Sec
        animation.addFrame(getResources().getDrawable(R.drawable.img5), 1000); //1 Sec
        animation.setOneShot(true);

        final ImageView imageAnim = (ImageView) findViewById(R.id.image);
        imageAnim.setBackgroundDrawable(animation);

        animation.start();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                imageAnim.setVisibility(View.INVISIBLE);

                //Perform Your Task Here 

            }
        }, animation.getNumberOfFrames() * 1000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

答案 1 :(得分:0)

也许你可以使用CountDownTimer:

http://developer.android.com/reference/android/os/CountDownTimer.html

protected int [] imgIds = {
R.drawable.img_1,R.drawable.img_2,...
};


new CountDownTimer(12000, 1000) {
    public void onTick(long millisUntilFinished) {
       img.setImageResource(R.drawable.my_image);
    }