Reputation: 504
I am making a carouel with android ViewPager. I need to show something like that
I need to show one element on the center of screen and a little part of another element on the sides (like in the image). I am trying with
viewPager.setPageMargin(-390);
But this doesn't work in all screens, how can I set the correct margin depending the density of the screen?
Upvotes: 0
Views: 3383
Reputation: 906
1.This is a background-to-foreground ViewPager,please try:
package com.eftimoff.viewpagertransformers;
import android.view.View;
public class BackgroundToForegroundTransformer extends BaseTransformer {
@Override
protected void onTransform(View view, float position) {
final float height = view.getHeight();
final float width = view.getWidth();
final float scale = min(position < 0 ? 1f : Math.abs(1f - position), 0.5f);
view.setScaleX(scale);
view.setScaleY(scale);
view.setPivotX(width * 0.5f);
view.setPivotY(height * 0.5f);
view.setTranslationX(position < 0 ? width * position : -width * position * 0.25f);
}
private static final float min(float val, float min) {
return val < min ? min : val;
}
}
2.Please check this repository out. This effect is exactly what you want.
https://github.com/dolphinwang/ImageCoverFlow
Upvotes: 2
Reputation: 3664
This is a good use case for a dimension resource.
In values/dimens.xml, add
<dimen name="page_margin">16dp</dimen>
Then in Java you can call
int marginPx = getResources().getDimensionPixelSize(R.dimen.page_margin);
viewPager.setPageMargin(marginPx);
Upvotes: 1