2012-08-01 51 views
-1

我做了網格視圖,並都在繪製現在我想的是這些圖像來隨機圖像9圖像視圖的我已經在網格視圖,如以1秒之儘快感動用戶顯示一個圖像它會消失,它在不同的圖像視圖中顯示另一個圖像,並繼續。 請幫助,如果任何機構有想法。之後dis如何使圖像隨機出現在圖像視圖中。如何在android中使用timer來在gridview的不同圖像視圖中隨機顯示圖像?

< 
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

你嘗試過這麼遠嗎?你遇到什麼問題?請在你的問題上留出更多時間。 – Benoir 2012-08-01 18:00:05

+0

喜請檢查一下我已經修改了疑問句我希望這是更清楚你 – 2012-08-01 18:03:23

+0

我覺得Benoir問你做了什麼。我們不能爲你做這個工作,哈哈。 – Andy 2012-08-01 18:15:15

回答

1

您將不得不使用類似CountDownTimer(或者如果您喜歡的處理程序)。我已經包含了一個如何使用CountDownTimer的例子。你將不得不使用這個倒數計時器來關閉顯示什麼(而不是顯示)以及什麼時候發生的事件。就在我頭頂的時候,一種方法是將所有的ImageViews(你應該從網格的適配器中獲得)放入一個數組中,並且每隔5秒(使用定時器)迭代該數組,並確定哪些ImageViews應該被設置爲隱形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); 
} 
} 
0

那麼,在你的情況你可以實現GridView與適配器,BaseAdapter是個不錯的選擇。你把圖像放在網格上的9個隨機位置。您可以使用Java中的隨機方法來獲得一些隨機行爲。您還在網格上設置了一個onClickListener,所以當用戶點擊它時,您會選擇另一個隨機數,它將對應網格上的另一個點。巴姆。我想,我把這個答案多的工作,你做的問題:)

哦,我只是意識到你想有一個計時器。那麼在這種情況下,請檢查定時器類的Java文檔。我很確定有幾個選項,不論是本機還是非本機。拿你的選擇。但是我的實現依然如此。每次計時器關閉時,只需使用Math.random在網格上隨機放置圖像即可刷新網格:)

+0

在網格視圖中實現圖像的隨機化..我已經發布了我現在使用的代碼。你可以做一些改變它隨機圖像 – 2012-08-01 18:23:45