---
title: "Android基础教程之-----访Iphone 拖动相片特效Gallery的简单应用."
author: "mmdev"
date: "2009-12-29T22:09:00Z"
tags: []
slug: "android-iphone-gallery"
draft: false
lastmod: "2009-12-29T22:09:00Z"
id: 990388
url: "https://mmdev.iteye.com/blog/990388"
---

**Step 1**:准备图片素材.

将**icon2**,**icon3**,**icon4**,**icon5**,**icon6**五张图片导入**res/drawable**里加上**icon.png**本身一共有6张图片.

**Step 2**:新建**Android**工程,命名为**GalleryDemo**.

**Step 3:**设计**UI**,修改**main.xml**代码如下:

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout  
xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
android:background="@drawable/white"  
android:orientation="vertical"  
android:layout\_width="fill\_parent"  
android:layout\_height="fill\_parent"  
&gt;  
&lt;TextView  
android:id="@+id/myTextView01"  
android:layout\_width="fill\_parent"  
android:layout\_height="wrap\_content"  
android:text="@string/hello"  
android:gravity="center\_vertical|center\_horizontal"  
/&gt;  
**&lt;Gallery**  
**android:id="@+id/myGallery1"**  
**android:layout\_width="fill\_parent"**  
**android:layout\_height="wrap\_content"**  
**android:gravity="bottom"**  
**/&gt;**  
&lt;/LinearLayout&gt;

**Step 4**:设计主程序类**GalleryDemo.java**代码如下:

package com.android.test;

**import com.android.test.R.drawable;**  
import android.app.Activity;  
**import android.content.Context;**  
import android.os.Bundle;  
**import android.view.View;**  
**import android.view.ViewGroup;**  
**import android.widget.BaseAdapter;**  
**import android.widget.Gallery;**  
**import android.widget.ImageView;**

public class **GalleryDemo** extends Activity {  
/\** Called when the activity is first created. \*/  
@Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.main);

**((Gallery) findViewById(R.id.myGallery1)).setAdapter(new ImageAdapter(**  
**this));**  
}

public class **ImageAdapter** extends **BaseAdapter** {  
/* 类成员 myContext为Context父类 \*/  
private Context myContext;

/* 使用res/drawable图片作为图片来源 \*/  
**private int\[] myImageIds = { drawable.icon, drawable.icon2,**  
**drawable.icon3, drawable.icon4, drawable.icon5, drawable.icon6};**

/* 构造器只有一个参数，即要存储的Context \*/  
**public ImageAdapter(Context c) {**  
**this.myContext = c;**  
**}**

/* 返回所有已定义的图片总数量 \*/  
**public int getCount() {**  
**return this.myImageIds.length;**  
**}**

/* 利用getItem方法，取得目前容器中图像的数组ID \*/  
**public Object getItem(int position) {**  
**return position;**  
**}**

**public long getItemId(int position) {**  
**return position;**  
**}**

/* 取得目前欲显示的图像View，传入数组ID值使之读取与成像 \*/  
**public View getView(int position, View convertView, ViewGroup parent) {**  
**/* 创建一个ImageView对象 \*/**  
**ImageView i = new ImageView(this.myContext);**

**i.setImageResource(this.myImageIds\[position]);**  
**i.setScaleType(ImageView.ScaleType.FIT\_XY);**

**/* 设置这个ImageView对象的宽高，单位为dip \*/**  
**i.setLayoutParams(new Gallery.LayoutParams(120, 120));**  
**return i;**  
**}**

/* 依据距离中央的位移量 利用getScale返回views的大小(0.0f to 1.0f) \*/  
**public float getScale(boolean focused, int offset) {**  
**/* Formula: 1 / (2 ^ offset) \*/**  
**return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));**  
**}**  
}  
}

Step 5:run it,效果如下图:

![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091229/g1.jpg)![](http://p.blog.csdn.net/images/p_blog_csdn_net/Android_Tutor/EntryImages/20091229/g2.jpg)

注明:该代码基本参照**Android SDK**开发范例代码大全.今天晚上就懒一回呵呵...