Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Grid View animation in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 942
    Comment on it

    By using below code I have implemented GridView animation in android. When you click on any image On Grid View, Image will zoom like popup. In below Adapter class I have used Animator class and I have created a zoomImageFromThumb method for zooming icon.

     

    public class ImageAdapter extends BaseAdapter {
    
        
        //Animator class
        private Animator mCurrentAnimator;
    
        private int mShortAnimationDuration;
    
        private Context mContext;
        LayoutInflater inflater;
    
        public ImageAdapter(Context c) {
            mContext = c;
        }
    
        public int getCount() {
            return mThumbIds.length;
        }
    
        public Object getItem(int position) {
            return mThumbIds[position];
        }
    
        public long getItemId(int position) {
            return 0;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            final ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            } else {
                imageView = (ImageView) convertView;
            }
    
            imageView.setImageResource(mThumbIds[position]);
            imageView.setTag(mThumbIds[position]);
            imageView.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    int id = (Integer) arg0.getTag();
                    zoomImageFromThumb(arg0, id);  //created method for animtor icon
                }
            });
    
            return imageView;
        }
    
    
        public Integer[] mThumbIds = { R.drawable.one, R.drawable.one,
                R.drawable.one, R.drawable.one, R.drawable.one,
                R.drawable.one, R.drawable.one, R.drawable.one,
                R.drawable.one, R.drawable.one, R.drawable.one,
                R.drawable.one, R.drawable.one, R.drawable.one,
                R.drawable.one, R.drawable.one, R.drawable.one,
                R.drawable.one, R.drawable.one };
    
        //
        private void zoomImageFromThumb(final View thumbView, int imageResId) {
    
            if (mCurrentAnimator != null) {
                mCurrentAnimator.cancel();
            }
    
    
            final ImageView expandedImageView = (ImageView) ((Activity) mContext)
                    .findViewById(R.id.expanded_image);
            expandedImageView.setImageResource(imageResId);
    
    
            final Rect startBounds = new Rect();
            final Rect finalBounds = new Rect();
            final Point globalOffset = new Point();
    
    
            thumbView.getGlobalVisibleRect(startBounds);
            ((Activity) mContext).findViewById(R.id.container)
                    .getGlobalVisibleRect(finalBounds, globalOffset);
            startBounds.offset(-globalOffset.x, -globalOffset.y);
            finalBounds.offset(-globalOffset.x, -globalOffset.y);
    
    
            float startScale;
            if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds
                    .width() / startBounds.height()) {
                // Extend start bounds horizontally
                startScale = (float) startBounds.height() / finalBounds.height();
                float startWidth = startScale * finalBounds.width();
                float deltaWidth = (startWidth - startBounds.width()) / 2;
                startBounds.left -= deltaWidth;
                startBounds.right += deltaWidth;
            } else {
                // Extend start bounds vertically
                startScale = (float) startBounds.width() / finalBounds.width();
                float startHeight = startScale * finalBounds.height();
                float deltaHeight = (startHeight - startBounds.height()) / 2;
                startBounds.top -= deltaHeight;
                startBounds.bottom += deltaHeight;
            }
    
    
            thumbView.setAlpha(0f);
            expandedImageView.setVisibility(View.VISIBLE);
    
    
            expandedImageView.setPivotX(0f);
            expandedImageView.setPivotY(0f);
    
            // Construct and run the  animation of the four translation and
            // scale properties
            // (X, Y, SCALE_X, and SCALE_Y).
            AnimatorSet set = new AnimatorSet();
            set.play(
                    ObjectAnimator.ofFloat(expandedImageView, View.X,
                            startBounds.left, finalBounds.left))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
                            startBounds.top, finalBounds.top))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X,
                            startScale, 1f))
                    .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y,
                            startScale, 1f));
            set.setDuration(mShortAnimationDuration);
            set.setInterpolator(new DecelerateInterpolator());
            set.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mCurrentAnimator = null;
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
                    mCurrentAnimator = null;
                }
            });
            set.start();
            mCurrentAnimator = set;
    
            // Upon clicking the zoomed-in image, it should zoom back down to the
            // original bounds
            // and show the thumbnail instead of the expanded image.
            final float startScaleFinal = startScale;
            expandedImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mCurrentAnimator != null) {
                        mCurrentAnimator.cancel();
                    }
    
                    // Animate the four positioning/sizing properties in parallel,
                    // back to their
                    // original values.
                    AnimatorSet set = new AnimatorSet();
                    set.play(
                            ObjectAnimator.ofFloat(expandedImageView, View.X,
                                    startBounds.left))
                            .with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
                                    startBounds.top))
                            .with(ObjectAnimator.ofFloat(expandedImageView,
                                    View.SCALE_X, startScaleFinal))
                            .with(ObjectAnimator.ofFloat(expandedImageView,
                                    View.SCALE_Y, startScaleFinal));
                    set.setDuration(mShortAnimationDuration);
                    set.setInterpolator(new DecelerateInterpolator());
                    set.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            thumbView.setAlpha(1f);
                            expandedImageView.setVisibility(View.GONE);
                            mCurrentAnimator = null;
                        }
    
                        @Override
                        public void onAnimationCancel(Animator animation) {
                            thumbView.setAlpha(1f);
                            expandedImageView.setVisibility(View.GONE);
                            mCurrentAnimator = null;
                        }
                    });
                    set.start();
                    mCurrentAnimator = set;
                }
            });
        }
    }
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: