android之ImageSwitcher 圖片查看

openkk 12年前發布 | 29K 次閱讀 Android開發 移動開發 Android

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">
    </ImageSwitcher>
    <Gallery android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_height="60dp"
        android:spacing="15dp"
        android:layout_alignParentBottom="true"
        android:gravity="center_vertical"
        android:background="#aaaaaa">
    </Gallery>
</RelativeLayout>
主程序如下:
package com.cloay.imageswitcher;

import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Gallery.LayoutParams; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory;

/**

  • ImageSwitcherActivity.java
  • @author cloay
  • 2011-7-16 */ public class ImageSwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory{ private ImageSwitcher imageSwitcher; private Gallery gallery; private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,

         R.drawable.f, R.drawable.g};
    

    private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,

         R.drawable.f, R.drawable.g};
    

    @Override public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    
     imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
     imageSwitcher.setFactory(this);
     //設置圖片切換時的動畫效果
     imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
     imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    
     gallery = (Gallery)findViewById(R.id.gallery);
     //自定義ImageAdapter繼承于BaseAdapter,是一個內部類
     gallery.setAdapter(new ImageAdapter(this));
     gallery.setOnItemSelectedListener(this);
    

    }

    @Override public View makeView() {

     ImageView image = new ImageView(this);
     image.setScaleType(ImageView.ScaleType.FIT_CENTER);
     image.setLayoutParams(new ImageSwitcher.LayoutParams(
             LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
     return image;
    

    }

    @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

         long arg3) {
     imageSwitcher.setImageResource(imagesId[arg2]);
    

    }

    @Override public void onNothingSelected(AdapterView<?> arg0) {

    } public class ImageAdapter extends BaseAdapter{

     private Context context;
     int galleryItemBackground;
     public ImageAdapter (Context c){
         context = c;
         TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery1);
         galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
         typeArray.recycle();
     }
     @Override
     public int getCount() {
         //返回selectId[]的長度
         return selectId.length;
     }
    
     @Override
     public Object getItem(int position) {
         return position;
     }
    
     @Override
     public long getItemId(int position) {
         return position;
     }
    
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         ImageView imageView = new ImageView(context);
         //設置資源圖片
         imageView.setImageResource(selectId[position]);
         imageView.setAdjustViewBounds(true);  //允許調整邊框
         //設定底部畫廊,自適應大小
         imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
         //設置畫廊背景
         imageView.setBackgroundResource(galleryItemBackground);
         return imageView;
     }
    
    

    } }</pre>轉自:http://blog.csdn.net/shang_515/article/details/6782844

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!