Android控件Gallery3D效果

来源:互联网 发布:淘宝怎么搜烟 编辑:程序博客网 时间:2024/03/29 05:35

Android控件Gallery3D效果

贴上代码:
1.扩展Gallery:

查看源码
打印?
001public class GalleryFlow extendsGallery {
002    privateCamera mCamera = newCamera();//相机类
003    privateint mMaxRotationAngle =60;//最大转动角度
004    privateint mMaxZoom = -300;////最大缩放值
005    privateint mCoveflowCenter;//半径值
006    publicGalleryFlow(Context context) {
007        super(context);
008//支持转换 ,执行getChildStaticTransformation方法
009        this.setStaticTransformationsEnabled(true);
010    }
011    publicGalleryFlow(Context context, AttributeSet attrs) {
012        super(context, attrs);
013        this.setStaticTransformationsEnabled(true);
014    }
015    publicGalleryFlow(Context context, AttributeSet attrs, int defStyle) {
016        super(context, attrs, defStyle);
017        this.setStaticTransformationsEnabled(true);
018    }
019    publicint getMaxRotationAngle() {
020        returnmMaxRotationAngle;
021    }
022    publicvoid setMaxRotationAngle(intmaxRotationAngle) {
023        mMaxRotationAngle = maxRotationAngle;
024    }
025    publicint getMaxZoom() {
026        returnmMaxZoom;
027    }
028    publicvoid setMaxZoom(intmaxZoom) {
029        mMaxZoom = maxZoom;
030    }
031    privateint getCenterOfCoverflow() {
032        return(getWidth() - getPaddingLeft() - getPaddingRight()) / 2
033                        + getPaddingLeft();
034    }
035    privatestatic int getCenterOfView(View view) {
036        System.out.println("view left :"+view.getLeft());
037        System.out.println("view width :"+view.getWidth());
038        returnview.getLeft() + view.getWidth() / 2;
039    }
040     
041     
042   //控制gallery中每个图片的旋转(重写的gallery中方法)
043    protectedboolean getChildStaticTransformation(View child, Transformation t) { 
044        //取得当前子view的半径值
045        finalint childCenter = getCenterOfView(child);
046        System.out.println("childCenter:"+childCenter);
047        finalint childWidth = child.getWidth();
048        //旋转角度
049        introtationAngle = 0;
050        //重置转换状态
051        t.clear();
052        //设置转换类型
053        t.setTransformationType(Transformation.TYPE_MATRIX);
054        //如果图片位于中心位置不需要进行旋转
055        if(childCenter == mCoveflowCenter) {
056            transformImageBitmap((ImageView) child, t,0);
057        }else {
058            //根据图片在gallery中的位置来计算图片的旋转角度
059            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
060            System.out.println("rotationAngle:"+rotationAngle);
061            //如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)
062            if(Math.abs(rotationAngle) > mMaxRotationAngle) {
063                rotationAngle = (rotationAngle <0) ? -mMaxRotationAngle : mMaxRotationAngle;
064            }
065            transformImageBitmap((ImageView) child, t, rotationAngle);
066        }
067        returntrue;
068    }
069    protectedvoid onSizeChanged(intw, int h, int oldw, int oldh) {
070        mCoveflowCenter = getCenterOfCoverflow();
071        super.onSizeChanged(w, h, oldw, oldh);
072    }
073    privatevoid transformImageBitmap(ImageView child, Transformation t,
074                    introtationAngle) {
075        //对效果进行保存
076        mCamera.save();
077        finalMatrix imageMatrix = t.getMatrix();
078        //图片高度
079        finalint imageHeight = child.getLayoutParams().height;
080        //图片宽度
081        finalint imageWidth = child.getLayoutParams().width;
082         
083        //返回旋转角度的绝对值
084        finalint rotation = Math.abs(rotationAngle);
085         
086        // 在Z轴上正向移动camera的视角,实际效果为放大图片。
087        // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
088        mCamera.translate(0.0f,0.0f, 100.0f);
089        // As the angle of the view gets less, zoom in
090        if(rotation < mMaxRotationAngle) {
091            floatzoomAmount = (float) (mMaxZoom + (rotation *1.5));
092            mCamera.translate(0.0f,0.0f, zoomAmount);
093        }
094        // 在Y轴上旋转,对应图片竖向向里翻转。
095        // 如果在X轴上旋转,则对应图片横向向里翻转。
096        mCamera.rotateY(rotationAngle);
097        mCamera.getMatrix(imageMatrix);
098        imageMatrix.preTranslate(-(imageWidth /2), -(imageHeight /2));
099        imageMatrix.postTranslate((imageWidth /2), (imageHeight /2));
100        mCamera.restore();
101    }
102}
103  
1042.填充图片容器(BaseAdapter):
105public class ImageAdapter extendsBaseAdapter {
106    intmGalleryItemBackground;
107    privateContext mContext;
108    privateInteger[] mImageIds;
109    privateImageView[] mImages;
110     
111    publicImageAdapter(Context c, Integer[] ImageIds) {
112     mContext = c;
113     mImageIds = ImageIds;
114     mImages =new ImageView[mImageIds.length];
115    }
116    /**
117     * 创建倒影效果
118     * @return
119     */
120    publicboolean createReflectedImages() {
121     //倒影图和原图之间的距离
122     finalint reflectionGap = 4;
123     intindex = 0;
124     for(int imageId : mImageIds) {
125      //返回原图解码之后的bitmap对象
126      Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
127      intwidth = originalImage.getWidth();
128      intheight = originalImage.getHeight();
129      //创建矩阵对象
130      Matrix matrix =new Matrix();
131       
132      //指定一个角度以0,0为坐标进行旋转
133      // matrix.setRotate(30);
134       
135      //指定矩阵(x轴不变,y轴相反)
136      matrix.preScale(1, -1);
137       
138      //将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图
139      Bitmap reflectionImage = Bitmap.createBitmap(originalImage,0,
140        height/2, width, height/2, matrix,false);
141       
142      //创建一个宽度不变,高度为原图+倒影图高度的位图
143      Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
144        (height + height /2), Config.ARGB_8888);
145       
146      //将上面创建的位图初始化到画布
147      Canvas canvas =new Canvas(bitmapWithReflection);
148      canvas.drawBitmap(originalImage,0, 0,null);
149       
150      Paint deafaultPaint =new Paint();
151      deafaultPaint.setAntiAlias(false);
152//    canvas.drawRect(0, height, width, height + reflectionGap,deafaultPaint);
153      canvas.drawBitmap(reflectionImage,0, height + reflectionGap,null);
154      Paint paint =new Paint();
155      paint.setAntiAlias(false);
156        
157      /**
158       * 参数一:为渐变起初点坐标x位置,
159       * 参数二:为y轴位置,
160       * 参数三和四:分辨对应渐变终点,
161       * 最后参数为平铺方式,
162       * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变
163       */
164      LinearGradient shader =new LinearGradient(0,originalImage.getHeight(),0,
165              bitmapWithReflection.getHeight() + reflectionGap,0x70ffffff,0x00ffffff, TileMode.MIRROR);
166      //设置阴影
167      paint.setShader(shader);
168      paint.setXfermode(newPorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
169      //用已经定义好的画笔构建一个矩形阴影渐变效果
170      canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);
171       
172      //创建一个ImageView用来显示已经画好的bitmapWithReflection
173      ImageView imageView =new ImageView(mContext);
174      imageView.setImageBitmap(bitmapWithReflection);
175      //设置imageView大小 ,也就是最终显示的图片大小
176      imageView.setLayoutParams(newGalleryFlow.LayoutParams(300,400));
177      //imageView.setScaleType(ScaleType.MATRIX);
178      mImages[index++] = imageView;
179     }
180     returntrue;
181    }
182    @SuppressWarnings("unused")
183    privateResources getResources() {
184        returnnull;
185    }
186    publicint getCount() {
187        returnmImageIds.length;
188    }
189    publicObject getItem(intposition) {
190        returnposition;
191    }
192    publiclong getItemId(intposition) {
193        returnposition;
194    }
195    publicView getView(intposition, View convertView, ViewGroup parent) {
196        returnmImages[position];
197    }
198    publicfloat getScale(booleanfocused, int offset) {
199        returnMath.max(0,1.0f / (float) Math.pow(2, Math.abs(offset)));
200    }
201   }
202  
2033.创建Activity:
204public class Gallery3DActivity extendsActivity {
205    publicvoid onCreate(Bundle savedInstanceState) {
206        super.onCreate(savedInstanceState);
207         
208         
209        setContentView(R.layout.layout_gallery);
210         
211        Integer[] images = { R.drawable.img0001, R.drawable.img0030,
212                R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,
213                R.drawable.img0230,  R.drawable.img0330,R.drawable.img0354 };
214         
215        ImageAdapter adapter =new ImageAdapter(this, images);
216        adapter.createReflectedImages();//创建倒影效果
217        GalleryFlow galleryFlow = (GalleryFlow)this.findViewById(R.id.Gallery01);
218        galleryFlow.setFadingEdgeLength(0);
219        galleryFlow.setSpacing(-100);//图片之间的间距
220        galleryFlow.setAdapter(adapter);
221         
222        galleryFlow.setOnItemClickListener(newOnItemClickListener() {
223            publicvoid onItemClick(AdapterView parent, View view,
224                    intposition, long id) {
225                Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
226            }
227             
228        });
229        galleryFlow.setSelection(4);
230    }
231}

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