我有一个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;
}
}
}
提前多多感谢!
答案 0 :(得分: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;
}
});
...
答案 1 :(得分: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]);
}
});
那是所有人:)