0

我有一个用于 Android 应用程序的图库查看器。

我需要先展示一张大图,然后在它下面展示其他图片的缩略图。5 秒后,缩略图消失,当用户触摸屏幕时,缩略图应该会再次出现。

这里的问题是,如果缩略图再次出现,它们将不再消失,因此这只适用于第一次。

我该如何解决这个问题?

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/GalleryImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" >
    </ImageView>

    <Gallery
        android:id="@+id/GalleryThumbnails"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/GalleryImage" />
</RelativeLayout>

这是我的实现:

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);

        final 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]);

        // Hiding the thumbnails after 5 seconds

        final View v = new View(this);
        v.postDelayed(new Runnable() {
            public void run() {
                ga.setVisibility(View.GONE);
            }
        }, (long) 5000);

        // Showing the thumbnails again when touching the screen
        // TODO: Not working yet

        imageView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    ga.setVisibility(View.VISIBLE);
                }
                return false;
            }
        });

        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;
        }

        // Thumbnails gallery

        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;
        }
    }
}

提前非常感谢!

4

2 回答 2

1

这是奇怪的代码

   final View v = new View(this);
    v.postDelayed(new Runnable() {
        public void run() {
            ga.setVisibility(View.GONE);
        }
    }, (long) 5000);

您正在创建一个悬空视图以安排对 ga 的延迟调用。

我猜你的意思是

private Gallery ga; // make ga to a member

private void scheduleHide ()
{
    ga.postDelayed(new Runnable() {
        public void run() {
            ga.setVisibility(View.GONE);
        }
    }, (long) 5000);
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallery_view_layout);

    ga = (Gallery) findViewById(R.id.GalleryThumbnails);
...
  scheduleHide ();
...
    imageView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ga.setVisibility(View.VISIBLE);
                scheduleHide ();
            }
            return false;
        }
    });
 ...
于 2012-04-16T12:48:37.630 回答
0

让我自己:只需在以下内容中包含对计时器的另一个调用setOnItemClickListener

ga.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                final View v = new View(getApplicationContext());
                v.postDelayed(new Runnable() {
                    public void run() {
                        ga.setVisibility(View.GONE);
                    }
                }, (long) 5000);
                imageView.setScaleType(ImageView.ScaleType.CENTER);
                imageView.setImageResource(pics[arg2]);
            }
        });

这就是所有人:)

于 2012-04-16T12:43:16.237 回答