Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package no.rosok.julie.HelloGallery;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.res.TypedArray;
- 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.ImageView;
- import android.widget.Toast;
- public class HelloGalleryActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Gallery gallery = (Gallery) findViewById(R.id.gallery);
- gallery.setAdapter(new ImageAdapter(this));
- gallery.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView parent, View v, int position, long id) {
- // Toast.makeText(HelloGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
- }
- });
- }
- public class ImageAdapter extends BaseAdapter {
- int mGalleryItemBackground;
- private Context mContext;
- private Integer[] mImageIds = {
- R.drawable.a,
- R.drawable.b,
- R.drawable.c,
- R.drawable.d,
- R.drawable.e,
- R.drawable.f,
- R.drawable.g,
- R.drawable.k,
- R.drawable.l,
- };
- public ImageAdapter(Context c) {
- mContext = c;
- TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
- mGalleryItemBackground = attr.getResourceId(
- R.styleable.HelloGallery_android_galleryItemBackground, 0);
- attr.recycle();
- }
- public int getCount() {
- return mImageIds.length;
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageView = new ImageView(mContext);
- imageView.setImageResource(mImageIds[position]);
- imageView.setLayoutParams(new Gallery.LayoutParams(250, 200));
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- imageView.setBackgroundResource(mGalleryItemBackground);
- return imageView;
- }
- }
- }
Add Comment
Please, Sign In to add comment