Skip to content

Instantly share code, notes, and snippets.

@jgilfelt
Created December 3, 2012 20:05
Show Gist options
  • Save jgilfelt/4197613 to your computer and use it in GitHub Desktop.
Save jgilfelt/4197613 to your computer and use it in GitHub Desktop.
An ICS+ app/widget drawer style PageTransformer for your ViewPager
/***
* Copyright (c) 2012 readyState Software Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.readystatesoftware.pagetransformer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewPager.PageTransformer;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class ScaleFadePageTransformer implements PageTransformer {
private int mScreenXOffset;
public ScaleFadePageTransformer(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
mScreenXOffset = display.getWidth()/2;
}
@SuppressLint("NewApi")
@Override
public void transformPage(View page, float position) {
final float transformValue = Math.abs(Math.abs(position) - 1);
// apply fade effect
page.setAlpha(transformValue);
if (position > 0) {
// apply zoom effect only for pages to the right
page.setScaleX(transformValue);
page.setScaleY(transformValue);
page.setPivotX(0.5f);
final float translateValue = position * -mScreenXOffset;
if (translateValue > -mScreenXOffset) {
page.setTranslationX(translateValue);
} else {
page.setTranslationX(0);
}
}
}
}
@yogeshlanjekar
Copy link

Hi Jgilfelt,
I want to create carousel view in android using ViewPager and Page Transform as follow :
|Page -n|..........|Page -2| |Page -1| |Page 0| |Page 1| |Page 2|........|Page n|
Page 0 should be viewed fully while 2 pages on either side of Page 0 should be viewed partially.
At a time maximum 5 middle pages should be visible.
I will thankful if you could help me out for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment