今天做了一下Gallery與ImageSwitcher的綜合運用,下面是運行的效果,可以點擊或者手動拖動圖片:
下面附上源碼:
- package com.Android.cf;
-
- import android.app.Activity;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.Gallery;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.ViewSwitcher.ViewFactory;
-
- public class MyMain extends Activity implements ViewFactory,
- OnItemSelectedListener {
- private ImageSwitcher mSwitcher;
- private Gallery mGallery;
- private int selectedTag = 0;
- private int upX, downX;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main);
- setTitle("圖片顯示");
-
- mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
- mSwitcher.setFactory(MyMain.this);
- mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_in));
- mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_out));
- mSwitcher.setOnTouchListener(touchlistener);
- mGallery = (Gallery) findViewById(R.id.gallery);
- mGallery.setAdapter(new ImageAdapter(MyMain.this));
- mGallery.setOnItemSelectedListener(this);
- }
-
- /**
- * (non-Javadoc)
- *
- * @see android.widget.ViewSwitcher.ViewFactory#makeView()
- */
-
- @Override
- public View makeView() {
- ImageView i = new ImageView(this);
- i.setBackgroundColor(0xFF000000);
- i.setScaleType(ImageView.ScaleType.FIT_CENTER);
- i.setLayoutParams(new ImageSwitcher.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
- return i;
- }
-
- OnTouchListener touchlistener = new OnTouchListener() {
-
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- downX = (int) event.getX(); // 取得按下時 的坐標x
- return true;
- } else if (event.getAction() == MotionEvent.ACTION_UP) {
- upX = (int) event.getX(); // 取得松開時的坐標x
- if (upX - downX > 100) { // 從左拖到右,即看前一張
- // 如果是第一,則去到尾部
- if (mGallery.getSelectedItemPosition() == 0)
- selectedTag = mGallery.getCount() - 1;
- else
- selectedTag = mGallery.getSelectedItemPosition() - 1;
- } else if (downX - upX > 100)// 從右拖到左,即看後一張
- {
- // 如果是最後,則去到第一
- if (mGallery.getSelectedItemPosition() == (mGallery
- .getCount() - 1))
- selectedTag = 0;
- else
- selectedTag = mGallery.getSelectedItemPosition() + 1;
- }
- // 改變gallery圖片所選,自動觸發ImageSwitcher的setOnItemSelectedListener
- mGallery.setSelection(selectedTag, true);
- return true;
-
- }
- return false;
- }
-
- };
-
- /**
- * (non-Javadoc)
- *
- * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView,
- * android.view.View, int, long)
- */
-
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- mSwitcher.setImageResource(ImageAdapter.imageIDs[arg2]);
- selectedTag = arg2;
- }
-
- /**
- * (non-Javadoc)
- *
- * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.AdapterView)
- */
-
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
-
- }
- }