我有Android應用程序的圖庫查看器。Android onTouch事件僅被稱爲第一次
我需要首先顯示一張大圖片,然後在其下方顯示其他圖片的縮略圖。 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;
}
}
}
非常感謝提前!
是確實這是一個更好的版本我的代碼,重構和可重複使用的,非常感謝! – noloman 2012-04-16 12:54:10