1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.view; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.content.res.TypedArray; 24 import android.os.Bundle; 25 import android.view.ContextMenu; 26 import android.view.MenuItem; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.view.ContextMenu.ContextMenuInfo; 30 import android.widget.AdapterView; 31 import android.widget.BaseAdapter; 32 import android.widget.Gallery; 33 import android.widget.ImageView; 34 import android.widget.Toast; 35 import android.widget.AdapterView.AdapterContextMenuInfo; 36 import android.widget.AdapterView.OnItemClickListener; 37 38 public class Gallery1 extends Activity { 39 40 @Override onCreate(Bundle savedInstanceState)41 public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.gallery_1); 44 45 // Reference the Gallery view 46 Gallery g = (Gallery) findViewById(R.id.gallery); 47 // Set the adapter to our custom adapter (below) 48 g.setAdapter(new ImageAdapter(this)); 49 50 // Set a item click listener, and just Toast the clicked position 51 g.setOnItemClickListener(new OnItemClickListener() { 52 public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 53 Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show(); 54 } 55 }); 56 57 // We also want to show context menu for longpressed items in the gallery 58 registerForContextMenu(g); 59 } 60 61 @Override onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)62 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 63 menu.add(R.string.gallery_2_text); 64 } 65 66 @Override onContextItemSelected(MenuItem item)67 public boolean onContextItemSelected(MenuItem item) { 68 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 69 Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show(); 70 return true; 71 } 72 73 public class ImageAdapter extends BaseAdapter { 74 private static final int ITEM_WIDTH = 136; 75 private static final int ITEM_HEIGHT = 88; 76 77 private final int mGalleryItemBackground; 78 private final Context mContext; 79 80 private final Integer[] mImageIds = { 81 R.drawable.gallery_photo_1, 82 R.drawable.gallery_photo_2, 83 R.drawable.gallery_photo_3, 84 R.drawable.gallery_photo_4, 85 R.drawable.gallery_photo_5, 86 R.drawable.gallery_photo_6, 87 R.drawable.gallery_photo_7, 88 R.drawable.gallery_photo_8 89 }; 90 91 private final float mDensity; 92 ImageAdapter(Context c)93 public ImageAdapter(Context c) { 94 mContext = c; 95 // See res/values/attrs.xml for the <declare-styleable> that defines 96 // Gallery1. 97 TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 98 mGalleryItemBackground = a.getResourceId( 99 R.styleable.Gallery1_android_galleryItemBackground, 0); 100 a.recycle(); 101 102 mDensity = c.getResources().getDisplayMetrics().density; 103 } 104 getCount()105 public int getCount() { 106 return mImageIds.length; 107 } 108 getItem(int position)109 public Object getItem(int position) { 110 return position; 111 } 112 getItemId(int position)113 public long getItemId(int position) { 114 return position; 115 } 116 getView(int position, View convertView, ViewGroup parent)117 public View getView(int position, View convertView, ViewGroup parent) { 118 ImageView imageView; 119 if (convertView == null) { 120 convertView = new ImageView(mContext); 121 122 imageView = (ImageView) convertView; 123 imageView.setScaleType(ImageView.ScaleType.FIT_XY); 124 imageView.setLayoutParams(new Gallery.LayoutParams( 125 (int) (ITEM_WIDTH * mDensity + 0.5f), 126 (int) (ITEM_HEIGHT * mDensity + 0.5f))); 127 128 // The preferred Gallery item background 129 imageView.setBackgroundResource(mGalleryItemBackground); 130 } else { 131 imageView = (ImageView) convertView; 132 } 133 134 imageView.setImageResource(mImageIds[position]); 135 136 return imageView; 137 } 138 } 139 } 140