ImageSwitcher配合Gallery的简单使用
来源:互联网 发布:多视角目标检测算法 编辑:程序博客网 时间:2024/05/23 16:58
ImageSwitcher:图片切换
Gallery:画廊,开发中也可用于做滑动的菜单
这样配合使用也可以用作图片选择器,下面我们就来看看是怎么实现的吧
1.效果图
2.配置布局文件 activity_gallery.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GalleryActivity" >
<ImageSwitcher
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:spacing="10dp"/>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GalleryActivity" >
<ImageSwitcher
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:spacing="10dp"/>
</RelativeLayout>
布局文件imageview.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>
显示图片布局 item.xml
<?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" >
<ImageView
android:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="80dp"
android:scaleType="fitXY"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="80dp"
android:scaleType="fitXY"
/>
</LinearLayout>
主要实现代码GalleryActivity.jvav
package com.seaares.gallery;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class GalleryActivity extends Activity {
private Gallery gallery;
private ImageSwitcher imagesw;
// 显示图片的资源
private static int[] images = { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e, R.drawable.f };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
gallery = (Gallery) findViewById(R.id.gallery);
imagesw = (ImageSwitcher) findViewById(R.id.image);
gallery.setAdapter(new GalleryAdapter(this));
// 选中gallery中图片的监听
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
imagesw.setImageResource(images[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
imagesw.setFactory(new ImageFactory());
}
public class GalleryAdapter extends BaseAdapter {
private Context context;
public GalleryAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return images[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item, null);
}
ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
iv.setImageResource(images[position]);
return convertView;
}
}
@SuppressWarnings("unused")
private class ImageFactory implements ViewFactory {
public View makeView() {
return getLayoutInflater().inflate(R.layout.imageview, null);
}
}
}
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class GalleryActivity extends Activity {
private Gallery gallery;
private ImageSwitcher imagesw;
// 显示图片的资源
private static int[] images = { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e, R.drawable.f };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
gallery = (Gallery) findViewById(R.id.gallery);
imagesw = (ImageSwitcher) findViewById(R.id.image);
gallery.setAdapter(new GalleryAdapter(this));
// 选中gallery中图片的监听
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
imagesw.setImageResource(images[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
imagesw.setFactory(new ImageFactory());
}
public class GalleryAdapter extends BaseAdapter {
private Context context;
public GalleryAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return images[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item, null);
}
ImageView iv = (ImageView) convertView.findViewById(R.id.iv);
iv.setImageResource(images[position]);
return convertView;
}
}
@SuppressWarnings("unused")
private class ImageFactory implements ViewFactory {
public View makeView() {
return getLayoutInflater().inflate(R.layout.imageview, null);
}
}
}
0 0
- ImageSwitcher配合Gallery的简单使用
- ImageSwitcher的使用(一):与Gallery配合使用
- ImageSwitcher 和 Gallery 的使用
- ImageSwitcher和Gallery的使用
- Gallery + ImageSwitcher 实现简单的电子相册
- Gallery与ImageSwitcher结合的简单实现
- Gallery与ImageSwitcher的综合使用
- Gallery 与ImageSwitcher的结合使用
- Gallery 与ImageSwitcher的结合使用
- Android中Gallery和ImageSwitcher的使用
- Android中Gallery和ImageSwitcher的使用
- gallery ,imageSwitcher 结合使用
- 简单图片浏览器(ImageSwitcher和Gallery结合使用)
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本1
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本2
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本2
- android之查看图片的程序 ImageSwitcher Gallery的配合应用---版本1
- android中画廊视图Gallery和ImageSwitcher组件的使用
- 23.1解析函数的级数表示(二)
- grep用法详解:grep与正则表达式(转)
- 忘记MySQL root密码解决方法(基于Ubuntu 12.04 LTS)
- ping命令运行过程及返回信息分析
- 横竖屏切换android:configChanges的使用
- ImageSwitcher配合Gallery的简单使用
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(2)----QUAD_COMMAND
- 结构体位制详解 -- C
- 安装并使用tomcat试验Android网络程序
- eclise工程导入AS
- 修改Ubuntu12.04启动菜单,图形界面软件grub-customizer
- FragmentPagerAdapter与FragmentStatePagerAdapter差异
- SQL数据库Sqlite3函数
- Scanner类的用法