本文實例講述了Android開發中畫廊視圖Gallery的兩種使用方法。分享給大家供大家參考,具體如下:
第一種方法:
第一步:設計xml布局文件
代碼如下:main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Gallery android:id="@+id/myGallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:spacing="3px" android:text="@string/hello" /></LinearLayout>
第二步:自定義一個適配器,這個適配器繼承BaseAdapter這個類
代碼如下:
package net.loonggg.gallery;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageGalleryAdapter extends BaseAdapter {private Context context;// 里面所有的方法表示的是可以根據指定的顯示圖片的數量,進行每個圖片的處理private int[] image = new int[] { R.drawable.ispic_a, R.drawable.ispic_b,R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e };public ImageGalleryAdapter(Context context) {this.context = context;}public int getCount() { // 取得要顯示內容的數量return image.length;}public Object getItem(int position) { // 每個資源的位置return image[position];}public long getItemId(int position) { // 取得每個項的IDreturn image[position];}// 將資源設置到一個組件之中,很明顯這個組件是ImageViewpublic View getView(int position, View convertView, ViewGroup parent) {ImageView iv = new ImageView(context);iv.setBackgroundColor(0xFFFFFFFF);iv.setImageResource(image[position]);// 給ImageView設置資源iv.setScaleType(ImageView.ScaleType.CENTER);// 設置對齊方式iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));return iv;}}第三步:主方法:
package net.loonggg.gallery;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;import android.widget.Toast;public class GalleryActivity extends Activity {private Gallery myGallery;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);myGallery = (Gallery) findViewById(R.id.myGallery);myGallery.setAdapter(new ImageGalleryAdapter(this));myGallery.setOnItemClickListener(new OnItemClickListenerImpl());}private class OnItemClickListenerImpl implements OnItemClickListener {public void onItemClick(AdapterView<?> parent, View view, int position,long id) {Toast.makeText(GalleryActivity.this, String.valueOf(position),Toast.LENGTH_SHORT).show();}}}第二種方法:
第一步:設計xml布局文件
代碼如下:main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="bottom" android:orientation="vertical" > <ImageSwitcher android:id="@+id/is" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ImageSwitcher> <Gallery android:id="@+id/myGallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:spacing="3px" /></LinearLayout>
gallery_item.xml文件:
代碼如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF" android:orientation="horizontal" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" /></LinearLayout>
第二步:MainActivity
代碼如下:
package net.loonggg.gallery2;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.SimpleAdapter;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity {private ImageSwitcher is;private Gallery gallery;private SimpleAdapter adapter;private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);is = (ImageSwitcher) findViewById(R.id.is);is.setFactory(new ViewFactoryImpl());initAdapter();gallery = (Gallery) findViewById(R.id.myGallery);gallery.setAdapter(adapter); // 為gallery設置合適的適配器gallery.setOnItemClickListener(new OnItemClickListenerImpl());}public class OnItemClickListenerImpl implements OnItemClickListener { // gallery的點擊事件@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {Map<String, Integer> map = (Map<String, Integer>) parent.getAdapter().getItem(position);is.setImageResource(map.get("image"));}}public void initAdapter() { // 這個方法的功能是:從R.java文件中獲取圖片資源的id,如果資源圖片數量比較多,用數組的方法一一定義,就不太合適,這種方法最好了。Field[] fields = R.drawable.class.getDeclaredFields();for (int x = 0; x < fields.length; x++) {if (fields[x].getName().startsWith("ispic_")) { // 根據圖片的名稱取出想要的圖片Map<String, Integer> map = new HashMap<String, Integer>();try {map.put("image", fields[x].getInt(R.drawable.class));} catch (Exception e) {e.printStackTrace();}list.add(map);}}adapter = new SimpleAdapter(MainActivity.this, list,R.layout.grid_item, new String[] { "image" },new int[] { R.id.iv });}public class ViewFactoryImpl implements ViewFactory {@Overridepublic View makeView() {ImageView iv = new ImageView(MainActivity.this);iv.setBackgroundColor(0xFFFFFFFF);iv.setScaleType(ImageView.ScaleType.CENTER);iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));return iv;}}}希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答