Android实现CoverFlow效果三

ImageAdapter适配器:

001 package com.coverflow; 
package com.coverflow; 

002   

003 import android.content.Context; 

004 import android.graphics.Bitmap; 

005 import android.graphics.BitmapFactory; 

006 import android.graphics.Canvas; 

007 import android.graphics.LinearGradient; 

008 import android.graphics.Matrix; 

009 import android.graphics.Paint; 

010 import android.graphics.PorterDuffXfermode; 

011 import android.graphics.Bitmap.Config; 

012 import android.graphics.PorterDuff.Mode; 

013 import android.graphics.Shader.TileMode; 

014 import android.graphics.drawable.BitmapDrawable; 

015 import android.view.View; 

016 import android.view.ViewGroup; 

017 import android.widget.BaseAdapter; 

018 import android.widget.ImageView; 

019   

020 import com.gallery.R; 

021   

022 public class ImageAdapter extends BaseAdapter { 

023     int mGalleryItemBackground; 

024     private Context mContext; 

025     private Integer[] mImageIds = {  

026             R.drawable.a1, 

027             R.drawable.a2,  

028             R.drawable.a3, 

029             R.drawable.a4,  

030             R.drawable.a5 }; 

031   

032     public ImageAdapter(Context c) { 

033         mContext = c; 

034     } 

035   

036     public int getCount() { 

037         return mImageIds.length; 

038     } 

039   

040     public Object getItem(int position) { 

041         return position; 

042     } 

043   

044     public long getItemId(int position) { 

045         return position; 

046     } 

047   

048     public View getView(int position, View convertView, ViewGroup parent) { 

049   

050         ImageView i = createReflectedImages(mContext,mImageIds[position]); 

051           

052         i.setLayoutParams(new CoverFlow.LayoutParams(120, 100)); 

053         i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

054           

055         // 设置的抗锯齿 

056         BitmapDrawable drawable = (BitmapDrawable) i.getDrawable(); 

057         drawable.setAntiAlias(true); 

058         return i; 

059     } 

060   

061     public float getScale(boolean focused, int offset) { 

062         return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); 

063     } 

064       

065     public ImageView createReflectedImages(Context mContext,int imageId) { 

066   

067         Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId); 

068           

069         final int reflectionGap = 4; 

070           

071         int width = originalImage.getWidth(); 

072         int height = originalImage.getHeight(); 

073   

074         Matrix matrix = new Matrix(); 

075         matrix.preScale(1, -1); 

076   

077         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 

078                 height / 2, width, height / 2, matrix, false); 

079   

080         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, 

081                 (height + height / 2), Config.ARGB_8888); 

082   

083         Canvas canvas = new Canvas(bitmapWithReflection); 

084   

085         canvas.drawBitmap(originalImage, 0, 0, null); 

086   

087         Paint deafaultPaint = new Paint(); 

088         canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint); 

089   

090         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 

091   

092         Paint paint = new Paint(); 

093         LinearGradient shader = new LinearGradient(0, originalImage 

094                 .getHeight(), 0, bitmapWithReflection.getHeight() 

095                 + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); 

096   

097         paint.setShader(shader); 

098   

099         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 

100   

101         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 

102                 + reflectionGap, paint); 

103   

104         ImageView imageView = new ImageView(mContext); 

105         imageView.setImageBitmap(bitmapWithReflection); 

106   

107         return imageView; 

108     } 

109       

110 }

002   

003 import android.content.Context; 

004 import android.graphics.Bitmap; 

005 import android.graphics.BitmapFactory; 

006 import android.graphics.Canvas; 

007 import android.graphics.LinearGradient; 

008 import android.graphics.Matrix; 

009 import android.graphics.Paint; 

010 import android.graphics.PorterDuffXfermode; 

011 import android.graphics.Bitmap.Config; 

012 import android.graphics.PorterDuff.Mode; 

013 import android.graphics.Shader.TileMode; 

014 import android.graphics.drawable.BitmapDrawable; 

015 import android.view.View; 

016 import android.view.ViewGroup; 

017 import android.widget.BaseAdapter; 

018 import android.widget.ImageView; 

019   

020 import com.gallery.R; 

021   

022 public class ImageAdapter extends BaseAdapter { 

023     int mGalleryItemBackground; 

024     private Context mContext; 

025     private Integer[] mImageIds = {  

026             R.drawable.a1, 

027             R.drawable.a2,  

028             R.drawable.a3, 

029             R.drawable.a4,  

030             R.drawable.a5 }; 

031   

032     public ImageAdapter(Context c) { 

033         mContext = c; 

034     } 

035   

036     public int getCount() { 

037         return mImageIds.length; 

038     } 

039   

040     public Object getItem(int position) { 

041         return position; 

042     } 

043   

044     public long getItemId(int position) { 

045         return position; 

046     } 

047   

048     public View getView(int position, View convertView, ViewGroup parent) { 

049   

050         ImageView i = createReflectedImages(mContext,mImageIds[position]); 

051           

052         i.setLayoutParams(new CoverFlow.LayoutParams(120, 100)); 

053         i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

054           

055         // 设置的抗锯齿 

056         BitmapDrawable drawable = (BitmapDrawable) i.getDrawable(); 

057         drawable.setAntiAlias(true); 

058         return i; 

059     } 

060   

061     public float getScale(boolean focused, int offset) { 

062         return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); 

063     } 

064       

065     public ImageView createReflectedImages(Context mContext,int imageId) { 

066   

067         Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId); 

068           

069         final int reflectionGap = 4; 

070           

071         int width = originalImage.getWidth(); 

072         int height = originalImage.getHeight(); 

073   

074         Matrix matrix = new Matrix(); 

075         matrix.preScale(1, -1); 

076   

077         Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 

078                 height / 2, width, height / 2, matrix, false); 

079   

080         Bitmap bitmapWithReflection = Bitmap.createBitmap(width, 

081                 (height + height / 2), Config.ARGB_8888); 

082   

083         Canvas canvas = new Canvas(bitmapWithReflection); 

084   

085         canvas.drawBitmap(originalImage, 0, 0, null); 

086   

087         Paint deafaultPaint = new Paint(); 

088         canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint); 

089   

090         canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 

091   

092         Paint paint = new Paint(); 

093         LinearGradient shader = new LinearGradient(0, originalImage 

094                 .getHeight(), 0, bitmapWithReflection.getHeight() 

095                 + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); 

096   

097         paint.setShader(shader); 

098   

099         paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 

100   

101         canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 

102                 + reflectionGap, paint); 

103   

104         ImageView imageView = new ImageView(mContext); 

105         imageView.setImageBitmap(bitmapWithReflection); 

106   

107         return imageView; 

108     } 

109       

110 }

BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
是保证图片绕Y旋转了以后不会出现锯齿.

下面是Activity:

01 package com.coverflow; 
package com.coverflow; 

02   

03 import android.app.Activity; 

04 import android.graphics.Color; 

05 import android.os.Bundle; 

06   

07 import com.gallery.R; 

08   

09 public class HelloAndroid extends Activity { 

10     /** Called when the activity is first created. */

11     @Override

12     public void onCreate(Bundle savedInstanceState) { 

13         super.onCreate(savedInstanceState); 

14   

15         CoverFlow cf = new CoverFlow(this); 

16         // cf.setBackgroundResource(R.drawable.shape); 

17         cf.setBackgroundColor(Color.BLACK); 

18         cf.setAdapter(new ImageAdapter(this)); 

19         ImageAdapter imageAdapter = new ImageAdapter(this); 

20         cf.setAdapter(imageAdapter); 

21         // cf.setAlphaMode(false); 

22         // cf.setCircleMode(false); 

23         cf.setSelection(2, true); 

24         cf.setAnimationDuration(1000); 

25         setContentView(cf); 

26     } 

27   

28 }

02   

03 import android.app.Activity; 

04 import android.graphics.Color; 

05 import android.os.Bundle; 

06   

07 import com.gallery.R; 

08   

09 public class HelloAndroid extends Activity { 

10     /** Called when the activity is first created. */

11     @Override

12     public void onCreate(Bundle savedInstanceState) { 

13         super.onCreate(savedInstanceState); 

14   

15         CoverFlow cf = new CoverFlow(this); 

16         // cf.setBackgroundResource(R.drawable.shape); 

17         cf.setBackgroundColor(Color.BLACK); 

18         cf.setAdapter(new ImageAdapter(this)); 

19         ImageAdapter imageAdapter = new ImageAdapter(this); 

20         cf.setAdapter(imageAdapter); 

21         // cf.setAlphaMode(false); 

22         // cf.setCircleMode(false); 

23         cf.setSelection(2, true); 

24         cf.setAnimationDuration(1000); 

25         setContentView(cf); 

26     } 

27   

28 }

参考自:
http://www.eoeandroid.com/thread-70209-1-1.html
http://www.apkbus.com/android-18441-1-1.html
 

你可能感兴趣的:(android,coverflow,gallery,Gallery炫酷效果)