Android開發(fā)中畫廊視圖Gallery的兩種使用方法分析
本文實例講述了Android開發(fā)中畫廊視圖Gallery的兩種使用方法。分享給大家供大家參考,具體如下:
第一種方法:
第一步:設(shè)計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;
// 里面所有的方法表示的是可以根據(jù)指定的顯示圖片的數(shù)量,進(jìn)行每個圖片的處理
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() { // 取得要顯示內(nèi)容的數(shù)量
return image.length;
}
public Object getItem(int position) { // 每個資源的位置
return image[position];
}
public long getItemId(int position) { // 取得每個項的ID
return image[position];
}
// 將資源設(shè)置到一個組件之中,很明顯這個組件是ImageView
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(context);
iv.setBackgroundColor(0xFFFFFFFF);
iv.setImageResource(image[position]);// 給ImageView設(shè)置資源
iv.setScaleType(ImageView.ScaleType.CENTER);// 設(shè)置對齊方式
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;
@Override
public 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();
}
}
}
第二種方法:
第一步:設(shè)計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>>();
@Override
public 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設(shè)置合適的適配器
gallery.setOnItemClickListener(new OnItemClickListenerImpl());
}
public class OnItemClickListenerImpl implements OnItemClickListener { // gallery的點擊事件
@Override
public 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,如果資源圖片數(shù)量比較多,用數(shù)組的方法一一定義,就不太合適,這種方法最好了。
Field[] fields = R.drawable.class.getDeclaredFields();
for (int x = 0; x < fields.length; x++) {
if (fields[x].getName().startsWith("ispic_")) { // 根據(jù)圖片的名稱取出想要的圖片
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 {
@Override
public 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;
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android實現(xiàn)漂亮的Gallery畫廊
- Android中Gallery和ImageSwitcher的使用實例
- Android TV開發(fā):實現(xiàn)3D仿Gallery效果的實例代碼
- Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器
- Android高級組件Gallery畫廊視圖使用方法詳解
- Android UI控件之Gallery實現(xiàn)拖動式圖片瀏覽效果
- Android 使用自定義RecyclerView控件實現(xiàn)Gallery效果
- Android開發(fā)實現(xiàn)Gallery畫廊效果的方法
- Android使用gallery和imageSwitch制作可左右循環(huán)滑動的圖片瀏覽器
- Android之Gallery使用例子
- Android使用Gallery實現(xiàn)照片拖動的特效
相關(guān)文章
談?wù)凙ndroid的三種網(wǎng)絡(luò)通信方式
Android平臺有三種網(wǎng)絡(luò)接口可以使用,他們分別是:java.net.*(標(biāo)準(zhǔn)Java接口)、Org.apache接口和Android.net.*(Android網(wǎng)絡(luò)接口)。本文詳細(xì)的介紹,有興趣的可以了解一下。2017-01-01
Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中(Saving Data in SQL Databases)
這篇文章主要介紹了Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫中的(Saving Data in SQL Databases)2014-10-10
Android重寫TextView實現(xiàn)文字整齊排版的方法(附demo源碼下載)
這篇文章主要介紹了Android重寫TextView實現(xiàn)文字整齊排版的方法,結(jié)合實例形式分析了Android重寫TextView實現(xiàn)文字整齊排版的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02
Android自定義View實現(xiàn)帶數(shù)字的進(jìn)度條實例代碼
這篇文章主要介紹了Android自定義View實現(xiàn)帶數(shù)字的進(jìn)度條實例代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03
Crashlytics Android 異常報告統(tǒng)計管理(詳解)
下面小編就為大家?guī)硪黄狢rashlytics Android 異常報告統(tǒng)計管理(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Android實現(xiàn)軟件列表的點擊啟動另外一個程序功能【附demo源碼下載】
這篇文章主要介紹了Android實現(xiàn)軟件列表的點擊啟動另外一個程序功能,涉及Android針對應(yīng)用程序的讀取、加載、啟動等操作相關(guān)技巧,需要的朋友可以參考下2016-07-07

