是男人就下100層【第二層】——幫美女更衣(1)

來源:互聯網
上載者:User

去年有一段時間有一個應用下載量很高,都是男同志的功勞,現在又出了第二版,第二層我們就來實現一下《幫美女更衣》這個應用,很多男生應該熟悉這個應用了吧,這裡我就不解釋裡面的內容了,呵呵。

先看看我們要實現的效果:

             (慘不忍睹!做好後大家去挖剩下的吧)

先看看布局檔案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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.