ViewPager基本用法

ViewPager基本用法step1:在layout中定义一个ViewPager组件。step2:在Activity中获取ViewPager的引用。step3:为ViewPager设置适配器,step4:为ViewPager设置滑动特效浮现特效:Google为我们提供了API,android.support.v4.

大家好,欢迎来到IT知识分享网。

step1:在layout中定义一个ViewPager组件。

<android.support.v4.view.ViewPager>

step2:在Activity中获取ViewPager的引用。

pager=(ViewPager) findViewById(R.id.view_pager);

step3: 为ViewPager设置适配器,

PagerAdapter adapter=new ViewAdapter(pages); pager.setAdapter(adapter);

step4:为ViewPager设置滑动特效

浮现特效:Google为我们提供了API,android.support.v4.view.ViewPager.PageTransformer接口

public void transformPage(View page, float position)

page表示ViewPager中的一页,position[0,0]表示当前屏幕的一页,[0,-1]表示屏幕左边第一页,【0,1】表示屏幕右边第一页

当`page`向左边滑动时,`position`从0向-1变化,当`position==-1`时完全不可见;当`page`向右滑动时,`position`从0向1变化,当`position==1`时完全不可见。

//滑动特效

public class ScalePageTransformer implements ViewPager.PageTransformer {

private static final float MIN_SCALE=0.75f;

@Override

public void transformPage(View page, float position) {

//Log.d("TAG", "<"+page.hashCode()+", "+position+">");

// out of left screen

if(position<-1.0f) {

page.setScaleX(MIN_SCALE);

page.setScaleY(MIN_SCALE);

}

// slide left

else if(position<=0.0f) {

page.setAlpha(1.0f);

page.setTranslationX(0.0f);

page.setScaleX(1.0f);

page.setScaleY(1.0f);

}

// slide right

else if(position<=1.0f) {

page.setAlpha(1.0f-position);

page.setTranslationX(-page.getWidth()*position);

float scale=MIN_SCALE+(1.0f-MIN_SCALE)*(1.0f-position);

page.setScaleX(scale);

page.setScaleY(scale);

}

// out of right screen

else {

page.setScaleX(MIN_SCALE);

page.setScaleY(MIN_SCALE);

}

}

}

//旋转特效

public class RotatePageTransformer implements ViewPager.PageTransformer {

private static final float MAX_ROTATION=20.0f;

@Override

public void transformPage(View page, float position) {

if(position<-1)

rotate(page, -MAX_ROTATION);

else if(position<=1)

rotate(page, MAX_ROTATION*position);

else

rotate(page, MAX_ROTATION);

}

private void rotate(View view, float rotation) {

view.setPivotX(view.getWidth()*0.5f);

view.setPivotY(view.getHeight());

view.setRotation(rotation);

}

}

// 3d相册---1.围绕Y轴旋转一定的角度

public class GalleryPageTransformer implements ViewPager.PageTransformer {

private static final float MAX_ROTATION=20.0f;

private static final float MIN_SCALE=0.75f;

private static final float MAX_TRANSLATE=20.0f;

@Override

public void transformPage(View page, float position) {

if(position<-1) {

page.setTranslationX(MAX_TRANSLATE);

page.setScaleX(MIN_SCALE);

page.setScaleY(MIN_SCALE);

page.setRotationY(-MAX_ROTATION);

}

else if(position<=0) {

page.setTranslationX(-MAX_TRANSLATE*position);

float scale=MIN_SCALE+(1-MIN_SCALE)*(1.0f+position);

page.setScaleX(scale);

page.setScaleY(scale);

page.setRotationY(MAX_ROTATION*position);

}

else if(position<=1) {

page.setTranslationX(-MAX_TRANSLATE*position);

float scale=MIN_SCALE+(1-MIN_SCALE)*(1.0f-position);

page.setScaleX(scale);

page.setScaleY(scale);

page.setRotationY(MAX_ROTATION*position);

}

else {

page.setTranslationX(-MAX_TRANSLATE);

page.setScaleX(MIN_SCALE);

page.setScaleY(MIN_SCALE);

page.setRotationY(MAX_ROTATION);

}

}

}

// 3d相册---2.生成有倒影的图片

public static Bitmap getReverseBitmapById(Context context, int resId, float percent) {

// get the source bitmap

Bitmap srcBitmap=BitmapFactory.decodeResource(context.getResources(), resId);

// get the tow third segment of the reverse bitmap

Matrix matrix=new Matrix();

matrix.setScale(1, -1);

Bitmap rvsBitmap=Bitmap.createBitmap(srcBitmap, 0, (int) (srcBitmap.getHeight()*(1-percent)),

srcBitmap.getWidth(), (int) (srcBitmap.getHeight()*percent), matrix, false);

// combine the source bitmap and the reverse bitmap

Bitmap comBitmap=Bitmap.createBitmap(srcBitmap.getWidth(),

srcBitmap.getHeight()+rvsBitmap.getHeight()+20, srcBitmap.getConfig());

Canvas gCanvas=new Canvas(comBitmap);

gCanvas.drawBitmap(srcBitmap, 0, 0, null);

gCanvas.drawBitmap(rvsBitmap, 0, srcBitmap.getHeight()+20, null);

Paint paint=new Paint();

LinearGradient shader=new LinearGradient(0, srcBitmap.getHeight()+20, 0, comBitmap.getHeight(),

Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);

paint.setShader(shader);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

gCanvas.drawRect(0, srcBitmap.getHeight()+20, srcBitmap.getWidth(), comBitmap.getHeight(), paint);

return comBitmap;

}

// 3d相册---3.对原图进行处理

private List<View> getPages() {

List<View> pages=new ArrayList<>();

Field[] fields=R.drawable.class.getDeclaredFields();

try {

for (Field field : fields) {

if (field.getName().startsWith("page")) {

ImageView view = new ImageView(this);

view.setImageBitmap(ImageUtils.getReverseBitmapById(this, field.getInt(null), 0.5f));

pages.add(view);

}

}

} catch (IllegalAccessException e) {

e.printStackTrace();

}

return pages;

}

step5:为这个pager添加这个特效

pager.setPageTransformer(true, new ScalePageTransformer());

step6: 设置监听

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override

public void onPageSelected(int position) {

mBottomNavigationBar.selectTab(position);

setDarkStatusIcon(position != 2);

}

@Override

public void onPageScrollStateChanged(int state) {

}

});

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/35093.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信