Android中Gallery和ImageSwitcher的使用實(shí)例
效果如下:

布局文件activity_main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingTop="30px" >
</ImageSwitcher>
<Gallery
android:id="@+id/gallery1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:spacing="5px"
android:unselectedAlpha="0.6" />
</RelativeLayout>
MainActivity.java代碼如下:
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private int imageId[] = new int[] { R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f,
R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j,
R.drawable.k };
private ImageSwitcher imageSwitcher;
private Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher);
gallery = (Gallery) this.findViewById(R.id.gallery1);
// 設(shè)置動(dòng)畫(huà)效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
imageSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 設(shè)置保持縱橫比居中
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return imageView;
}
});
GalleryAdapter adapter = new GalleryAdapter(MainActivity.this,imageId);
gallery.setAdapter(adapter);
gallery.setSelection(imageId.length / 2);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
imageSwitcher.setImageResource(imageId[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
其中需要的一個(gè)適配器:
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryAdapter extends BaseAdapter {
private int[] imageId;
private Context mContext;
/**
* 穿入上下文和圖片資源數(shù)組
* @param mContext
* @param imageId
*/
public GalleryAdapter(Context mContext, int[] imageId) {
this.mContext = mContext;
this.imageId = imageId;
}
@Override
public int getCount() {
return imageId.length;
}
@Override
public Object getItem(int position) {
return imageId[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView1;
if (convertView == null) {
imageView1 = new ImageView(mContext);
imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
imageView1.setLayoutParams(new Gallery.LayoutParams(180, 135));
TypedArray typedArray = mContext
.obtainStyledAttributes(R.styleable.Gallery);
imageView1.setBackgroundResource(typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground, 0));
imageView1.setPadding(5, 0, 5, 0); // 設(shè)置ImageView的內(nèi)邊距
} else {
imageView1 = (ImageView) convertView;
}
imageView1.setImageResource(imageId[position]); // 為ImageView設(shè)置要顯示的圖片
return imageView1; // 返回ImageView
}
}
到此 OK!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
android 獲取手機(jī)內(nèi)存及 內(nèi)存可用空間的方法
下面小編就為大家?guī)?lái)一篇android 獲取手機(jī)內(nèi)存及SD卡內(nèi)存可用空間的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Android ExpandableListView長(zhǎng)按事件的完美解決辦法
本篇文章是對(duì)Android中ExpandableListView長(zhǎng)按事件的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
限時(shí)搶購(gòu)秒殺系統(tǒng)架構(gòu)分析與實(shí)戰(zhàn)
這篇文章主要介紹了限時(shí)搶購(gòu)秒殺系統(tǒng)架構(gòu)分析與實(shí)戰(zhàn) 的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android獲取系統(tǒng)時(shí)間以及網(wǎng)絡(luò)時(shí)間
這篇文章主要為大家詳細(xì)介紹了Android獲取系統(tǒng)時(shí)間以及網(wǎng)絡(luò)時(shí)間的方法,感興趣的小伙伴們可以參考一下2016-07-07
Android EditText輸入框?qū)崿F(xiàn)下拉且保存最近5個(gè)歷史記錄思路詳解
今天給大家介紹Android EditText輸入框?qū)崿F(xiàn)下拉且保存最近5個(gè)歷史記錄功能,android實(shí)現(xiàn)文本框下拉利用sharedpreferences來(lái)保存每次app啟動(dòng)和關(guān)閉時(shí)已經(jīng)填寫(xiě)的數(shù)值,具體代碼跟隨小編一起看看吧2021-07-07
零基礎(chǔ)學(xué)習(xí)教程之Linux下搭建android開(kāi)發(fā)環(huán)境
這篇文章主要介紹了Linux下搭建android開(kāi)發(fā)環(huán)境,特別適合零基礎(chǔ)的同學(xué)學(xué)習(xí),想要在Linux及ubuntu11.10下配置android4.0.3開(kāi)發(fā)環(huán)境的朋友可以參考一下2015-12-12
Android中兩個(gè)類(lèi)讓你再也不用實(shí)現(xiàn)onActivityResult()
這篇文章主要給大家介紹了關(guān)于Android中兩個(gè)類(lèi)讓你再也不用實(shí)現(xiàn)onActivityResult()的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-08-08
Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
這篇文章主要介紹了Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡的相關(guān)資料,需要的朋友可以參考下2016-01-01

