Android UI控件之Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果
我們知道現(xiàn)在智能手機(jī)上都有這樣一種功能,就是你在瀏覽圖片的時(shí)候。不是硬性的點(diǎn)擊按鈕而是可以實(shí)現(xiàn)手指的拖動(dòng),劃開效果。使用戶具有更好的交互體驗(yàn),不過這種效果是如何實(shí)現(xiàn)的呢?
在Android中是通過Gallery來實(shí)現(xiàn)拖動(dòng)效果的。
通過Gallery可以實(shí)現(xiàn)各種各樣的效果,此篇文章只是簡要談?wù)勊挠梅?,至于后續(xù)的一些效果
有機(jī)會(huì)的時(shí)候做一個(gè)整理。
首先看看其簡單實(shí)現(xiàn)吧!本次實(shí)例是通過選取圖片實(shí)現(xiàn)類似設(shè)置背景的功能!
不過需要說明的是:圖片不宜過大,否則容易內(nèi)存溢出,android對大圖片的支持不好!

我們來看看切換之后的效果吧

看看重新設(shè)置一幅背景圖片!

怎么樣一個(gè)簡單的效果就出來了吧!
下面是具體的實(shí)現(xiàn)方法:
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/gallery1" android:layout_height="fill_parent" android:layout_width="fill_parent" android:spacing="3px" > </Gallery> </LinearLayout>
MainActivity文件:
package com.kiritor.ui_gallery;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;
/**
* @author 記憶的永恒
*
*/
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Gallery gallery = (Gallery) findViewById(R.id.gallery1);
// 將存放圖片的ImageAdapter給gallery對象
gallery.setAdapter(new ImageAdapter(this));
// 設(shè)置gallery 的背景圖片
gallery.setBackgroundResource(R.drawable.first);
// 設(shè)置Gallery的監(jiān)聽事件
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
switch (arg2) {
case 0:
gallery.setBackgroundResource(R.drawable.first);
break;
case 1:
gallery.setBackgroundResource(R.drawable.second);
break;
case 2:
gallery.setBackgroundResource(R.drawable.third);
break;
case 3:
gallery.setBackgroundResource(R.drawable.forth);
break;
case 4:
gallery.setBackgroundResource(R.drawable.fifth);
break;
default:
break;
}
}
});
}
}
自己實(shí)現(xiàn)一個(gè)ImageAdapter繼承與BaseAdapter實(shí)現(xiàn)適配器
package com.kiritor.ui_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 ImageAdapter extends BaseAdapter{
//定義Content
private Context mContext;
//定義一個(gè)數(shù)組,存放圖片資源
private Integer[] mImageIds = {
R.drawable.first,
R.drawable.second,
R.drawable.third,
R.drawable.forth,
R.drawable.fifth,
};
//構(gòu)造
public ImageAdapter(Context c){
mContext = c;
}
//獲取圖片的個(gè)數(shù)
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
//獲取圖片在庫中的位置
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
//獲取圖片在庫中的ID
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//將圖片取出來
public View getView(int position, View convertView, ViewGroup parent) {
//要取出圖片,即要定義一個(gè)ImageView來存
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
//設(shè)置顯示比例類型
//設(shè)置布局圖片以105*150顯示 (簡單解釋——設(shè)置數(shù)字不一樣,圖片的顯示大小不一樣)
imageView.setLayoutParams(new Gallery.LayoutParams(240, 200));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
Over!一個(gè)簡單的圖片拖動(dòng)展示外加設(shè)置背景圖片的小功能就實(shí)現(xiàn)了。
下面是項(xiàng)目完整代碼部分:Gallery實(shí)現(xiàn)拖動(dòng)式圖片瀏覽效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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 使用自定義RecyclerView控件實(shí)現(xiàn)Gallery效果
- Android開發(fā)實(shí)現(xiàn)Gallery畫廊效果的方法
- Android使用gallery和imageSwitch制作可左右循環(huán)滑動(dòng)的圖片瀏覽器
- Android之Gallery使用例子
- Android使用Gallery實(shí)現(xiàn)照片拖動(dòng)的特效
相關(guān)文章
Android TabLayout設(shè)置指示器寬度的方法
本篇文章主要介紹了Android TabLayout設(shè)置指示器寬度的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法
這篇文章主要介紹了Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題解決,需要的朋友可以參考下2017-10-10
關(guān)于Kotlin的自動(dòng)類型轉(zhuǎn)換詳解
這篇文章主要給大家介紹了關(guān)于Kotlin的自動(dòng)類型轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android最簡單的狀態(tài)切換布局實(shí)現(xiàn)教程
這篇文章主要給大家介紹了關(guān)于Android中最簡單的狀態(tài)切換布局的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
android TextView屬性的詳細(xì)介紹 分享
android TextView屬性的詳細(xì)介紹 分享,需要的朋友可以參考一下2013-05-05
Android OpenGL ES實(shí)現(xiàn)簡單綠幕摳圖
這篇文章主要為大家介紹了Android OpenGL ES實(shí)現(xiàn)簡單綠幕摳圖示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Java語言讀取配置文件config.properties的方法講解
今天小編就為大家分享一篇關(guān)于Java語言讀取配置文件config.properties的方法講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
Android中的webview監(jiān)聽每次URL變化實(shí)例
這篇文章主要介紹了Android中的webview監(jiān)聽每次URL變化實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

