Android開發(fā)實(shí)現(xiàn)Gallery畫廊效果的方法
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)Gallery畫廊效果的方法。分享給大家供大家參考,具體如下:
畫廊 使用Gallery表示,按水平方向顯示內(nèi)容,并且可以用手指直接拖動(dòng)圖片移動(dòng),一般用來(lái)瀏覽圖片,被選中的選項(xiàng)位于中間,可以響應(yīng)事件顯示信息。
xml布局文件基本語(yǔ)法
<Gallery 屬性列表 />
Gallery支持4中xml屬性
屬性名稱
|
描述
|
|||||||||||||||||||||||||||||||||||||||
android:animationDuration
|
設(shè)置布局變化時(shí)動(dòng)畫的轉(zhuǎn)換所需的時(shí)間(毫秒級(jí))。僅在動(dòng)畫開始時(shí)計(jì)時(shí)。該值必須是整數(shù),比如:100。
|
|||||||||||||||||||||||||||||||||||||||
android:gravity
|
指定在對(duì)象的X和Y軸上如何放置內(nèi)容。指定一下常量中的一個(gè)或多個(gè)(使用 “|”分割)
|
|||||||||||||||||||||||||||||||||||||||
android:spacing
|
(譯者注:設(shè)置圖片之間的間距)
|
|||||||||||||||||||||||||||||||||||||||
android:unselectedAlpha
|
設(shè)置未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”
|
效果的具體實(shí)現(xiàn)過(guò)程
layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Gallery android:id="@+id/gallery" android:spacing="5px" //設(shè)置列表項(xiàng)之間的間距為5像素 android:unselectedAlpha="0.5" //設(shè)置未被選中的列表項(xiàng)的透明度 android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
Activity:
package xqx; import com.example.xqx_lianxi.R; import android.app.Activity; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; public class MainGallery extends Activity{ //設(shè)置畫廊圖片 private int[] imageId = new int[] { R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main_gallery); //獲取Gallery組件 Gallery gallery = (Gallery) findViewById(R.id.gallery); BaseAdapter adapter = new BaseAdapter() { //獲取當(dāng)前選項(xiàng)ID @Override public long getItemId(int position) { return position; } //獲取當(dāng)前選項(xiàng)值 @Override public Object getItem(int position) { return position; } //獲取數(shù)量 @Override public int getCount() { return imageId.length; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageview; //聲明ImageView的對(duì)象 if (convertView == null) { imageview = new ImageView(MainGallery.this); //創(chuàng)建ImageView的對(duì)象 imageview.setScaleType(ImageView.ScaleType.FIT_XY); //設(shè)置縮放方式 imageview.setLayoutParams(new Gallery.LayoutParams(500, 400)); TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery); imageview.setBackgroundResource(typedArray.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0)); imageview.setPadding(5, 0, 5, 0); //設(shè)置imageview的內(nèi)邊距 } else { imageview = (ImageView) convertView; } imageview.setImageResource(imageId[position]); return imageview; } }; //將適配器與Gallery關(guān)聯(lián) gallery.setAdapter(adapter); gallery.setSelection(imageId.length / 2); //默認(rèn)顯示的圖片的id //畫廊圖片的點(diǎn)擊事件 gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainGallery.this, "第" + String.valueOf(position+1) + "張圖片被選中", Toast.LENGTH_SHORT).show(); } }); } }
最后在res/values/string.xml中添加一段代碼 ,這里對(duì)應(yīng)activity中的51行
<declare-styleable name="Gallery"> <attr name="android:galleryItemBackground" /> </declare-styleable>
這樣便完成了一個(gè)畫廊的效果
效果圖:
可以看到 一共有6張圖片 默認(rèn)顯示第4張
gallery.setSelection(imageId.length / 2); //默認(rèn)顯示的圖片的id
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)漂亮的Gallery畫廊
- Android中Gallery和ImageSwitcher的使用實(shí)例
- Android TV開發(fā):實(shí)現(xiàn)3D仿Gallery效果的實(shí)例代碼
- Android自定義Gallery控件實(shí)現(xiàn)3D圖片瀏覽器
- Android開發(fā)中畫廊視圖Gallery的兩種使用方法分析
- Android高級(jí)組件Gallery畫廊視圖使用方法詳解
- Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果
- Android 使用自定義RecyclerView控件實(shí)現(xiàn)Gallery效果
- Android使用gallery和imageSwitch制作可左右循環(huán)滑動(dòng)的圖片瀏覽器
- Android之Gallery使用例子
- Android使用Gallery實(shí)現(xiàn)照片拖動(dòng)的特效
相關(guān)文章
Android開發(fā)之permission動(dòng)態(tài)權(quán)限獲取詳解
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之permission動(dòng)態(tài)權(quán)限獲取,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Android中通過(guò)view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法
這篇文章主要介紹了Android中通過(guò)view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法,本文方法相對(duì)簡(jiǎn)單,容易理解,需要的朋友可以參考下2014-09-09Android高仿抖音照片電影功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android高仿抖音照片電影功能的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09Android仿微博個(gè)人詳情頁(yè)滾動(dòng)到頂部的實(shí)例代碼
這篇文章主要介紹了Android仿微博個(gè)人詳情頁(yè)滾動(dòng)到頂部的實(shí)例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒家,需要的朋友可以參考下2019-05-05Rocksdb?Memtable數(shù)據(jù)結(jié)構(gòu)源碼解析
這篇文章主要為大家介紹了Rocksdb?Memtable數(shù)據(jù)結(jié)構(gòu)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android ViewPager向?qū)ы?yè)面制作方法
這篇文章主要為大家詳細(xì)介紹了Android ViewPager向?qū)ы?yè)面制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)現(xiàn)QQ側(cè)滑(刪除、置頂?shù)?功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)QQ側(cè)滑刪除、置頂?shù)裙δ埽哂幸欢ǖ膮⒖純r(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android在項(xiàng)目中接入騰訊TBS瀏覽器WebView的教程與注意的地方
今天小編就為大家分享一篇關(guān)于Android在項(xiàng)目中接入騰訊TBS瀏覽器WebView的教程與注意的地方,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10