編輯:關於Android編程
Gallery與Spinner有共同父類:AbsPinner,說明Gallery與Spinner都是一個列表框。它們之間的區別在於Spinner顯示的是一個垂直的列表選擇框,而Gallery顯示的是一個水平的列表選擇框。Spinner的作用是供用戶選擇,而Gallery則允許用戶通過拖動查看上一個,下一個。
Gallery用法與Spinner的用法形似,只要為它提供一個內容Adapter就可以了。Adapter的getView方法返回View作為Gallery列表的列表項。如果程序需要監控Gallery選擇項的改變,可以添加OnItemSelectedListener監聽即可。
Gallery 的xml屬性

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPs/Cw+bNqLn90ru49rvDtcbGrMD919PAtMrsz6RHYWxsZXJ5PC9wPgo8cD48YnI+CjwvcD4KPHA+o6gxo6lhY3Rpdml0eV9tYWluLnhtbCAgICAgsry+1tK7uPZJbWFnZVN3aXRjaGVyILrNR2FsbGVyeSAgICAgICAgIEltYWdlU3dpdGNoZXLTw9Paz9TKvkdhbGxlcnnRodbQtcTNvMasPGJyPgo8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">
(2)MainActivity.java
package com.example.gallery;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RadioGroup.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
//定義組件
private ImageSwitcher imgSwt = null;
private Gallery gallery = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取布局組件
imgSwt = (ImageSwitcher) findViewById(R.id.imgSwt);
gallery = (Gallery) findViewById(R.id.gallery);
//圖片
final int images[] = new int[]{
R.drawable.name01,R.drawable.name02,R.drawable.name03,
R.drawable.name04,R.drawable.name05,R.drawable.name06,
R.drawable.name07,R.drawable.name08,R.drawable.name09,
R.drawable.name10,R.drawable.name11,R.drawable.name12
};
//設置圖片切換效果
imgSwt.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
imgSwt.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
//設置ViewFactory對象
imgSwt.setFactory(new ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(350,350));
return imageView;
}
});
//創建BaseAdapter對象,負責提供Gallery顯示所有圖像
BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//創建imageview
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
return imageView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public int getCount() {
return images.length;
}
};
//給Gallery設置適配器
gallery.setAdapter(adapter);
//添加事件
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> parent, View view,
int position, long id) {
imgSwt.setImageResource(images[position]);
}
@Override
public void onNothingSelected(AdapterView> parent) {
// TODO Auto-generated method stub
}
});
}
}

java代碼質量檢測評價工具
如果能在構建代碼前發現代碼中潛在的問題會怎麼樣呢?很有趣的是,Eclipse 插件中就有這樣的工具,比如 JDepend 和 CheckStyle,它們能幫您在軟件問題暴
android性能優化實戰前篇
前言:最近因為某項目cpu,內存的使用率實在讓人不敢恭維;手機發燙,電量下降已經讓用戶無法忍受;頻繁快速迭代發版導致各種性能問題突出;由於之前產品不考慮低端手機情況,低端
自定義控件三部曲之繪圖篇(十一)——Paint之setXfermode(二)
這篇文章將逐個講解每個模式的意義。這裡所講的各種模式,在大家理解了之後可以回過頭來看看setColorFilter(new PorterDuffColorFilter(C
Android菜鳥的成長筆記(28)——Google官方對Andoird 2.x提供的ActionBar支持
在Google官方Android設計指南中(鏈接:http://www.apkbus.com/design/get-started/ui-overview.html)有一