main.xml
<?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
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/grd"
></Gallery>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/im"
android:layout_gravity="center_horizontal"
android:src="@drawable/a1"
/>
</LinearLayout>
GalleryActivity.java
package com.ss.baka;
import android.app.Activity;
public class GalleryBakaActivity extends Activity {
/** Called when the activity is first created. */
Gallery grid;
ImageView img;
int arr[]={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,
R.drawable.a6,R.drawable.a7,R.drawable.a8,R.drawable.a9,R.drawable.a10,
R.drawable.a11,R.drawable.a12,R.drawable.a13};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grid=(Gallery)findViewById(R.id.grd);
img=(ImageView)findViewById(R.id.im);
grid.setSpacing(2);
grid.setAdapter(new GalleryAdapter(this));
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
img.setImageResource(arr[arg2]);
}
});
}
}
class GalleryAdapter extends BaseAdapter
{
Context con;
int arr1[]={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,
R.drawable.a6,R.drawable.a7,R.drawable.a8,R.drawable.a9,R.drawable.a10,
R.drawable.a11,R.drawable.a12,R.drawable.a13};
public GridAdapter(Context cont) {
// TODO Auto-generated constructor stub
con=cont;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr1.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i=new ImageView(con);
i.setImageResource(arr1[position]);
i.setLayoutParams(new Gallery.LayoutParams(200,200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
}
import android.app.Activity;
public class GalleryBakaActivity extends Activity {
/** Called when the activity is first created. */
Gallery grid;
ImageView img;
int arr[]={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,
R.drawable.a6,R.drawable.a7,R.drawable.a8,R.drawable.a9,R.drawable.a10,
R.drawable.a11,R.drawable.a12,R.drawable.a13};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
grid=(Gallery)findViewById(R.id.grd);
img=(ImageView)findViewById(R.id.im);
grid.setSpacing(2);
grid.setAdapter(new GalleryAdapter(this));
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
img.setImageResource(arr[arg2]);
}
});
}
}
class GalleryAdapter extends BaseAdapter
{
Context con;
int arr1[]={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,
R.drawable.a6,R.drawable.a7,R.drawable.a8,R.drawable.a9,R.drawable.a10,
R.drawable.a11,R.drawable.a12,R.drawable.a13};
public GridAdapter(Context cont) {
// TODO Auto-generated constructor stub
con=cont;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr1.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i=new ImageView(con);
i.setImageResource(arr1[position]);
i.setLayoutParams(new Gallery.LayoutParams(200,200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
}