Android UI控件之Gallery实现拖动式图片浏览效果
作者:LCore 时间:2023-02-06 13:22:01
我们知道现在智能手机上都有这样一种功能,就是你在浏览图片的时候。不是硬性的点击按钮而是可以实现手指的拖动,划开效果。使用户具有更好的交互体验,不过这种效果是如何实现的呢?
在Android中是通过Gallery来实现拖动效果的。
通过Gallery可以实现各种各样的效果,此篇文章只是简要谈谈他的用法,至于后续的一些效果
有机会的时候做一个整理。
首先看看其简单实现吧!本次实例是通过选取图片实现类似设置背景的功能!
不过需要说明的是:图片不宜过大,否则容易内存溢出,android对大图片的支持不好!
我们来看看切换之后的效果吧
看看重新设置一幅背景图片!
怎么样一个简单的效果就出来了吧!
下面是具体的实现方法:
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));
// 设置gallery 的背景图片
gallery.setBackgroundResource(R.drawable.first);
// 设置Gallery的监听事件
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;
}
}
});
}
}
自己实现一个ImageAdapter继承与BaseAdapter实现适配器
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;
//定义一个数组,存放图片资源
private Integer[] mImageIds = {
R.drawable.first,
R.drawable.second,
R.drawable.third,
R.drawable.forth,
R.drawable.fifth,
};
//构造
public ImageAdapter(Context c){
mContext = c;
}
//获取图片的个数
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) {
//要取出图片,即要定义一个ImageView来存
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
//设置显示比例类型
//设置布局图片以105*150显示 (简单解释——设置数字不一样,图片的显示大小不一样)
imageView.setLayoutParams(new Gallery.LayoutParams(240, 200));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}
}
Over!一个简单的图片拖动展示外加设置背景图片的小功能就实现了。
下面是项目完整代码部分:Gallery实现拖动式图片浏览效果
来源:http://blog.csdn.net/lcore/article/details/8697421
标签:Android,UI,Gallery,拖动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot中Shiro缓存使用Redis、Ehcache的方法
2023-10-04 17:34:59
分享WCF聊天程序--WCFChat实现代码
2023-01-19 17:04:59
![](https://img.aspxhome.com/file/2023/8/83248_0s.jpg)
c语言颜色代码详解
2021-05-27 08:07:54
![](https://img.aspxhome.com/file/2023/0/125910_0s.png)
一文助你搞懂参数传递原理解析(java、go、python、c++)
2022-04-04 13:10:15
![](https://img.aspxhome.com/file/2023/5/130925_0s.jpg)
Android自定义View实现钟摆效果进度条PendulumView
2022-07-04 15:50:14
![](https://img.aspxhome.com/file/2023/1/126751_0s.gif)
Android实现倒计时效果
2021-10-13 03:25:29
![](https://img.aspxhome.com/file/2023/3/138643_0s.jpg)
Linux+Docker+SpringBoot+IDEA一键自动化部署的详细步骤
2023-08-12 15:25:39
![](https://img.aspxhome.com/file/2023/2/106182_0s.png)
关于Java中String类字符串的解析
2021-06-24 20:15:06
![](https://img.aspxhome.com/file/2023/3/91733_0s.png)
Android编程实现应用自动更新、下载、安装的方法
2021-11-15 11:21:39
Java实现按行读取大文件
2022-11-05 13:56:57
Unity通过脚本创建网格Mesh的方法
2023-02-26 23:38:00
![](https://img.aspxhome.com/file/2023/6/113706_0s.png)
Spring应用抛出NoUniqueBeanDefinitionException异常的解决方案
2023-11-25 07:36:19
Java实现导入导出Excel文件的方法(poi,jxl)
2021-09-13 21:12:35
SpringMVC @RequestBody自动转json Http415错误的解决
2022-09-12 13:12:34
![](https://img.aspxhome.com/file/2023/0/63070_0s.jpg)
Android简单实现一个颜色渐变的ProgressBar的方法
2023-07-23 00:30:12
![](https://img.aspxhome.com/file/2023/0/120110_0s.gif)
Android WebView开发之自定义WebView工具框
2023-01-12 13:41:43
![](https://img.aspxhome.com/file/2023/7/139327_0s.jpg)
C#使用SQL DataAdapter数据适配代码实例
2021-11-29 11:51:15
Java实战宠物医院预约挂号系统的实现流程
2022-12-28 03:07:52
![](https://img.aspxhome.com/file/2023/9/74989_0s.jpg)
Android实现简单的分批加载ListView
2023-10-28 14:49:21
Javaweb mybatis接口开发实现过程详解
2022-03-11 22:02:21