您的位置:首页 > 移动开发 > Android开发

Android控件Gallery 3D效果

2011-10-27 20:09 676 查看
 
本文源码下载在:

在Linux公社的1号FTP服务器里,下载地址:

FTP地址:ftp://www.linuxidc.com

用户名:www.linuxidc.com

密码:www.muu.cc

在 2011年LinuxIDC.com\8月\Android控件Gallery 3D效果源码

下载方法见这里 http://www.linuxidc.net/thread-1187-1-1.html

1.扩展Gallery:

1.public class GalleryFlow extends Gallery {   

2.    private Camera mCamera = new Camera();//相机类   

3.    private int mMaxRotationAngle = 60;//最大转动角度   

4.    private int mMaxZoom = -300;////最大缩放值   

5.    private int mCoveflowCenter;//半径值   

6.    public GalleryFlow(Context context) {   

7.        super(context);   

8.        //支持转换 ,执行getChildStaticTransformation方法   

9.        this.setStaticTransformationsEnabled(true);   

10.    }   

11.    public GalleryFlow(Context context, AttributeSet attrs) {   

12.        super(context, attrs);   

13.        this.setStaticTransformationsEnabled(true);   

14.    }   

15.    public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {   

16.        super(context, attrs, defStyle);   

17.        this.setStaticTransformationsEnabled(true);   

18.    }   

19.    public int getMaxRotationAngle() {   

20.        return mMaxRotationAngle;   

21.    }   

22.    public void setMaxRotationAngle(int maxRotationAngle) {   

23.        mMaxRotationAngle = maxRotationAngle;   

24.    }   

25.    public int getMaxZoom() {   

26.        return mMaxZoom;   

27.    }   

28.    public void setMaxZoom(int maxZoom) {   

29.        mMaxZoom = maxZoom;   

30.    }   

31.    private int getCenterOfCoverflow() {   

32.        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2  

33.                        + getPaddingLeft();   

34.    }   

35.    private static int getCenterOfView(View view) {   

36.        System.out.println("view left :"+view.getLeft());   

37.        System.out.println("view width :"+view.getWidth());   

38.        return view.getLeft() + view.getWidth() / 2;   

39.    }   

40.       

41.       

42.   //控制gallery中每个图片的旋转(重写的gallery中方法)   

43.    protected boolean getChildStaticTransformation(View child, Transformation t) {     

44.        //取得当前子view的半径值   

45.        final int childCenter = getCenterOfView(child);   

46.        System.out.println("childCenter:"+childCenter);   

47.        final int childWidth = child.getWidth();   

48.        //旋转角度   

49.        int rotationAngle = 0;   

50.        //重置转换状态   

51.        t.clear();   

52.        //设置转换类型   

53.        t.setTransformationType(Transformation.TYPE_MATRIX);   

54.        //如果图片位于中心位置不需要进行旋转   

55.        if (childCenter == mCoveflowCenter) {   

56.            transformImageBitmap((ImageView) child, t, 0);   

57.        } else {   

58.            //根据图片在gallery中的位置来计算图片的旋转角度   

59.            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);   

60.            System.out.println("rotationAngle:" +rotationAngle);   

61.            //如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)   

62.            if (Math.abs(rotationAngle) > mMaxRotationAngle) {   

63.                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;   

64.            }   

65.            transformImageBitmap((ImageView) child, t, rotationAngle);   

66.        }   

67.        return true;   

68.    }   

69.    protected void onSizeChanged(int w, int h, int oldw, int oldh) {   

70.        mCoveflowCenter = getCenterOfCoverflow();   

71.        super.onSizeChanged(w, h, oldw, oldh);   

72.    }   

73.    private void transformImageBitmap(ImageView child, Transformation t,   

74.                    int rotationAngle) {   

75.        //对效果进行保存   

76.        mCamera.save();   

77.        final Matrix imageMatrix = t.getMatrix();   

78.        //图片高度   

79.        final int imageHeight = child.getLayoutParams().height;   

80.        //图片宽度   

81.        final int imageWidth = child.getLayoutParams().width;   

82.           

83.        //返回旋转角度的绝对值   

84.        final int rotation = Math.abs(rotationAngle);   

85.           

86.        // 在Z轴上正向移动camera的视角,实际效果为放大图片。   

87.        // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。   

88.        mCamera.translate(0.0f, 0.0f, 100.0f);   

89.        // As the angle of the view gets less, zoom in   

90.        if (rotation < mMaxRotationAngle) {   

91.            float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));   

92.            mCamera.translate(0.0f, 0.0f, zoomAmount);   

93.        }   

94.        // 在Y轴上旋转,对应图片竖向向里翻转。   

95.        // 如果在X轴上旋转,则对应图片横向向里翻转。   

96.        mCamera.rotateY(rotationAngle);   

97.        mCamera.getMatrix(imageMatrix);   

98.        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));   

99.        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));   

100.        mCamera.restore();   

101.    }   

102.} 

 

2.填充图片容器(BaseAdapter):

1.public class ImageAdapter extends BaseAdapter {  

2.    int mGalleryItemBackground;   3.    private Context mContext;   4.    private Integer[] mImageIds;   5.    private ImageView[] mImages;   6.      

7.    public ImageAdapter(Context c, Integer[] ImageIds) {   8.     mContext = c;  

9.     mImageIds = ImageIds;  

10.     mImages = new ImageView[mImageIds.length];   11.    }  

12.    /**  13.     * 创建倒影效果 

14.     * @return 

15.     */ 

16.    public boolean createReflectedImages() {   17.     //倒影图和原图之间的距离   18.     final int reflectionGap = 4;   19.     int index = 0;   20.     for (int imageId : mImageIds) {   21.      //返回原图解码之后的bitmap对象   22.      Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(),
imageId);  

23.      int width = originalImage.getWidth();   24.      int height = originalImage.getHeight();   25.      //创建矩阵对象   26.      Matrix matrix = new Matrix();   27.        

28.      //指定一个角度以0,0为坐标进行旋转   29.      // matrix.setRotate(30);   30.        

31.      //指定矩阵(x轴不变,y轴相反)   32.      matrix.preScale(1, -1);   33.        

34.      //将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图   35.      Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,   36.        height/2, width, height/2, matrix, false);   37.        

38.      //创建一个宽度不变,高度为原图+倒影图高度的位图   39.      Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  

40.        (height + height / 2), Config.ARGB_8888);   41.        

42.      //将上面创建的位图初始化到画布   43.      Canvas canvas = new Canvas(bitmapWithReflection);   44.      canvas.drawBitmap(originalImage, 0, 0, null);   45.        

46.      Paint deafaultPaint = new Paint();    47.      deafaultPaint.setAntiAlias(false);   48.//    canvas.drawRect(0, height, width, height + reflectionGap,deafaultPaint);   49.      canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
50.      Paint paint = new Paint();   51.      paint.setAntiAlias(false);   52.         

53.      /**  54.       * 参数一:为渐变起初点坐标x位置, 

55.       * 参数二:为y轴位置, 

56.       * 参数三和四:分辨对应渐变终点, 

57.       * 最后参数为平铺方式, 

58.       * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变 

59.       */ 

60.      LinearGradient shader = new LinearGradient(0,originalImage.getHeight(), 0,   61.              bitmapWithReflection.getHeight() + reflectionGap,0x70ffffff, 0x00ffffff, TileMode.MIRROR);   62.      //设置阴影   63.      paint.setShader(shader);  

64.      paint.setXfermode(new PorterDuffXfermode(Android.graphics.PorterDuff.Mode.DST_IN));   65.      //用已经定义好的画笔构建一个矩形阴影渐变效果   66.      canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);   67.        

68.      //创建一个ImageView用来显示已经画好的bitmapWithReflection   69.      ImageView imageView = new ImageView(mContext);   70.      imageView.setImageBitmap(bitmapWithReflection);  

71.      //设置imageView大小 ,也就是最终显示的图片大小   72.      imageView.setLayoutParams(new GalleryFlow.LayoutParams(300, 400));   73.      //imageView.setScaleType(ScaleType.MATRIX);   74.      mImages[index++] = imageView;  

75.     }  

76.     return true;   77.    }  

78.    @SuppressWarnings("unused")   79.    private Resources getResources() {   80.        return null;   81.    }  

82.    public int getCount() {   83.        return mImageIds.length;   84.    }  

85.    public Object getItem(int position) {   86.        return position;   87.    }  

88.    public long getItemId(int position) {   89.        return position;   90.    }  

91.    public View getView(int position, View convertView, ViewGroup parent) {   92.        return mImages[position];   93.    }  

94.    public float getScale(boolean focused, int offset) {   95.        return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));   96.    }  

97.   } 

3.创建Activity:

1.public class Gallery3DActivity extends Activity {  

2.    public void onCreate(Bundle savedInstanceState) {   3.        super.onCreate(savedInstanceState);   4.          

5.          

6.        setContentView(R.layout.layout_gallery);  

7.          

8.        Integer[] images = { R.drawable.img0001, R.drawable.img0030,  

9.                R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,  

10.                R.drawable.img0230,  R.drawable.img0330,R.drawable.img0354 };  

11.          

12.        ImageAdapter adapter = new ImageAdapter(this, images);   13.        adapter.createReflectedImages();//创建倒影效果   14.        GalleryFlow galleryFlow = (GalleryFlow) this.findViewById(R.id.Gallery01);   15.        galleryFlow.setFadingEdgeLength(0);  
16.        galleryFlow.setSpacing(-100); //图片之间的间距   17.        galleryFlow.setAdapter(adapter);  

18.          

19.        galleryFlow.setOnItemClickListener(new OnItemClickListener() {   20.            public void onItemClick(AdapterView<?> parent, View view,   21.                    int position, long id) {   22.                Toast.makeText(getApplicationContext(),
String.valueOf(position), Toast.LENGTH_SHORT).show();  

23.            }  

24.              

25.        });  

26.        galleryFlow.setSelection(4);   27.    }  

28.} 

以上实现代码里面我都做了注释相信大家完全可以看懂。稍微解释下,在BaseAdapter中主要做了图片的倒影效果以及创建了对原始图片和倒影的显示区域。GalleryFlow中主要做了对图片的旋转和缩放操作,根据图片的屏幕中的位置对其进行旋转缩放操作。

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-08/41159p3.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息