在iphone手機上用手指拖動圖片移動,這功能很Cool,咱Android也不能含糊,用Gallery類就可以實現這個功能。

今天我就做了個小小的電子相冊:
假設你已經新建好了專案。
首先我們事先準備好的圖片存放在drawable資料夾下,然後新建一個介面:
public interface ImageResource {
用一個Integer陣列保存圖像資源
Integer[] dImageID = {
R.drawable.sample_0,
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7,
};
}
這個介面裡有一個int型的陣列保存了咱drawable下的圖片,這樣方便以後修改。
要用Gallery實現這個功能,先要有一個容器來存放Gallery要顯示的圖片,我使用的是一個繼承自BaseAdapter的ImageAdapter。這個電子相冊分兩部分,上部分是使用者可以拖動圖像清單,下部分是使用者選中上面圖像後更大的顯示出來。

 

負責上部分顯示的ImageView是imageAll,負責下部分顯示的是imageOne,代碼如下:
import android.app.Activity;
import android.content.CoNtext;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Gallery;

 

public class HelloGallery extends Activity {
這個ImageView是用來顯示單個圖像
private ImageView imageOne;
這個ImageView是用來顯示所有圖像
private ImageView imageAll;

 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

 

imageOne = (ImageView)findViewById(R.id.imageView);

 

新建一個Gallery類,這是是實現拖動效果的關鍵
Gallery is 一個用來水準捲動顯示物件的視圖
Gallery gallery = (Gallery)findViewById(R.id.gallery);

 

ImageAdapter就是gallery要顯示的物件
gallery.setAdapter(new ImageAdapter(this));

 

}
實現ImageResource介面獲得我自己創建的圖像資源
class ImageAdapter extends BaseAdapter implements ImageResource{
每一個gallery中圖像的背景資源
private int galleryItemBackground;
private CoNtext coNtext;

 

public ImageAdapter(CoNtext coNtext) {
this.coNtext = coNtext;
這裡實現的功能就是上半部分每個圖像的那個背景框
對應的xml檔就是attr.xml
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
galleryItemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}

 

public int getCount() {
return dImageID.length;
}

 

public Object getItem(int position) {
return position;
}

 

這個方法獲得是呈現在使用者面前的圖像下標
public long getItemId(int position) {
將此索引的圖像設為imageOne顯示 imageOne.setImageResource(ImageAdapter.dImageID[position]);
imageOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
return position;
}

 

這個方法返回的ImageView就是實現拖動效果的圖像
public View getView(int position, View convertView, ViewGroup parent) {
imageAll= new ImageView(coNtext);
設置圖像資源
imageAll.setImageResource(dImageID[position]);
設置imageAll視圖為120×120
imageAll.setLayoutParams(new Gallery.LayoutParams(120,120));
設置圖像相對於視圖的比例,FIT_XY表示充滿X和Y軸
imageAll.setScaleType(ImageView.ScaleType.FIT_XY);
設置imageAll中每一個Item的背景資源
imageAll.setBackgroundResource(galleryItemBackground);
return imageAll;
}
}
}
佈局檔如下:
Mail.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="HTTP://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Gallery>

 

<ImageView android:id="@+id/imageView"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ImageView>
</LinearLayout>
下面就是上文中提到的attr.xml:
Attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
<declare-styleable name="LabelView">
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
 
 
來源:
https://www.google.com.tw/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CB4QFjAA&url=http%3A%2F%2Fblog.csdn.net%2Fldj299%2Farticle%2Fdetails%2F6138460&ei=TO08VZuxB6awmwX9xICQDA&usg=AFQjCNGOqfKMt-wSpftGRT9sgN6EGz_q1g&sig2=vta0USZvst6KDZJaGtIYJg
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()