Android控件之Gallery用法實(shí)例分析
本文實(shí)例講述了Android控件之Gallery用法。分享給大家供大家參考。具體如下:
Gallery組件主要用于橫向顯示圖像列表,不過按常規(guī)做法。Gallery組件只能有限地顯示指定的圖像。也就是說,如果為Gallery組件指定了10張圖像,那么當(dāng)Gallery組件顯示到第10張時(shí),就不會再繼續(xù)顯示了。這雖然在大多數(shù)時(shí)候沒有什么關(guān)系,但在某些情況下,我們希望圖像顯示到最后一張時(shí)再重第1張開始顯示,也就是循環(huán)顯示。要實(shí)現(xiàn)這種風(fēng)格的Gallery組件,就需要對Gallery的Adapter對象進(jìn)行一番改進(jìn)。
以下通過Gallery模擬循環(huán)顯示圖像,在單擊某一個(gè)Gallery組件中的圖像時(shí)在下方顯示一個(gè)放大的圖像(使用ImageSwitcher組件)。
目錄結(jié)構(gòu)

main.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"> <!-- android:unselectedAlpha: 設(shè)置未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”。 --> <Gallery android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="10dip" android:unselectedAlpha="1.2" android:id="@+id/gallery" android:layout_marginTop="30dp"/> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" /> </LinearLayout>
GalleryActivity類:
package com.ljq.ga;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class GalleryActivity extends Activity implements ViewFactory {
private Gallery gallery = null;
private ImageSwitcher imageSwitcher=null;
int[] imageIDs={
R.drawable.p1,R.drawable.p2,R.drawable.p3,
R.drawable.p4,R.drawable.p5,R.drawable.p6,
R.drawable.p7,R.drawable.p8 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);
// 設(shè)置ImageSwitcher組件的工廠對象
imageSwitcher.setFactory(this);
// 設(shè)置ImageSwitcher組件顯示圖像的動畫效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
gallery = (Gallery) findViewById(R.id.gallery);
ImageViewAdapter adapter=new ImageViewAdapter(GalleryActivity.this, imageIDs);
gallery.setAdapter(adapter);
gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//選中Gallery中某個(gè)圖像時(shí),在ImageSwitcher組件中放大顯示該圖像
imageSwitcher.setImageResource(imageIDs[position%imageIDs.length]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
gallery.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ljq", "parent="+parent.getClass().getName()); //Gallery
Log.i("ljq", "view="+view.getClass().getName()); //ImageView
Log.i("ljq", "position=" + position); //1
Log.i("ljq", "id=" + id);//1
Gallery gl=(Gallery)parent;
ImageView iv=(ImageView)view;
}
});
}
// ImageSwitcher組件需要這個(gè)方法來創(chuàng)建一個(gè)View對象(一般為ImageView對象)
// 來顯示圖像
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
}
ImageViewAdapter自定義適配器:
package com.ljq.ga;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ImageViewAdapter extends BaseAdapter{
private int[] imageIDs=null;
private Context context=null;
public ImageViewAdapter(Context context, int[] imageIDs) {
this.context=context;
this.imageIDs=imageIDs;
}
//用于返回圖像總數(shù),要注意的是,這個(gè)總數(shù)不能大于圖像的實(shí)際數(shù)(可以小于圖像的實(shí)際數(shù)),否則會拋出越界異常。
public int getCount() {
//優(yōu)化一
//return imageIDs.length;
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
return imageIDs[position];
}
public long getItemId(int position) {
return position;
}
//ScaleType的用法
//CENTER/center 按圖片的原來size居中顯示,當(dāng)圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
//CENTER_CROP/centerCrop 按比例擴(kuò)大圖片的size居中顯示,使得圖片長 (寬)等于或大于View的長(寬)
//CENTER_INSIDE/centerInside 將圖片的內(nèi)容完整居中顯示,通過按比例縮小 或原來的size使得圖片長/寬等于或小于View的長/寬
//FIT_CENTER/fitCenter 把圖片按比例擴(kuò)大/縮小到View的寬度,居中顯示
//FIT_END/fitEnd 把 圖片按比例擴(kuò)大/縮小到View的寬度,顯示在View的下部分位置
//FIT_START/fitStart 把 圖片按比例擴(kuò)大/縮小到View的寬度,顯示在View的上部分位置
//FIT_XY/fitXY 把圖片 不按比例 擴(kuò)大/縮小到View的大小顯示
//MATRIX/matrix 用矩陣來繪制
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(context);
//優(yōu)化二,通過取余來循環(huán)取得imageIDs數(shù)組中的圖像資源ID,取余可以大大較少資源的浪費(fèi)
iv.setImageResource(imageIDs[position%imageIDs.length]);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
iv.setLayoutParams(new LinearLayout.LayoutParams(77,77));//把圖片縮小原來的60%
return iv;
}
}
運(yùn)行結(jié)果

希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- Android從系統(tǒng)Gallery獲取圖片具體實(shí)現(xiàn)
- Android入門之Gallery用法實(shí)例解析
- Android 使用Gallery實(shí)現(xiàn)3D相冊(附效果圖+Demo源碼)
- Android開發(fā)學(xué)習(xí)筆記 Gallery和GridView淺析
- Android入門之Gallery+ImageSwitcher用法實(shí)例解析
- 怎樣刪除android的gallery中的圖片實(shí)例說明
- Android控件Gallery3D效果實(shí)例代碼
- Android控件系列之相冊Gallery&Adapter適配器入門&控件縮放動畫入門
- Android App開發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫廊的實(shí)例
- Android高級組件Gallery畫廊視圖使用方法詳解
相關(guān)文章
Android 自定義彈出菜單和對話框功能實(shí)例代碼
Android 開發(fā)當(dāng)中,可能會存在許多自定義布局的需求,比如自定義彈出菜單(popupWindow),以及自定義對話框(Dialog)。下面通過本文給大家介紹Android 自定義彈出菜單和對話框功能實(shí)例代碼,感興趣的朋友一起看看吧2017-08-08
android 修改launcher行數(shù)和列數(shù)的方法
這篇文章主要介紹了android 修改launcher行數(shù)和列數(shù)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Android之ArcSlidingHelper制作圓弧滑動效果
這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動效果,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09

