具体步奏:
1、建立新项目ActivityBasicActivity
2、把图片拷贝到res/drawable目录
3、在res/values目录中新建一个attrs.xml内容
这里我是用Framelayout来实现叠加效果,使用ImageView来显示大图,Gallery来展示画廊,Toast类时用来显示图像名称
main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/p1"
/>
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="5dp"/>
</FrameLayout>
attrs.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground"></attr>
</declare-styleable>
</resources>
这部
Java代码:
package com.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.content.Context;
import android.content.res.TypedArray;
public class ActivityBasicActivity extends Activity {
///要显示的图像,图片数组
Integer[] images = {
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5
};
/** 当第一次被创建时调用 */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//创建一个线性布局管理器
setContentView(R.layout.main); //显示该视图
///定义UI组件
///final 如果修饰类,该类不能被继承;如果修饰变量,该变量不能被改变,就是不能再重新赋值;
///如果修饰方法,这个方法不能被重写。
final ImageView iv = (ImageView)findViewById(R.id.imageView1);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);///获取id传值给gallery
///设置图片匹配器
gallery.setAdapter(new ImageAdapter(this));
///设置AdapterView点击监听器,Gallery是AdapterView的子类
gallery.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
///显示点击的是第几张图片
Toast.makeText(getBaseContext(), "第"+(position+1)+"张", Toast.LENGTH_SHORT).show();
///设置背景部分的ImageView显示当前的Item的图片
iv.setImageResource(((ImageView)v).getId());
//iv.setImageResource(images[position]);
}
});
}
public class ImageAdapter extends BaseAdapter{
///Item的修饰背景
int myGalleryItemBackground;
///上下文对象
private Context myContext;
///构造方法
public ImageAdapter(Context c){
myContext = c;
读取styleable资源,设定样式
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
myGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
///返回相片数组的数量
public int getCount(){
return images.length;
}
///返回项目
public Object getItem(int position){
return position;
}
///返回项目id
public long getItemId(int position){
return position;
}
///返回视图
public View getView(int position, View convertView, ViewGroup parent){
ImageView iv = new ImageView(myContext);
iv.setImageResource(images[position]);
//给生成的ImageView设置id,不设置的话id都是-1
iv.setId(images[position]);
iv.setLayoutParams(new Gallery.LayoutParams(120,160));
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setBackgroundResource(myGalleryItemBackground);
return iv;
}
}
}
这里提供一个小扩展,AdapterView的继承关系