Gallery----自定义画廊效果

1) 自定义Gallery

public class GalleryFlow extends Gallery {
    private Camera mCamera = new Camera();
    private int mMaxRotationAngle = 60;
    private int mMaxZoom = -300;
    private int mCoveflowCenter;
    public GalleryFlow(Context context) {
        super(context);
        this.setStaticTransformationsEnabled(true);
    }
    public GalleryFlow(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setStaticTransformationsEnabled(true);
    }
    public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setStaticTransformationsEnabled(true);
    }
    public int getMaxRotationAngle() {
        return mMaxRotationAngle;
    }
    public void setMaxRotationAngle(int maxRotationAngle) {
        mMaxRotationAngle = maxRotationAngle;
    }
    public int getMaxZoom() {
        return mMaxZoom;
    }
    public void setMaxZoom(int maxZoom) {
        mMaxZoom = maxZoom;
    }
    private int getCenterOfCoverflow() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
    }
    private static int getCenterOfView(View view) {
        return view.getLeft() + view.getWidth() / 2;
    }

    protected boolean getChildStaticTransformation(View child, Transformation t) {
        final int childCenter = getCenterOfView(child);
        final int childWidth = child.getWidth();
        int rotationAngle = 0;
        t.clear();
        t.setTransformationType(Transformation.TYPE_MATRIX);
        if (childCenter == mCoveflowCenter) {
            transformImageBitmap((ImageView) child, t, 0);
        } else {
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
            }
            transformImageBitmap((ImageView) child, t, rotationAngle);
        }
        return true;
    }
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCoveflowCenter = getCenterOfCoverflow();
        super.onSizeChanged(w, h, oldw, oldh);
    }
    private void transformImageBitmap(ImageView child, Transformation t,
                    int rotationAngle) {
        mCamera.save();
        final Matrix imageMatrix = t.getMatrix();
        final int imageHeight = child.getLayoutParams().height;
        final int imageWidth = child.getLayoutParams().width;
        
        final int rotation = Math.abs(rotationAngle);

        mCamera.translate(0.0f, 0.0f, 100.0f);
        if (rotation < mMaxRotationAngle) {
            float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
            mCamera.translate(0.0f, 0.0f, zoomAmount);
        }

        mCamera.rotateY(rotationAngle);
        mCamera.getMatrix(imageMatrix);
        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
        mCamera.restore();
    }

2) 在布局文件中使用自定义的Grally控件

<tongchuang.com.test.app.utils.GZXGallery.GalleryFlow
    android:id="@+id/Gallery01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:scaleType="fitXY"
    android:padding="4dp"
/>

3) 定义适配器

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private Integer[] mImageIds;
    private ImageView[] mImages;
    
    public ImageAdapter(Context c, Integer[] ImageIds) {
     mContext = c;
     mImageIds = ImageIds;
     mImages = new ImageView[mImageIds.length];
    }

    public boolean createReflectedImages() {

     final int reflectionGap = 4;
     int index = 0;
     for (int imageId : mImageIds) {
      Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
      int width = originalImage.getWidth();
      int height = originalImage.getHeight();
      Matrix matrix = new Matrix();

      matrix.preScale(1, -1);
      
      Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,height/2, width, height/2, matrix, false);
      Bitmap bitmapWithReflection = Bitmap.createBitmap(width,(height + height / 2), Config.ARGB_8888);
      
      Canvas canvas = new Canvas(bitmapWithReflection);
      canvas.drawBitmap(originalImage, 0, 0, null);
      
      Paint deafaultPaint = new Paint();
      deafaultPaint.setAntiAlias(false);
      canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
      Paint paint = new Paint();
      paint.setAntiAlias(false);
       

      LinearGradient shader = new LinearGradient(0,originalImage.getHeight(), 0,bitmapWithReflection.getHeight() + reflectionGap,0x70ffffff, 0x00ffffff, TileMode.MIRROR);
      paint.setShader(shader);
      paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
      canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);
      
      ImageView imageView = new ImageView(mContext);
      imageView.setImageBitmap(bitmapWithReflection);
      imageView.setLayoutParams(new GalleryFlow.LayoutParams(300, 800));
      mImages[index++] = imageView;
     }
     return true;
    }
    @SuppressWarnings("unused")
    private Resources getResources() {
        return null;
    }

    public int getCount() {
        return mImageIds.length;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.v("aaaaaaaaa",position+"aaa");
        return mImages[position];
    }


//    public int getCount() {
//        return Integer.MAX_VALUE;
//    }
//    public Object getItem(int position) {
//        return mImages[position%mImages.length];
//    }
//    public long getItemId(int position) {
//        return position%mImages.length;
//    }
//    public View getView(int position, View convertView, ViewGroup parent) {
//        return mImages[position%mImages.length];
//    }
    
public float getScale(boolean focused, int offset) {
        return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
    }
   }

在定义适配器的时候,主要两点,主要是是否要实现Grally的循环显示,如果不实现循环显示的时候,这里就使用的是普通的自定义适配器,而实现循环的原理就是实现图片的个数无限大,在设置getCount()方法的时候,这里返回的是Integer.MAX_VALUE,而这个值是无限大的,并且的是在取position值的时候,此时使用的是取余的方法。

4) Activity中使用

public void initGallery(){
    Integer[] images = { R.drawable.changbiluo,R.drawable.xieweizhizhuluo,R.drawable.jinfufenghuangluo,
            R.drawable.madingchangbiluo,R.drawable.chiquanfenghuangluo,R.drawable.liupingdingzhizhuluo,
            R.drawable.zhizhuluo,R.drawable.banfengluo,R.drawable.daifengluo,R.drawable.shizhuashanbei,
            R.drawable.xinxilanshanbei,R.drawable.lilinqiyishanbei,R.drawable.huanghouhaishanha,
            R.drawable.shidie,R.drawable.matiluo, R.drawable.jianjiaomatiluo };

    ImageAdapter adapter = new ImageAdapter(this, images);
    adapter.createReflectedImages();
    GalleryFlow galleryFlow = (GalleryFlow) this.findViewById(R.id.Gallery01);
    galleryFlow.setFadingEdgeLength(0);
    galleryFlow.setSpacing(-100);
    galleryFlow.setAdapter(adapter);

    galleryFlow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
        }
    });
    
    int imgIndex=getIntentData().getImgIndex();
    galleryFlow.setSelection(imgIndex);
  //  galleryFlow.setSelection(300);
}

activity中使用的时候,关键的地方就是最后一句代码,该句代码的作用就是设置当前显示的图片,而这个imgIndex就是上一个界面传递过来的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值