2012年5月17日 星期四

image switcher

Image Switcher






public class Cq_ImgSwticherActivity extends Activity implements OnItemSelectedListener, ViewFactory {
    /** Called when the activity is first created. */
//設定 Gallery 小圖示的背景圖框大小
private final int LAYOUT_PARAMS_WIDTH = 120;
private final int LAYOUT_PARAMS_HEIGHT = 120;
//設定 大圖示的寬高
private final int LAYOUT_PARAMS_imgWIDTH = 350;//CQ : 500
private final int LAYOUT_PARAMS_imgHEIGHT = 300;//CQ: 450
private ImageSwitcher is;
private Gallery gallery;
private TextView tv;
private TextView tvHeader;
private Context mContext;
//Prepare two int array for GalleryIds & ImageIds
private Integer[] mThumbIds = {R.drawable.kid_01s, R.drawable.kid_02s, R.drawable.kid_03s,
R.drawable.kid_04s, R.drawable.kid_05s, R.drawable.kid_06s, R.drawable.kid_07s};
private Integer [] mImgIds = {R.drawable.kid_01, R.drawable.kid_02, R.drawable.kid_03,
R.drawable.kid_04, R.drawable.kid_05, R.drawable.kid_06, R.drawable.kid_07
};
private String [] mTextStr = {"望遠鏡", "忍者龜", "小學生", "泡泡澡", "周來瘋", "小菩薩", "困獸之鬥"};
private String [] mGalleryText = {"望遠鏡", "忍者龜", "小學生", "泡泡澡", "周來瘋", "小菩薩", "困獸之鬥"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.main);
     
        tv = (TextView)findViewById(R.id.textView1);//text string for each image
        tvHeader = (TextView)findViewById(R.id.textViewHeader);
        
        is = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
        is.setFactory(this);
        //way the two images transit
        is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        
        gallery = (Gallery)findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));  //Create a ImageAdapter
        gallery.setOnItemSelectedListener(this);
        
        
    }

    //Switcher (大圖示)的 View
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView imgView = new ImageView(this);
imgView.setBackgroundColor(0xFF0000AA);
imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//Set Images 的寬高
//imgView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));//
imgView.setLayoutParams(new ImageSwitcher.LayoutParams(LAYOUT_PARAMS_imgWIDTH, LAYOUT_PARAMS_imgHEIGHT));
return imgView;
}

//Custom ImageAdapter 自定的 adapter
public class ImageAdapter extends BaseAdapter{

public ImageAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

//Set Gallery's View (ThumbNail 小圖示 )
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
//i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setLayoutParams(new Gallery.LayoutParams(LAYOUT_PARAMS_WIDTH, LAYOUT_PARAMS_HEIGHT));
i.setBackgroundResource(R.drawable.background05);
return i;
}
}//End of Custom adapter
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
//Display images
is.setImageResource(mImgIds[position]);
//Display image text
tv.setText(mTextStr[position]);
tv.setTextSize(30);
tv.setTextColor(Color.YELLOW);
//Gallery 上 (小圖示)顯示相對的文字
Toast.makeText(Cq_ImgSwticherActivity.this, mGalleryText[position], Toast.LENGTH_LONG).show();
tvHeader.setText(mGalleryText[position]);
tvHeader.setTextColor(Color.MAGENTA);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

***************************************
<?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"
    android:orientation="vertical"
    android:background="@drawable/background3" >
    

    <ImageSwitcher
        android:layout_marginTop="130dp"
        android:id="@+id/imageSwitcher1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        
         >
    </ImageSwitcher>
    <!--  -->
        
        
        <!-- 此 TextView 將置於 imgSwitcher 裡的下方 -->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/imageSwitcher1"
        android:text="TextView"
        android:gravity="center_vertical" />
        
    
    
     <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="100dp"  
        android:background="#55000000"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="15dp"
        
        android:spacing="16dp"
        android:gravity="center_vertical"
        />
    <!-- android:layout_alignParentBottom="true" 如果將此行去除;Gellery 將跑到螢幕的最上方-->
     <TextView
        android:id="@+id/textViewHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="**** Image Switcher ****"
        android:gravity="center"
        android:layout_centerHorizontal="true" />  
   
</RelativeLayout>

沒有留言:

張貼留言