Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 8.09 KB  |  hits: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android Coverflow OnClickListener
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Canvas;
  8. import android.graphics.LinearGradient;
  9. import android.graphics.Matrix;
  10. import android.graphics.Paint;
  11. import android.graphics.PorterDuffXfermode;
  12. import android.graphics.Typeface;
  13. import android.graphics.Bitmap.Config;
  14. import android.graphics.PorterDuff.Mode;
  15. import android.graphics.Shader.TileMode;
  16. import android.graphics.drawable.BitmapDrawable;
  17. import android.os.Bundle;
  18. import android.view.View;
  19. import android.view.ViewGroup;
  20. import android.widget.BaseAdapter;
  21. import android.widget.Button;
  22. import android.widget.ImageView;
  23. import android.widget.TextView;
  24. import android.widget.ImageView.ScaleType;
  25.  
  26. public class MainActivity extends Activity {
  27.     /** Called when the activity is first created. */
  28.     @Override
  29.     public void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         CoverFlow coverFlow;
  32.         coverFlow = new CoverFlow(this);
  33.  
  34.         coverFlow.setAdapter(new ImageAdapter(this));
  35.  
  36.         ImageAdapter coverImageAdapter =  new ImageAdapter(this);
  37.  
  38.         //coverImageAdapter.createReflectedImages();
  39.  
  40.         coverFlow.setAdapter(coverImageAdapter);
  41.  
  42.         coverFlow.setSpacing(-15);
  43.         coverFlow.setSelection(4, true);
  44.         coverFlow.setAnimationDuration(1000);
  45.  
  46.  
  47.         setContentView(coverFlow);
  48.  
  49.  
  50.         //Use this if you want to use XML layout file
  51.         //setContentView(R.layout.main);
  52.         //coverFlow =  (CoverFlow) findViewById(R.id.coverflow);
  53.         setContentView(R.layout.main);
  54.  
  55.         Button main1 = (Button) findViewById(R.id.button1);
  56.         main1.setOnClickListener(new View.OnClickListener() {
  57.             public void onClick(View view) {
  58.                  // fire intent
  59.                 finish(); // finish current activity
  60.                 Intent myIntent = new Intent(view.getContext(),
  61.                         MainActivity.class);
  62.                 startActivityForResult(myIntent, 0);
  63.  
  64.             }
  65.  
  66.  
  67.         });
  68.  
  69.         Button main2 = (Button) findViewById(R.id.button2);
  70.         main2.setOnClickListener(new View.OnClickListener() {
  71.             public void onClick(View view) {
  72.                  // fire intent
  73.                 finish(); // finish current activity
  74.                 Intent myIntent = new Intent(view.getContext(),
  75.                         MainbActivity.class);
  76.                 startActivityForResult(myIntent, 0);
  77.  
  78.             }
  79.  
  80.  
  81.         });
  82.  
  83.         coverFlow =  (CoverFlow) findViewById(R.id.coverflow);
  84.         coverFlow.setAdapter(coverImageAdapter);
  85.  
  86.  
  87.     }
  88.  
  89.     public class ImageAdapter extends BaseAdapter {
  90.          int mGalleryItemBackground;
  91.          private Context mContext;
  92.  
  93.          private FileInputStream fis;
  94.  
  95.          private Integer[] mImageIds = {
  96.            R.drawable.tabletsplash,
  97.                  R.drawable.tabletsplash2,
  98.                  R.drawable.tabletsplash3,
  99.                  R.drawable.tabletsplash4,
  100.                  R.drawable.tabletsplash5,
  101.                  R.drawable.tabletsplash,
  102.                  R.drawable.tabletsplash2,
  103.                  R.drawable.tabletsplash3,
  104.                  R.drawable.tabletsplash4,
  105.                  R.drawable.tabletsplash5,
  106.          };
  107.  
  108.          private ImageView[] mImages;
  109.  
  110.          public ImageAdapter(Context c) {
  111.           mContext = c;
  112.           mImages = new ImageView[mImageIds.length];
  113.          }
  114.       public boolean createReflectedImages() {
  115.               //The gap we want between the reflection and the original image
  116.               final int reflectionGap = 4;
  117.  
  118.  
  119.               int index = 0;
  120.               for (int imageId : mImageIds) {
  121.             Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
  122.               imageId);
  123.                int width = originalImage.getWidth();
  124.                int height = originalImage.getHeight();
  125.  
  126.  
  127.                //This will not scale but will flip on the Y axis
  128.                Matrix matrix = new Matrix();
  129.                matrix.preScale(1, -1);
  130.  
  131.                //Create a Bitmap with the flip matrix applied to it.
  132.                //We only want the bottom half of the image
  133.                Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);
  134.  
  135.  
  136.                //Create a new bitmap with same width but taller to fit reflection
  137.                Bitmap bitmapWithReflection = Bitmap.createBitmap(width
  138.                  , (height + height/2), Config.ARGB_8888);
  139.  
  140.               //Create a new Canvas with the bitmap that's big enough for
  141.               //the image plus gap plus reflection
  142.               Canvas canvas = new Canvas(bitmapWithReflection);
  143.               //Draw in the original image
  144.               canvas.drawBitmap(originalImage, 0, 0, null);
  145.               //Draw in the gap
  146.               Paint deafaultPaint = new Paint();
  147.               canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
  148.               //Draw in the reflection
  149.               canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);
  150.  
  151.               //Create a shader that is a linear gradient that covers the reflection
  152.               Paint paint = new Paint();
  153.               LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
  154.                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
  155.                 TileMode.CLAMP);
  156.               //Set the paint to use this shader (linear gradient)
  157.               paint.setShader(shader);
  158.               //Set the Transfer mode to be porter duff and destination in
  159.               paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  160.               //Draw a rectangle using the paint with our linear gradient
  161.               canvas.drawRect(0, height, width,
  162.                 bitmapWithReflection.getHeight() + reflectionGap, paint);
  163.  
  164.               ImageView imageView = new ImageView(mContext);
  165.               imageView.setImageBitmap(bitmapWithReflection);
  166.               imageView.setLayoutParams(new CoverFlow.LayoutParams(120, 180));
  167.               imageView.setScaleType(ScaleType.MATRIX);
  168.               mImages[index++] = imageView;
  169.  
  170.               }
  171.            return true;
  172.       }
  173.  
  174.          public int getCount() {
  175.              return mImageIds.length;
  176.          }
  177.  
  178.          public Object getItem(int position) {
  179.              return position;
  180.          }
  181.  
  182.          public long getItemId(int position) {
  183.              return position;
  184.          }
  185.  
  186.          public View getView(int position, View convertView, ViewGroup parent) {
  187.  
  188.           //Use this code if you want to load from resources
  189.              ImageView i = new ImageView(mContext);
  190.              i.setImageResource(mImageIds[position]);
  191.              i.setLayoutParams(new CoverFlow.LayoutParams(300, 900));
  192.              i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
  193.  
  194.              //Make sure we set anti-aliasing otherwise we get jaggies
  195.              BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
  196.              drawable.setAntiAlias(true);
  197.              return i;
  198.  
  199.           //return mImages[position];
  200.          }
  201.        /** Returns the size (0.0f to 1.0f) of the views
  202.           * depending on the 'offset' to the center. */
  203.           public float getScale(boolean focused, int offset) {
  204.             /* Formula: 1 / (2 ^ offset) */
  205.               return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
  206.           }
  207.  
  208.  
  209.  
  210.      }
  211.  
  212.     }
  213.        
  214. coverFlow.setOnItemClickListener(new OnItemClickListener(){
  215.   @Override
  216.   public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
  217.     // Do something }
  218.   }
  219. );
  220.        
  221. if (arg2 == 0) {
  222.     startActivityForResult(new Intent(getApplicationContext(), ActivityA.class), 0);
  223. } else if (arg2 == 1) {
  224.     startActivityForResult(new Intent(getApplicationContext(), ActivityB.class), 0);
  225. }
  226.        
  227. public View getView(int position, View convertView, ViewGroup parent) {
  228.  
  229.           //Use this code if you want to load from resources
  230.              ImageView i = new ImageView(mContext);
  231.             i.setOnClickListener(new OnClickListener(){
  232.             public void onClick(View v) {
  233.             }....