去年有一段時間有一個應用下載量很高,都是男同志的功勞,現在又出了第二版,第二層我們就來實現一下《幫美女更衣》這個應用,很多男生應該熟悉這個應用了吧,這裡我就不解釋裡面的內容了,呵呵。
先看看我們要實現的效果:
(慘不忍睹!做好後大家去挖剩下的吧)
先看看布局檔案activity_main.xml
<?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/image_switcher" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="80.0dip" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:background="#55000000" android:gravity="center_vertical" android:spacing="15.0dip" /></RelativeLayout>
ImageSwitcher繼承自ViewSwitcher,重寫了showNext()、showPrevious()方法。
使用ImageSwithcer的步驟如下:
1、為ImageSwitcher提供一個ViewFactory,該ViewFactory會有一個makeView()方法來產生View組件,這個組件必須是ImageView.
2、切換圖片時只需要調用setImageDrawable(Drawable drawable)方法、setImageResource(int resid)方法或setImageURI(Uri uri).
package com.example.mygrilproject;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.Gallery.LayoutParams;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity implements ViewFactory{private ImageSwitcher is;private Gallery gallery;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//擷取ImageSwitcher對象is = (ImageSwitcher)findViewById(R.id.image_switcher);is.setFactory(this);//擷取gallery對象gallery = (Gallery)findViewById(R.id.gallery);//設定適配器gallery.setAdapter(new ImageAdapter(MainActivity.this));//設定監聽gallery.setOnItemSelectedListener(new OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {int drawableId = 0; try { drawableId = R.drawable.class.getDeclaredField( "pre" + position).getInt(this); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } //更換ImageSwitcher中的圖片 is.setImageResource(drawableId);}@Overridepublic void onNothingSelected(AdapterView<?> parent) {} });is.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){}});}//設定圖片切換的動畫效果@Overridepublic View makeView() {ImageView i = new ImageView(this);i.setBackgroundColor(0xFF000000);i.setScaleType(ImageView.ScaleType.FIT_CENTER);i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));return i;}private class ImageAdapter extends BaseAdapter {private Context mContext;public ImageAdapter(Context c){mContext = c;}@Overridepublic int getCount() {return 11;//11張圖片}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView(mContext);int drawableId = 0;try {//使用反射擷取資來源物件drawableId = R.drawable.class.getDeclaredField("pre" + position).getInt(this);} catch (IllegalArgumentException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (NoSuchFieldException e) {e.printStackTrace();}imageView.setLayoutParams(new Gallery.LayoutParams(120,120));imageView.setScaleType(ScaleType.FIT_XY);imageView.setImageResource(drawableId);return imageView;}}}運行效果:
下一篇我們再來看看如何撕衣服,呵呵
原始碼下載:http://download.csdn.net/detail/lxq_xsyu/7014543