Android中的GalleryView实例演示
Android中的GalleryView实例演示
先看Android中的GalleryView实例演示效果:
滑动小图点击后上面的大图就和小图一致了
activity代码:
package com.tmacsky;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
public class GalleryActivity extends Activity implements ViewFactory {
int index = 0;
ImageSwitcher imageSwitcher;
int [] image = {R.drawable.ha,R.drawable.hb,R.drawable.hc,R.drawable.hd,R.drawable.he,R.drawable.hf,R.drawable.hg,R.drawable.hh};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
//设置imageswitch
imageSwitcher = (ImageSwitcher)findViewById(R.id.imageswitcher);
//设置一个容器
imageSwitcher.setFactory(this);
//给一个初始的值
imageSwitcher.setImageResource(image[index]);
//定义gallery
Gallery gallery = (Gallery)findViewById(R.id.gallery);
//设置adapter
gallery.setAdapter(new ImageAdapter());
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//设置滑动后弹出切换图片的数字
Toast.makeText(GalleryActivity.this, " " + arg2, 3000).show();
imageSwitcher.setImageResource(image[arg2]);
}
});
}
class ImageAdapter extends BaseAdapter{
public int getCount() {
// TODO Auto-generated method stub
return image.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return image[position];
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return image[position];
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(GalleryActivity.this);
imageView.setPadding(10, 50, 10, 5);
imageView.setImageResource(image[position]);
return imageView;
}
}
//设置工厂的时候,系统需要返回一个新的imageview,此处直接是默认的就可以了
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(this);
}
}
gallery.xml的文件代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageSwitcher
android:id="@+id/imageswitcher"
android:background="@drawable/hc"
android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ImageSwitcher>
<Gallery
android:layout_width="200px"
android:layout_height="wrap_content"
android:id="@+id/gallery"
android:layout_gravity="center_horizontal"
>
</Gallery>
</LinearLayout>
那啥图片资源什么的自己可以找自己喜欢的图,OK~
Android开发之简单图片浏览器 读取SDCard图片+形成缩略图+Gallery
Android Gallery 3D效果
评论暂时关闭