使用Gallery滑动查看图片
来源:互联网 发布:java http请求工具类 编辑:程序博客网 时间:2024/05/11 14:31
效果图:
包含Gallery和Indicator的布局文件:
为了给Gallery动态绑定从服务器加载的图片,这里使用了适配器:
在适配其中,使用了子布局文件screenshot_item:
在这里默认显示一张实现规定的图片,当从服务器读取图片后,才动态显示来自服务器的图片,最后给Gallery添加适配器:
上图中使用TextView来显示相关软件的名字,例子中的名字叫YouMi短信,显然这个值是其他的类传递过来的。这篇帖子中省略了对该组件的介绍。中间显示图片的组件是Gallery,需要一个适配器,其中包含了异步加载网络图片的功能,若加载失败则显示默认图片,实际上每一个可以滑动的卡片,都是由一个ImageView组合而成,因此需要单独定义一个ImageView样式文件。
这里自定义了一个Indicator,用于显示上图中最底部的导航标识,它是通过在Gallery的监听器中显示两张不同的图片来实现图示效果的。mScreenshotIndicators就是这个Indicator,它继承的是一个LineaLayout。在Gallery的监听器OnItemSelectedListener中需要调用Indicator的方法来设置需要显示的对应的导航标识。方法如下:mScreenshotIndicators.setHightlightIndicator(arg2);该方法也是自定义的方法,详见ScreenshotIndicatorsView类
初始化Gallery和Indicator并做监听:
mScreenshotIndicators = (ScreenshotIndicatorsView) findViewById(R.id.screenshot_indicators);mGallery = (Gallery) findViewById(R.id.screenshot_gallery);mGallery.setOnItemSelectedListener(new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3){MyLog.d(TAG, "====== onItemSelected ====");mScreenshotIndicators.setHightlightIndicator(arg2);}@Overridepublic void onNothingSelected(AdapterView<?> arg0){// Do nothing}});自定义上面代码中用到的Indicator:
public class ScreenshotIndicatorsView extends LinearLayout {private int mIndicatorCount;public ScreenshotIndicatorsView(Context context, AttributeSet attrs) {super(context, attrs);}public void setIndicatorCount(int c) {mIndicatorCount = c;Context ctx = getContext();for (int i = 0; i < c; i++) {ImageView v = new ImageView(ctx);v.setImageResource(R.drawable.m3);addView(v);}}public void setHightlightIndicator(int i) {for (int j = 0; j < mIndicatorCount; j++) {ImageView v = (ImageView)getChildAt(j);v.setImageResource((j == i) ? R.drawable.m2 : R.drawable.m3);//通过切换图片达到指示器的效果}}}
包含Gallery和Indicator的布局文件:
<FrameLayout android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="#000000" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <Gallery android:id="@+id/screenshot_gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="13dip" android:background="@drawable/gallery_bg" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="57dip" android:layout_gravity="bottom" > <com.market.hjapp.ui.view.ScreenshotIndicatorsView android:id="@+id/screenshot_indicators" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="horizontal" /> </RelativeLayout> </LinearLayout> </FrameLayout>
为了给Gallery动态绑定从服务器加载的图片,这里使用了适配器:
private class ScreenshotAdapter extends BaseAdapter{private ArrayList<String> mData = new ArrayList<String>();@Overridepublic int getCount(){return mData.size();}@Overridepublic Object getItem(int position){return position;}@Overridepublic long getItemId(int position){return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent){View screenshot;MyLog.d(TAG, "=================================getView" + position);if (position < ssItemView.size()&& ssItemView.get(position) != null)return ssItemView.get(position);if (convertView != null){screenshot = convertView;} else{screenshot = mInflater.inflate(R.layout.screenshot_item, null);}ImageView image = (ImageView) screenshot.findViewById(R.id.img);String imageUrl = mData.get(position);String imagePath = DatabaseUtils.getLocalPathFromUrl(imageUrl);MyLog.d(TAG, "=================================position:"+ position);MyLog.d(TAG, "=================================convertView:"+ convertView);MyLog.d(TAG, "=================================image:" + image);if (GeneralUtil.needDisplayImg(ViewScreenshotsActivity.this)){// if (imagePath!= null && !imagePath.equals("") && new// File(imagePath).exists()) {// image.setImageBitmap(DatabaseUtils.getImage(imagePath));// } else {image.setImageResource(R.drawable.default_screenshot);ImageLoader.getInstance().loadBitmapOnThread(mData.get(position), ViewScreenshotsActivity.this,image);// }} elseimage.setImageResource(R.drawable.default_screenshot);ssItemView.add(screenshot);return screenshot;}public void setData(ArrayList<String> data){mData = data;}}
在适配其中,使用了子布局文件screenshot_item:
<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:paddingLeft="21dip"android:paddingRight="21dip"><ImageViewandroid:id="@+id/img"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_margin="20dip"android:scaleType="fitXY"android:src="@drawable/default_screenshot" /></FrameLayout>
在这里默认显示一张实现规定的图片,当从服务器读取图片后,才动态显示来自服务器的图片,最后给Gallery添加适配器:
private ScreenshotAdapter mAdapter = new ScreenshotAdapter();mAdapter.setData(urlList);此时的adapter还不具有数据,这里需要调用
mAdapter.setData(urlList);方法进行赋值,这样才能让Adapter给Gallery绑定数据,但是具体urlList里的数据如何获取呢?这个要是情况而定,我这里不在详细展开。
- 使用Gallery滑动查看图片
- 使用Gallery查看图片
- android Gallery滑动图片
- Android Gallery图片一张张滑动
- Android Gallery图片一张张滑动
- gallery一次滑动一张图片
- Gallery实现横向滑动图片
- Android遍历某个文件夹的图片并实现滑动查看的的Gallery
- (转)Android 模仿淘宝滑动查看图片的效果 Gallery + ImageSwitcher
- android Gallery 仿iPhone 图片滑动
- 用Gallery和ImageView实现图片滑动
- android Gallery 仿iPhone 图片滑动
- Android图片之滑动效果(Gallery)
- Gallery-Dome图片滑动效果
- 王立平--Gallery:实现图片的左右滑动
- Gallery实现图片文字左右滑动
- BaseAdapter应用 和gallery滑动图片
- Android的滑动图片Gallery弃用
- CMS开源代码
- Android SD卡文件浏览器技术分析
- android linux 基础知识总结
- 《计算机图形学(OpenGL版)第3版》
- XCode 的调试定位技巧, 使用Xcode 4编译器设定Release/Debug教程
- 使用Gallery滑动查看图片
- MFC 设置对话框位置
- 联合体相关知识点
- SAP HANA 表的连接类型(SAP HANA join type)
- ORA-600 [2662] Block SCN is ahead of Current SCN 处理方法 说明
- 关于混淆找不到字段的处理
- 如何在IDA中找到MFC程序的消息处理函数
- Mysql修改设置root密码的命令及方法
- 菜鸟日记——在myeclipse中配置weka