萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android開發之實現手指直接拖動圖片移動

Android開發之實現手指直接拖動圖片移動

一、基礎知識:

 要實現這一效果,需要一個容器來存放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; 

    } 

 

    // 獲取圖片在庫

copyright © 萬盛學電腦網 all rights reserved