如何在android中使用计时器在gridview的不同图像视图中随机显示图像?

时间:2012-08-01 17:58:30

标签: android android-layout android-intent android-widget

我已经制作了网格视图并且现在所有的图像都在drawable中我想要的是这些图像随机出现在我在网格视图中的9个图像视图中,就像在1秒钟时它显示一个图像一旦触及用户它消失,它在不同的图像视图中显示另一个图像,然后继续。 请帮助,如果有任何机构有这个想法。之后如何使图像随机出现在图像视图中。

<
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}
>

2 个答案:

答案 0 :(得分:1)

你将不得不使用像CountDownTimer(如果你愿意,还是Handler)。我已经包含了一个如何使用CountDownTimer的示例。你必须使用这个倒数计时器来发射关于显示(和不显示)的内容以及何时发生的事件。就在我的脑海中,一种方法是将你的所有ImageViews(你应该从网格的适配器中获取)放入一个数组中,并每隔5秒迭代一次该数组(使用你的计时器)并确定哪些ImageViews应设置为Invisible setVisibility(View.Invisible)

**Activity**

// CountDownTimer Example

public class ExampleActivity extends Activity implements OnClickListener {

// Constants
private static final long DURATION = 5 * 1000; // 5 seconds
private static final long INTERVAL = 500; // 500 milliseconds
private static final String FINISHED = "finished";
private static final String CANCEL_MESSAGE = "Timer Cancelled";
private static final long START_TICK_VALUE = 0;
private static final String START = "Start";
private static final String STOP = "Stop";

// Timer
private MyTimer timer;
private long tick;

// UI
private TextView tv;
private Button startButton;
private Button stopButton;

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

    tv = (TextView) findViewById(R.id.your_text_view);
    tv.setVisibility(View.VISIBLE);

    // start button
    startButton = (Button) findViewById(R.id.button1);
    startButton.setVisibility(View.VISIBLE);
    startButton.setOnClickListener(this);   
    startButton.setText(START);
    // stop button
    stopButton = (Button) findViewById(R.id.button2);
    stopButton.setVisibility(View.VISIBLE);
    stopButton.setOnClickListener(this);
    stopButton.setText(STOP);
    stopButton.setEnabled(false);

    timer = new MyTimer(DURATION, INTERVAL);
}

// CountDownTimer class
private class MyTimer extends CountDownTimer {

    public MyTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        tv.setText(FINISHED);
        setStartLayout();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        tick += INTERVAL;
        String tickText = String.valueOf(tick);
        tv.setText(tickText);
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    // start timer
    case R.id.button1:
        resetTick();
        setStopLayout();
        timer.start();
        break;
    // stop timer
    case R.id.button2:
        timer.cancel();
        setStartLayout();
        tv.setText(CANCEL_MESSAGE);
        break;
    }
}

private void resetTick() {
    tick = START_TICK_VALUE;
}

private void setStopLayout() {
    startButton.setEnabled(false);
    stopButton.setEnabled(true);
}

private void setStartLayout() {
    startButton.setEnabled(true);
    stopButton.setEnabled(false);
}
}

答案 1 :(得分:0)

嗯,在您的情况下,您可以使用适配器实现GridViewBaseAdapter不是一个糟糕的选择。您将图像放在网格上的9个随机位置。您可以在Java中使用Random方法以获得一些随机行为。您还可以在网格上设置onClickListener,因此当用户单击它时,您会选择另一个随机数,该数字将对应于网格上的另一个点。巴姆。我想我在这个问题上做了尽可能多的工作:)

哦,我刚才意识到你想要一个计时器。那么在这种情况下,请查看Timer类的Java文档。我很确定有一些选项,无论是原生的还是非原生的。随便挑选。但我的实施仍然是一样的。每次定时器关闭时,您只需使用Math.random在网格上随机放置图像就刷新网格:)