Android基础教程之-----访Iphone 拖动相片特效Gallery的简单应用.

Step 1:准备图片素材.
将icon2,icon3,icon4,icon5,icon6五张图片导入res/drawable里加上icon.png本身一共有6张图片.
Step 2:新建Android工程,命名为GalleryDemo.
Step 3:设计UI,修改main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns: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" 
    > 
    <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" 
    /> 
    <Gallery 
    android:id="@+id/myGallery1"    
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:gravity="bottom"    
    />    
</LinearLayout> 

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,效果如下图:
 
注明:该代码基本参照Android SDK开发范例代码大全.今天晚上就懒一回呵呵...


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值