Android入門(mén)之Gallery用法實(shí)例解析
本文實(shí)例介紹的Android的Gallery控件是個(gè)很不錯(cuò)的看圖控件,可以大大減輕開(kāi)發(fā)者對(duì)于看圖功能的開(kāi)發(fā),并且效果也很美觀。本文實(shí)例中的Gallery的用法,主要實(shí)現(xiàn)用反射機(jī)制來(lái)動(dòng)態(tài)讀取資源中的圖片。
該實(shí)例代碼運(yùn)行的效果圖如下:
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" > <Gallery android:id="@+id/gallery" android:layout_height="fill_parent" android:layout_width="fill_parent"></Gallery> </LinearLayout>
Java程序源碼如下:
package com.testImageView;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class testImageView extends Activity {
private Gallery mGallery;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGallery = (Gallery)findViewById(R.id.gallery);
try {
mGallery.setAdapter(new ImageAdapter(this));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
testImageView.this.setTitle(String.valueOf(position));
}
});
}
/*
* class ImageAdapter is used to control gallery source and operation.
*/
private class ImageAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Integer> imgList=new ArrayList<Integer>();
private ArrayList<Object> imgSizes=new ArrayList<Object>();
public ImageAdapter(Context c) throws IllegalArgumentException, IllegalAccessException{
mContext = c;
//用反射機(jī)制來(lái)獲取資源中的圖片ID和尺寸
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields)
{
if (!"icon".equals(field.getName()))//除了icon之外的圖片
{
int index=field.getInt(R.drawable.class);
//保存圖片ID
imgList.add(index);
//保存圖片大小
int size[]=new int[2];
Bitmap bmImg=BitmapFactory.decodeResource(getResources(),index);
size[0]=bmImg.getWidth();size[1]=bmImg.getHeight();
imgSizes.add(size);
}
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView (mContext);
//從imgList取得圖片ID
i.setImageResource(imgList.get(position).intValue());
i.setScaleType(ImageView.ScaleType.FIT_XY);
//從imgSizes取得圖片大小
int size[]= new int[2];
size=(int[]) imgSizes.get(position);
i.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));
return i;
}
};
}
- Android 使用Gallery實(shí)現(xiàn)3D相冊(cè)(附效果圖+Demo源碼)
- Android開(kāi)發(fā)學(xué)習(xí)筆記 Gallery和GridView淺析
- Android入門(mén)之Gallery+ImageSwitcher用法實(shí)例解析
- 怎樣刪除android的gallery中的圖片實(shí)例說(shuō)明
- Android控件Gallery3D效果實(shí)例代碼
- Android App開(kāi)發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫(huà)廊的實(shí)例
- android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動(dòng)效果實(shí)例
- Android App中使用Gallery制作幻燈片播放效果
- Android自定義HorizontalScrollView打造超強(qiáng)Gallery效果
- Android編程滑動(dòng)效果之Gallery仿圖像集瀏覽實(shí)現(xiàn)方法
- Android開(kāi)發(fā)實(shí)現(xiàn)Gallery畫(huà)廊效果的方法
- Android開(kāi)發(fā)中畫(huà)廊視圖Gallery的兩種使用方法分析
相關(guān)文章
Kotlin?掛起函數(shù)CPS轉(zhuǎn)換原理解析
這篇文章主要為大家介紹了Kotlin?掛起函數(shù)CPS轉(zhuǎn)換原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android通過(guò)交互實(shí)現(xiàn)貝塞爾曲線(xiàn)的繪制
本篇我們將介紹簡(jiǎn)單的交互式繪圖,通過(guò)獲取觸控位置來(lái)設(shè)定貝塞爾曲線(xiàn)的控制點(diǎn),從而實(shí)現(xiàn)交互式繪制曲線(xiàn),感興趣的小伙伴可以了解一下2022-05-05
android shape實(shí)現(xiàn)陰影或模糊邊效果
這篇文章主要介紹了android shape實(shí)現(xiàn)陰影或模糊邊效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Android開(kāi)發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
這篇文章主要為大家詳細(xì)介紹了Android中Handler機(jī)制的使用,文中的示例代碼講解詳細(xì),有需要的朋友可以借鑒參考下,希望能夠?qū)Υ蠹矣兴鶐椭?/div> 2022-11-11
Android自定義扇形倒計(jì)時(shí)實(shí)例代碼
最近工作中需要做一個(gè)倒計(jì)時(shí),是那種一個(gè)圓,慢慢的被吃掉的動(dòng)畫(huà)倒計(jì)時(shí),由于自己是android小白,效果還不是多滿(mǎn)意,先給大家分享實(shí)例代碼,僅供大家參考2017-03-03
Android實(shí)現(xiàn)Toast提示框圖文并存的方法
這篇文章主要介紹了Android實(shí)現(xiàn)Toast提示框圖文并存的方法,實(shí)例分析了Toast提示框的參數(shù)設(shè)置及圖文調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-01-01
Android ToolBar 修改邊距的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ToolBar 修改邊距的實(shí)現(xiàn)方法的相關(guān)資料,通過(guò)此文希望能幫助到大家,需要的朋友可以參考下2017-08-08最新評(píng)論

