Android開發(fā)之組件GridView簡單使用方法示例
本文實(shí)例講述了Android開發(fā)之組件GridView簡單使用方法。分享給大家供大家參考,具體如下:
案例:簡單的圖片瀏覽器,保存圖片到相冊
保存圖片到相冊 方法代碼:http://www.dbjr.com.cn/article/158668.htm
廢話不多說先上效果;

具體實(shí)現(xiàn):
首先是布局文件: 1.一個GridView(展示所有的圖片) 2.一個ImageView(放選中的圖片)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <!--定義一個GridView組件--> <GridView android:id="@+id/grid01" android:layout_width="match_parent" android:layout_height="wrap_content" android:horizontalSpacing="1pt" android:verticalSpacing="1pt" android:numColumns="3" android:gravity="center"/> <!--定義一個ImageView組件--> <ImageView android:id="@+id/image_view" android:layout_width="240dp" android:layout_height="240dp" android:layout_gravity="center_horizontal"/> </LinearLayout>
然后是點(diǎn)擊事件的設(shè)置:
其中arry_list只有一個ImageView放圖片 代碼下面給出
public class MainActivity extends Activity {
private GridView gridView;
//定義字符串?dāng)?shù)組作為提示的文本
private ImageView imageView;
private int[] photoId = new int[]{
R.drawable.huangjindiao, R.drawable.piaopiao,
R.drawable.xiaochouyu, R.drawable.paodangyu,
R.drawable.addphoto, R.drawable.huangjinxiahu
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//創(chuàng)建一個list對象,list對象的元素是Map
List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
for (int i = 0 ; i < photoId.length ; i ++ ){
Map<String,Object> photolist = new HashMap<String, Object>();
photolist.put("image",photoId[i]);
listItems.add(photolist);
}
//獲取圖片的imageView
imageView = (ImageView) findViewById(R.id.image_view);
//創(chuàng)建一個SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,
//arry_list只有一個ImageView放圖片 代碼下面給出
R.layout.array_list,
new String[]{"image"}, new int[]{R.id.image});
gridView = (GridView) findViewById(R.id.grid01);
//為gridview設(shè)置adapter
gridView.setAdapter(simpleAdapter);
//添加列表項中被選中的監(jiān)聽器
gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//顯示當(dāng)前被選中的圖片
imageView.setImageResource(photoId[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//添加列表中被單擊的監(jiān)聽器
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//顯示被單擊的圖片
imageView.setImageResource(photoId[position]);
}
});
//點(diǎn)擊圖片保存
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] PERMISSIONS = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE" };
//檢測是否有寫的權(quán)限
int permission = ContextCompat.checkSelfPermission(MainActivity.this,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 沒有寫的權(quán)限,去申請寫的權(quán)限,會彈出對話框
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS,1);
}
try {
//創(chuàng)建savephoto類保存圖片
SavePhoto savePhoto = new SavePhoto(MainActivity.this);
savePhoto.SaveBitmapFromView(imageView);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
}
}
值得注意的一點(diǎn):array_list:
<?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/image" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="fitXY"> </ImageView>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android?Spinner和GridView組件的使用示例
- Android 控件GridView使用案例講解
- Android使用GridView實(shí)現(xiàn)表格分割線效果
- Android自定義gridView仿頭條頻道拖動管理功能
- Android中GridView插件的使用方法
- Android控件gridview實(shí)現(xiàn)單行多列橫向滾動效果
- Android通過實(shí)現(xiàn)GridView的橫向滾動實(shí)現(xiàn)仿京東秒殺效果
- Android使用Gridview單行橫向滾動顯示
- Android Gridview布局出現(xiàn)滾動條或組件沖突解決方法
相關(guān)文章
Android如何判斷一個點(diǎn)在不在多邊形區(qū)域內(nèi)
這篇文章主要為大家詳細(xì)介紹了Android判斷一個點(diǎn)在不在多邊形區(qū)域內(nèi)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android游戲開發(fā)學(xué)習(xí)①彈跳小球?qū)崿F(xiàn)方法
這篇文章主要介紹了Android游戲開發(fā)學(xué)習(xí)①彈跳小球?qū)崿F(xiàn)方法,涉及Android通過物理引擎BallThread類模擬小球運(yùn)動的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android 實(shí)現(xiàn)自定義圓形listview功能的實(shí)例代碼
這篇文章主要介紹了Android 實(shí)現(xiàn)自定義圓形listview功能的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Android利用Paint自定義View實(shí)現(xiàn)進(jìn)度條控件方法示例
這篇文章主要給大家介紹了關(guān)于Android利用Paint自定義View實(shí)現(xiàn)進(jìn)度條控件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
詳解Android系統(tǒng)中跨應(yīng)用數(shù)據(jù)分享功能的實(shí)現(xiàn)
這篇文章主要介紹了Android系統(tǒng)中跨應(yīng)用數(shù)據(jù)分享功能的實(shí)現(xiàn),文中分為發(fā)送文字、二進(jìn)制內(nèi)容和圖片三種情況來講,需要的朋友可以參考下2016-04-04
實(shí)例講解Android多線程應(yīng)用開發(fā)中Handler的使用
這篇文章主要介紹了Android多線程應(yīng)用開發(fā)中Handler的使用,Handle主要被用來更新UI和處理消息,需要的朋友可以參考下2016-01-01

