第31讲 UI组件之 Gallery画廊控件
第31講 UI組件之 Gallery畫廊控件
1.Gallery的簡介
Gallery(畫廊)是一個鎖定中心條目并且擁有水平滾動列表的視圖,一般用來瀏覽圖片,并且可以響應事件顯示信息。Gallery只能水平顯示一行,且Gallery列表中的圖片會根據不同的拖動情況向左或向右移動,直到最后一張圖片為止。Gallery還可以和ImageSwitcher組件結合使用來實現一個通過縮略圖來瀏覽圖片的效果。
?
Gallery常用的XML屬性
| 屬性名稱 | 描述 | |||||||||||||||||||||||||||||||||||||||
| android:animationDuration | 設置布局變化時動畫的轉換所需的時間(毫秒級)。僅在動畫開始時計時。該值必須是整數,比如:100。 | |||||||||||||||||||||||||||||||||||||||
| android:gravity | 指定在對象的X和Y軸上如何放置內容。指定一下常量中的一個或多個(使用 “|”分割)
| |||||||||||||||||||||||||||||||||||||||
| android:spacing | 圖片之間的間距 | |||||||||||||||||||||||||||||||||||||||
| android:unselectedAlpha | 設置未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”。 |
?
一、實現圖片的左右滑動瀏覽效果。
//Gallery的使用方法類似于其余的ViewGroup控件,使用adapter進行布局。
Gallery gallery=(Gallery)findViewById(R.id.gallery1);
//設置圖片適配器
gallery.setAdapter(new MyAdapter(image,this));
//image:定義整型數組 即圖片源??
final int[] image=new int[] {R.drawable.attack , R.drawable .boy1 , R.drawable .boy2 , R.drawable .doupo };
?
MyAdapter定義如下:
private class MyAdapter extends BaseAdapter{
private int[] image;
private Context context;
public MyAdapter(int[] image, Context context) {
super();
this.image = image;
this.context = context;
}
public int getCount() { return image.length; } //獲取圖片的個數
public Object getItem(int arg0) { return null; }
public long getItemId(int arg0) { return 0; }
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imageView=new ImageView(context);
imageView.setImageResource(image[arg0]); //給ImageView設置資源
return imageView;
}
}
?
二、實現縮略圖加放大圖顯示
對上述程序做修改:
首先,將layout設置為上方為ImageView,下方為Gallery顯示縮略圖。
之后,通過設置點擊Gallery時設置圖片到ImageView中。
final ImageView imageView=(ImageView)findViewById(R.id.imageView1);
Gallery gallery=(Gallery) findViewById(R.id.gallery1);
//設置圖片適配器
gallery.setAdapter(new MyAdapter(image,this));
//設置監聽器?
gallery.setOnItemClickListener(newOnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, intposition, long arg3) {
imageView.setImageResource(image[position]);
}
});
?
public View getView(int arg0, View arg1,ViewGroup arg2) {
ImageView imageView=new ImageView(context);
//設置布局圖片120x120顯示?
imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));?
imageView.setImageResource(image[arg0]); //給ImageView設置資源
return imageView;
}
?
轉載于:https://www.cnblogs.com/anyuan9/p/6171591.html
總結
以上是生活随笔為你收集整理的第31讲 UI组件之 Gallery画廊控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端资源分享
- 下一篇: C#中析构函数,命名空间及字符串的运用(