Android實現(xiàn)漂亮的Gallery畫廊
本文實例為大家分享了Android實現(xiàn)Gallery畫廊的具體代碼,供大家參考,具體內(nèi)容如下
僅是實現(xiàn)基本功能,頁面粗糙請見諒
圖片下標(biāo)0開始
activity_main.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.gallery.MainActivity"> <Gallery android:id="@+id/galPicture" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
GalleryAdapter.java頁面:
package com.example.gallery; import android.content.Context; 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 Context mContext; int[] images = {R.mipmap.apple, R.mipmap.banana, R.mipmap.bicycle, R.mipmap.chair,R.mipmap.chopsticks, R.mipmap.dog, R.mipmap.fish, R.mipmap.pear}; //本地圖片 public GalleryAdapter (Context context) { this.mContext = context; } @Override public int getCount() { return images.length; } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ImageView image = new ImageView(mContext); image.setImageResource(images[i]); //設(shè)置圖片 image.setAdjustViewBounds(true); //是否調(diào)整邊框 image.setLayoutParams(new Gallery.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return image; } }
MainActivity.java頁面:
package com.example.gallery; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.Toast; public class MainActivity extends AppCompatActivity { GalleryAdapter galleryAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Gallery galPicture = findViewById(R.id.galPicture); galleryAdapter = new GalleryAdapter(MainActivity.this); galPicture.setAdapter(galleryAdapter); //相應(yīng)的點擊事件 galPicture.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "圖片" + i, Toast.LENGTH_LONG).show(); } }); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android中Gallery和ImageSwitcher的使用實例
- Android TV開發(fā):實現(xiàn)3D仿Gallery效果的實例代碼
- Android自定義Gallery控件實現(xiàn)3D圖片瀏覽器
- Android開發(fā)中畫廊視圖Gallery的兩種使用方法分析
- Android高級組件Gallery畫廊視圖使用方法詳解
- Android UI控件之Gallery實現(xiàn)拖動式圖片瀏覽效果
- Android 使用自定義RecyclerView控件實現(xiàn)Gallery效果
- Android開發(fā)實現(xiàn)Gallery畫廊效果的方法
- Android使用gallery和imageSwitch制作可左右循環(huán)滑動的圖片瀏覽器
- Android之Gallery使用例子
- Android使用Gallery實現(xiàn)照片拖動的特效
相關(guān)文章
android 6.0 權(quán)限授權(quán)方法
今天小編就為大家分享一篇android 6.0 權(quán)限授權(quán)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07詳解Android平臺JSON預(yù)覽(JSON-handle)
這篇文章主要介紹了Android平臺JSON預(yù)覽(JSON-handle),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09android中實現(xiàn)完全退出程序方法(退出所有activity)
這篇文章主要介紹了android中實現(xiàn)完全退出程序方法(退出所有activity),本文方法是博主個人使用的一個方法,據(jù)說效果非常好,需要的朋友可以參考下2015-05-05Android Root設(shè)備中的su權(quán)限獲取和使用詳解
本篇文章主要介紹了Android Root設(shè)備中的su權(quán)限獲取和使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView(附源碼)
這篇文章主要介紹了Android通過Handler與AsyncTask兩種方式動態(tài)更新ListView的方法,結(jié)合實例形式分析了ListView動態(tài)更新的常用技巧,并附上完整實例源碼供讀者下載,需要的朋友可以參考下2015-12-12