main.xml
<LinearLayout 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:orientation="vertical"
>
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="150dp"
/>
<ImageView
android:id="@+id/selectedImage"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:contentDescription="@string/app_name"
android:src="@drawable/img1"
/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import
android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
private ImageView selectedImage;
private Integer[] imageResIds = { R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6,
R.drawable.img7, R.drawable.img8, R.drawable.img9 };
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectedImage = (ImageView) findViewById(R.id.selectedImage);
gallery.setSpacing(1);
gallery.setAdapter(new GalleryImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// show the selected Image
selectedImage.setImageResource(imageResIds[position]);
}
});
}
}
GalleryImageAdapter.java
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
@SuppressWarnings("deprecation")
public class GalleryImageAdapter extends BaseAdapter {
private Context context;
private Integer[] imageResIds = { R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6,
R.drawable.img7, R.drawable.img8, R.drawable.img9 };
public GalleryImageAdapter(Context context) {
this.context = context;
}
public int getCount() {
return imageResIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int index, View view,
ViewGroup viewGroup) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageResIds[index]);
imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
}
Output :
No comments:
Post a Comment