效果如下图:
1.首先设计man.xml这里使用HorizontalScrollView结合GridView实现Gallery的左右滚动效果,并且解决了Gallery从中间开始的问题,GridView可以从最左边开始显示。代码如下:
<?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 view 图片效果图 -->
<HorizontalScrollView
android:id="@+id/galleryScroll"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:scrollbars="none"
android:focusable="false"
android:layout_alignParentBottom="true"
android:background="@drawable/gallerybackground"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
>
<!--注意这里 宽度一定要设置不然不能达到效果 -->
<LinearLayout
android:layout_width="770dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:focusable="false"
>
<GridView android:id="@+id/gallery"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:horizontalSpacing="1.0dip"
android:verticalSpacing="1.0dip"
android:stretchMode="spacingWidthUniform"
android:numColumns="auto_fit"
android:columnWidth="70dip"
android:focusable="false"
>
</GridView>
</LinearLayout>
</FrameLayout>
</HorizontalScrollView>
</LinearLayout>
2.图片列表适配的实现,如下:
package com.src;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter{
private static String[] effectTitle = {"原图", "柔光", "LOMO", "复古", "古典", "黑白",
"淡雅", "哥特风", "暖化", "水印", "印象"};
private static int[] effectDrawable = {R.drawable.lomoeffect_01, R.drawable.lomoeffect_02,
R.drawable.lomoeffect_03, R.drawable.lomoeffect_04,
R.drawable.lomoeffect_05, R.drawable.lomoeffect_06,
R.drawable.lomoeffect_07, R.drawable.lomoeffect_08,
R.drawable.lomoeffect_09, R.drawable.lomoeffect_10,
R.drawable.lomoeffect_11};
private ArrayList<GalleryInfo> list;
private LayoutInflater inflater;
public ImageAdapter(Context context) {
super();
list = new ArrayList<GalleryInfo>();
inflater = LayoutInflater.from(context);
// 初始化数据
for (int i=0; i<effectDrawable.length; i++) {
GalleryInfo info = new GalleryInfo();
info.title = effectTitle[i];
info.drawable = effectDrawable[i];
if (i ==0 ) {
//默认第一项问选中
info.isSelect = true;
} else {
info.isSelect = false;
}
list.add(info);
}
}
public int getCount() {
return effectTitle.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.gallery_item, null);
holder = new ViewHolder();
holder.effectTitle = (TextView)convertView.findViewById(R.id.effectTitle);
holder.effectDrawable = (ImageView)convertView.findViewById(R.id.effectDrawable);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.effectTitle.setText(list.get(position).title);
holder.effectDrawable.setImageResource(list.get(position).drawable);
if (list.get(position).isSelect) {//被选中的选项加选中的背景框
holder.effectDrawable.setBackgroundResource(R.drawable.gallery_select);
}else {//未被选中的选项设置背景透明
holder.effectDrawable.setBackgroundDrawable(null);
}
// notifyDataSetChanged();
return convertView;
}
public void changeStatus(int select) {
for (int i=0; i<list.size(); i++) {
list.get(i).isSelect = false;
}
list.get(select).isSelect = true;
}
private class ViewHolder {
TextView effectTitle;
ImageView effectDrawable;
}
class GalleryInfo {
public String title;
public int drawable;
public boolean isSelect;
}
}
3.测试实例效果,代码如下:
package com.src;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class GrideToGalleryActivity extends Activity {
private GridView gallery;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageAdapter adapter = new ImageAdapter(this);
gallery = (GridView)findViewById(R.id.gallery);
gallery.setAdapter(adapter);
gallery.setNumColumns(11);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int select,
long arg3) {
adapter.changeStatus(select);//设置选中项
adapter.notifyDataSetChanged();//更新列表
}
});
gallery.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.clearFocus();
return false;
}
});
}
}
源代码附件
- 大小: 163.8 KB
分享到:
相关推荐
用GridView实现的Gallery效果,由于Gallery实现起来效果不好,所以用GridView实现了,有点击选中效果
Android 滑动效果 Gallery + GridView
gridView实现gallery效果防止gallery滑动过快效果
android 使用GridView实现Gallery的效果,并且从最左边开始
由于gallery控件的最左边和最右边都会留空,如果我们要把空白去掉,直接用自定义gallery实现很困难,所以我gridView实现gallery的功能,实现很简单,有兴趣的同学不妨看看。
Android GridView,Gallery动态更新数据(整理自网络)
android 自定义gridView 按日期显示图片和视频 galleryDemo android 自定义gridView 按日期显示图片和视频 galleryDemo android 自定义gridView 按日期显示图片和视频 galleryDemo 免费下载 给个好评!
android后台资源的获取与展示,一个简单的相册。使用到了GridView、Gallery
在数据源适配器知识点上面,我们可以自己定义一个数据源适配器,要继承BaseAdapter,实现getView的方法 画廊控件:gallery imageswitch HorizontalScrollView,在一个屏幕上显示出超过屏幕尺寸的内容
Demo介绍:首页是一个GridView加载图片,竖屏...点击某一张图片,会链接到Gallery页面,由于Android自带的Gallery控件滑动效果很不好(滑动一次会加载好多张图片),这里对Gallery进行了扩展,滑动一次只加载一张图片。
本文实例讲述了Android编程滑动效果之Gallery+GridView实现图片预览功能。分享给大家供大家参考,具体如下: Android系统自带一个GridView和Gallery两个控件,GridView网格显示,Gallery单个浏览,两者结合起来可以...
An Android GridView that can be configured to scroll horizontally or vertically. I should have posted this over a year and a half ago, but never got around to it. I needed a grid view that in portrait...
android listview gridview gallery 等的综合demo
android 2.2 platform eclipse project source code,include more than 30 animation effect by java code and xml,slide by one item and loop gallery and dragable gridview.
048-集-GridView和Gallery视频教程,很是不错哦,有兴趣的可以学习下。
具体说明可以参考博客:我的Android进阶之旅------>Android之Gallery和GridView两种方式与ImageSwitcher实现带预览的和幻灯片方式的两种图片浏览器 博客地址:...
带水平滚动的gridveiw 重载HorizontalScrollView实现类Gallery功能 ViewPage支持图片缩放 支持网络图片显示
RecylerView常见用法,包含了: 系统提供了几种LayoutManager的使用;...横向ListView的功能,参考Android 自定义RecyclerView 实现真正的Gallery效果 横向ScrollView的功能 瀑布流效果 便于添加Item增加和移除动画
android图片特效,图片过滤 首先实选择列表从最左边开始排序并且默认选中第一项,这里为了实现从最左边开始所以使用HorizontalScrollView结合GridView实现Gallery view 图片效果图