ViewPager实现画廊效果
程序员文章站
2022-05-15 17:16:33
...
1.先看一下效果
2.技术要点:
1、setClipChildren(boolean clipChildren)方法:
clipChild用来定义他的子控件是否要在他应有的边界内进行绘制。 默认情况下,clipChild被设置为true。 也就是不允许进行扩展绘制。而在这个Demo中承载ViewPager的父容器必须要将其设置为false才可以绘制旁边的两页
2、setPageTransformer(boolean reverseDrawingOrder, PageTransformer transformer))方法
用于设置ViewPager切换时的动画效果,许多ViewPager切换效果都是通过创建一个类实现ViewPager.PageTransformer然后重写transformPage方法来实现各种切换效果
3.上代码
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/viewPagerContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:clipChildren="false"
android:gravity="center"
tools:context="com.lsl.myapplication.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:clipChildren="false" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
主要代码
package com.lsl.myapplication;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.lsl.myapplication.utils.DeviceUtils;
public class MainActivity extends AppCompatActivity {
private ViewPager mViewPager;
private RelativeLayout mViewPagerContainer;
private static int TOTAL_COUNT = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager= (ViewPager) findViewById(R.id.viewpager);
mViewPagerContainer= (RelativeLayout) findViewById(R.id.viewPagerContainer);
initViewPager();
}
private void initViewPager() {
//设置ViewPager的布局
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(
DeviceUtils.getWindowWidth(this)*5/10,
DeviceUtils.getWindowHeight(this)*6/10);
/**** 重要部分 ******/
//clipChild用来定义他的子控件是否要在他应有的边界内进行绘制。 默认情况下,clipChild被设置为true。 也就是不允许进行扩展绘制。
mViewPager.setClipChildren(false);
//父容器一定要设置这个,否则看不出效果
mViewPagerContainer.setClipChildren(false);
mViewPager.setLayoutParams(params);
//为ViewPager设置PagerAdapter
mViewPager.setAdapter(new MyPagerAdapter());
//设置ViewPager切换效果,即实现画廊效果
mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
//设置预加载数量
mViewPager.setOffscreenPageLimit(2);
//设置每页之间的左右间隔
mViewPager.setPageMargin(100);
//将容器的触摸事件反馈给ViewPager
mViewPagerContainer.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// dispatch the events to the ViewPager, to solve the problem that we can swipe only the middle view.
return mViewPager.dispatchTouchEvent(event);
}
});
}
class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
//return viewList==null?0:viewList.size();
return TOTAL_COUNT;//ViewPager里的个数
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(R.mipmap.test);
((ViewPager)container).addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager)container).removeView((ImageView)object);
}
}
public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageSelected(int position) {
//这里做切换ViewPager时,底部RadioButton的操作
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (mViewPagerContainer != null) {
mViewPagerContainer.invalidate();
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}
/**
* 实现的原理是,在当前显示页面放大至原来的MAX_SCALE
* 其他页面才是正常的的大小MIN_SCALE
*/
class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MAX_SCALE = 1.2f;
private static final float MIN_SCALE = 1.0f;//0.85f
@Override
public void transformPage(View view, float position) {
//setScaleY只支持api11以上
if (position < -1){
view.setScaleX(MIN_SCALE);
view.setScaleY(MIN_SCALE);
} else if (position <= 1) //a页滑动至b页 ; a页从 0.0 -1 ;b页从1 ~ 0.0
{ // [-1,1]
// Log.e("TAG", view + " , " + position + "");
float scaleFactor = MIN_SCALE+(1-Math.abs(position))*(MAX_SCALE-MIN_SCALE);
view.setScaleX(scaleFactor);
//每次滑动后进行微小的移动目的是为了防止在三星的某些手机上出现两边的页面为显示的情况
if(position>0){
view.setTranslationX(-scaleFactor*2);
}else if(position<0){
view.setTranslationX(scaleFactor*2);
}
view.setScaleY(scaleFactor);
} else
{ // (1,+Infinity]
view.setScaleX(MIN_SCALE);
view.setScaleY(MIN_SCALE);
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
代码里面有一个DeviceUtils类,这个类主要用来获取设备的屏幕尺寸