当前位置:操作系统 > 安卓/Android >>

[Android开发学习31]Gallery之初体验--实现手指直接拖动图片移动

一、基础知识:

 

要实现这一效果,需要一个容器来存放Gallrey显示的图片,这里使用一个继承自BaseAdapter类的派生类来装这些图片。
我们需要监听其事件setOnItemClickListener,从而确定用户当前选中的是哪一张图片。
首先,需要将所有要显示的图片的索引存放在一个int型数组中,然后通过setImageResource方法来设置ImageView要显示的图片资源,最后将
每张图片的ImageView显示在屏幕上。

 

 

 

二、代码展示:

1."main.xml"

[html]
<?xml version="1.0" encoding="utf-8"?> 
<Gallery  
xmlns:android="http://schemas.android.com/apk/res/android"  
  android:id="@+id/Gallery01" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
/> 

<?xml version="1.0" encoding="utf-8"?>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/Gallery01"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
/>
 

2."Activity01.java"

[java] view plaincopyprint?package com.yarin.android.XX; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Gallery; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 
 
public class Activity01 extends Activity 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        //获得Gallery对象  
        Gallery g = (Gallery) findViewById(R.id.Gallery01); 
 
        //添加ImageAdapter给Gallery对象  
        g.setAdapter(new ImageAdapter(this)); 
 
        //设置Gallery的背景  
        g.setBackgroundResource(R.drawable.bg0); 
         
        //设置Gallery的事件监听  
        g.setOnItemClickListener(new OnItemClickListener() { 
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
            { 
                Toast.makeText(Activity01.this,"你选择了"+(position+1)+" 号图片",  
                    Toast.LENGTH_SHORT).show(); 
            } 
        }); 
    } 

package com.yarin.android.XX;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

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

  //获得Gallery对象
  Gallery g = (Gallery) findViewById(R.id.Gallery01);

  //添加ImageAdapter给Gallery对象
  g.setAdapter(new ImageAdapter(this));

  //设置Gallery的背景
  g.setBackgroundResource(R.drawable.bg0);
  
  //设置Gallery的事件监听
  g.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View v, int position, long id)
   {
          Toast.makeText(Activity01.this,"你选择了"+(position+1)+" 号图片",
              Toast.LENGTH_SHORT).show();
   }
  });
 }
}

 

3."ImageAdapter.java"

[java]
package com.yarin.android.XX; 
 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
 
public class ImageAdapter extends BaseAdapter 

    // 定义Context  
    private Context     mContext;        
    // 定义整型数组 即图片源  
    private Integer[]   mImageIds =  
    {                        
            R.drawable.img1,  
            R.drawable.img2,  
            R.drawable.img3,  
            R.drawable.img4,  
            R.drawable.img5,  
            R.drawable.img6,  
            R.drawable.img7, 
            R.drawable.img8,         
    }; 
 
    // 声明 ImageAdapter  
    public ImageAdapter(Context c) 
    { 
        mContext = c; 
    } 
 
    // 获取图片的个数  
    public int getCount() 
    { 
        return mImageIds.length; 
    } 
 
    // 获取图片在库中的位置  
    public Object getItem(int position) 
    { 
        return position; 
    }

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,