Created
September 13, 2016 07:07
-
-
Save twiceyuan/9f875ce262e2f8b8ea50c712d3c0aeda to your computer and use it in GitHub Desktop.
ViewPager 实现 Gallery 效果。主要依靠设置 clipChildren 属性和 ViewPager.Transformer 接口实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<FrameLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:clipChildren="false"> | |
<ViewPager | |
android:id="@+id/viewPager" | |
android:layout_width="200dp" | |
android:layout_height="100dp" | |
android:layout_gravity="center" | |
android:layout_marginTop="10dp" | |
android:clipChildren="false"/> | |
</FrameLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Build; | |
import android.support.v4.view.ViewCompat; | |
import android.support.v4.view.ViewPager; | |
import android.view.View; | |
public class ScaleInTransformer implements ViewPager.PageTransformer { | |
@Override | |
public void transformPage(View view, float position) { | |
float scale = 0.5f; | |
float scaleValue = Math.abs(Math.abs(position) - 1) * scale + scale; | |
scaleValue = position < -1 || position > 1 ? scale : scaleValue; | |
view.setScaleX(scaleValue); | |
view.setScaleY(scaleValue); | |
view.setAlpha(scaleValue); | |
/** | |
* position = -1: PivotX 为 width | |
* position = 0: PivotX 为 width / 2 | |
* position = 1: PivotX 为 0 | |
* 以此为基准做调整 | |
*/ | |
final float offset = Build.VERSION.SDK_INT > 19 ? (Math.abs(position) / position) * 0.5f : 0f; | |
view.setPivotX(view.getWidth() * (1 - position - offset) * scale); | |
ViewCompat.setElevation(view, position >= -0.25 && position <= 0.25 ? 1 : 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment