Guest User

Untitled

a guest
Jul 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. package no.rosok.julie.HelloGallery;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.res.TypedArray;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.BaseAdapter;
  13. import android.widget.Gallery;
  14. import android.widget.ImageView;
  15. import android.widget.Toast;
  16.  
  17. public class HelloGalleryActivity extends Activity {
  18. /** Called when the activity is first created. */
  19. @Override
  20. public void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23.  
  24. Gallery gallery = (Gallery) findViewById(R.id.gallery);
  25. gallery.setAdapter(new ImageAdapter(this));
  26.  
  27. gallery.setOnItemClickListener(new OnItemClickListener() {
  28. @Override
  29. public void onItemClick(AdapterView parent, View v, int position, long id) {
  30.  
  31. // Toast.makeText(HelloGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
  32. }
  33.  
  34. });
  35. }
  36. public class ImageAdapter extends BaseAdapter {
  37. int mGalleryItemBackground;
  38. private Context mContext;
  39.  
  40. private Integer[] mImageIds = {
  41. R.drawable.a,
  42. R.drawable.b,
  43. R.drawable.c,
  44. R.drawable.d,
  45. R.drawable.e,
  46. R.drawable.f,
  47. R.drawable.g,
  48. R.drawable.k,
  49. R.drawable.l,
  50. };
  51.  
  52. public ImageAdapter(Context c) {
  53. mContext = c;
  54. TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
  55. mGalleryItemBackground = attr.getResourceId(
  56. R.styleable.HelloGallery_android_galleryItemBackground, 0);
  57. attr.recycle();
  58. }
  59.  
  60. public int getCount() {
  61. return mImageIds.length;
  62. }
  63.  
  64. public Object getItem(int position) {
  65. return position;
  66. }
  67.  
  68. public long getItemId(int position) {
  69. return position;
  70. }
  71. @Override
  72. public View getView(int position, View convertView, ViewGroup parent) {
  73. ImageView imageView = new ImageView(mContext);
  74.  
  75. imageView.setImageResource(mImageIds[position]);
  76. imageView.setLayoutParams(new Gallery.LayoutParams(250, 200));
  77. imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  78. imageView.setBackgroundResource(mGalleryItemBackground);
  79.  
  80. return imageView;
  81. }
  82. }
  83.  
  84. }
Add Comment
Please, Sign In to add comment