最終:
當我們在最下邊的gallery中切換圖片時,上面的大圖片會自動切換,切換時有動畫效果哦,很簡單的一個程式,有待完善更多的功能!
activity代碼:
package cn.com.chenzheng_java;</p><p>import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.ViewGroup;<br />import android.view.Window;<br />import android.view.animation.AnimationUtils;<br />import android.widget.AdapterView;<br />import android.widget.BaseAdapter;<br />import android.widget.Gallery;<br />import android.widget.ImageSwitcher;<br />import android.widget.ImageView;<br />import android.widget.Toast;<br />import android.widget.AdapterView.OnItemSelectedListener;<br />import android.widget.ImageView.ScaleType;<br />import android.widget.ViewSwitcher.ViewFactory;<br />/**<br /> * @description 對圖片的一個小小展示<br /> * @author chenzheng_java<br /> * @since 2011/3/17<br /> *<br /> */<br />public class GalleryActivity extends Activity implements OnItemSelectedListener {</p><p>int[] imagesId = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3,<br />R.drawable.a4</p><p>};<br />int index = R.drawable.a1;<br />ImageSwitcher imageSwitcher;</p><p>@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />/**<br /> * 設定視窗無標題列,一定要在setContentView前進行設定哦<br /> */<br />requestWindowFeature(Window.FEATURE_NO_TITLE);<br />setContentView(R.layout.gallery);</p><p>Gallery gallery = (Gallery) findViewById(R.id.gallery1);<br />gallery.setOnItemSelectedListener(this);</p><p>imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);<br />imageSwitcher.setVisibility(View.VISIBLE);</p><p>/***<br /> * setInAnimation可以設定淡入動畫<br /> * setOutAnimation可以設定淡齣動畫<br /> */<br />imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(<br />getApplicationContext(), android.R.anim.fade_in));<br />imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(<br />getApplicationContext(), android.R.anim.fade_out));</p><p>imageSwitcher.setFactory(new ViewFactory() {<br />/**<br /> * 建立一個新的圖片放置到ImageSwitcher上,可以使用其設定背景哦。<br /> * 一般只會在建立時調用一次。相當於為我們建立一個進行動畫效果時的一個背景圖。<br /> * 此方法亦可以通過setView替代<br /> */<br />@Override<br />public View makeView() {<br />ImageView imageView = new ImageView(GalleryActivity.this);<br />// 設定截模數式<br />imageView.setScaleType(ScaleType.CENTER_INSIDE);<br />imageView.setBackgroundResource(R.drawable.ground);<br />Toast.makeText(getApplicationContext(), "執行了一次",<br />Toast.LENGTH_SHORT).show();<br />return imageView;<br />}<br />});</p><p>gallery.setVisibility(View.VISIBLE);<br />gallery.setAdapter(new ImageAdapter(this));</p><p>}</p><p>class ImageAdapter extends BaseAdapter {<br />private Context context2;</p><p>public ImageAdapter(Context context) {<br />context2 = context;<br />}</p><p>@Override<br />public int getCount() {<br />return imagesId.length;<br />}</p><p>@Override<br />public Object getItem(int position) {<br />return imagesId[position];<br />}</p><p>@Override<br />public long getItemId(int position) {<br />return position;<br />}</p><p>@Override<br />public View getView(int position, View image2, ViewGroup parent) {<br />ImageView image = new ImageView(context2);<br />image.setBackgroundResource(imagesId[position]);<br />/**<br /> * setScaleType()可以設定當圖片大小和容器大小不匹配時的剪輯模式.<br /> * ScaleType.CENTER_INSIDE的意思是,按圖片的原比例縮小或者擴大,直到長或者寬中的任何一個頂到容器為止<br /> */<br />image.setScaleType(ScaleType.CENTER_INSIDE);</p><p>return image;<br />}</p><p>}<br />/**<br /> * 當gallery的映像發生變化時,同步ImageSwitcher的變化<br /> */<br />@Override<br />public void onItemSelected(AdapterView<?> parent, View view, int position,<br />long id) {<br />imageSwitcher.setImageResource(imagesId[position]);</p><p>}</p><p>@Override<br />public void onNothingSelected(AdapterView<?> parent) {</p><p>}</p><p>}<br />
gallery.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout<br /> xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical"<br /> android:gravity="center_horizontal"<br /> ></p><p> <ImageSwitcher<br /> android:id="@+id/imageSwitcher"<br /> android:layout_height="350dp"<br /> android:layout_width="fill_parent"<br /> ></ImageSwitcher></p><p> <Gallery android:id="@+id/gallery1"<br /> android:gravity="center"<br /> android:layout_width="70dp"<br /> android:layout_height="100dp"></Gallery><br /></LinearLayout><br />