Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wonkwh/4256069 to your computer and use it in GitHub Desktop.
Save wonkwh/4256069 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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment