2012-04-11 70 views
2

我有一個畫廊查看器(下面的this教程),我需要在5秒後隱藏縮略圖,並在用戶觸摸屏幕時再次顯示它們。 基本上,當縮略圖隱藏時,只會顯示已選擇的圖像。Android:在倒計時後隱藏畫廊

我的代碼如下:

package com.xs2theworld.sundio; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 

public class GalleryView extends Activity { 
    Integer[] pics = { R.drawable.antartica1, R.drawable.antartica2, R.drawable.antartica3, R.drawable.antartica4, R.drawable.antartica5, R.drawable.antartica6, R.drawable.antartica7, 
      R.drawable.antartica8, R.drawable.antartica9, R.drawable.antartica10 }; 
    ImageView imageView; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gallery_view_layout); 

     Gallery ga = (Gallery) findViewById(R.id.GalleryThumbnails); 
     ga.setAdapter(new ImageAdapter(this)); 

     imageView = (ImageView) findViewById(R.id.GalleryImage); 

     // We show the first image when creating the view gallery 
     imageView.setScaleType(ImageView.ScaleType.CENTER); 
     imageView.setImageResource(pics[0]); 

     ga.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       imageView.setScaleType(ImageView.ScaleType.CENTER); 
       imageView.setImageResource(pics[arg2]); 
      } 
     }); 
    } 

    public class ImageAdapter extends BaseAdapter { 

     private Context ctx; 
     int imageBackground; 

     public ImageAdapter(Context c) { 
      ctx = c; 
      TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1); 
      imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1); 
      ta.recycle(); 
     } 

     public int getCount() { 

      return pics.length; 
     } 

     public Object getItem(int arg0) { 
      return arg0; 
     } 

     public long getItemId(int arg0) { 
      return arg0; 
     } 

     public View getView(int arg0, View arg1, ViewGroup arg2) { 
      ImageView iv = new ImageView(ctx); 
      iv.setImageResource(pics[arg0]); 
      iv.setScaleType(ImageView.ScaleType.FIT_XY); 
      iv.setLayoutParams(new Gallery.LayoutParams(200, 120)); 
      iv.setBackgroundResource(imageBackground); 
      return iv; 
     } 
    } 
} 

我怎麼能這樣做呢?

非常感謝!

回答

1

使用的TimerTask隱藏在預定時間畫廊5秒後...

Timer timer = new Timer(); 

    timer.scheduleAtFixedRate(new TimerTask() 
    { 
    public void run() 
    { 
     //Do your task here 
    } 

    },delay,period); 

或者您可以使用此...

  View v = new View(this); 
      v.postDelayed(new Runnable(){ 
       public void run() { 
        // TODO Auto-generated method stub 
        // Hide your Gallery here 
       } 
      }, (long) 5000.0); 

希望它會幫助你。

+0

完美,非常感謝,它的作品就像一個魅力! – noloman 2012-04-12 08:11:56

+0

ga.setVisibility(View.GONE); – 2012-11-14 21:03:11