先加上三张图片,在res下创建menu文件夹然后.xml文件
在values下的strings.xml中配置下,后面都会有配置的内容
主方法
private ViewPager mViewPager;
private PagerAdapter mAdapter;
int[] imgRes = {R.drawable.a1, R.drawable.a2, R.drawable.a3};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
mViewPager.setPageMargin(40);
mViewPager.setAdapter(mAdapter = new PagerAdapter()
{
@Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView view = new ImageView(MainActivity.this);
// ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// view.setLayoutParams(lp);
// view.setText(position + ":" + view);
view.setScaleType(ImageView.ScaleType.FIT_XY);
// view.setBackgroundColor(Color.parseColor("#44ff0000"));
final int realPosition = getRealPosition(position);
view.setImageResource(imgRes[realPosition]);
container.addView(view);
// view.setAdjustViewBounds(true);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "click position= " + realPosition, Toast.LENGTH_SHORT).show();
}
});
return view;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((View) object);
}
@Override
public int getCount()
{
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object o)
{
return view == o;
}
@Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
ViewPager viewPager = (ViewPager) container;
int position = viewPager.getCurrentItem();
if (position == 0) {
position = getFirstItemPosition();
} else if (position == getCount() - 1) {
position = getLastItemPosition();
}
viewPager.setCurrentItem(position, false);
}
private int getRealCount() {
return imgRes.length;
}
private int getRealPosition(int position) {
return position % getRealCount();
}
private int getFirstItemPosition() {
return Integer.MAX_VALUE / getRealCount() / 2 * getRealCount();
}
private int getLastItemPosition() {
return Integer.MAX_VALUE / getRealCount() / 2 * getRealCount() - 1;
}
});
mViewPager.setPageTransformer(true, new AlphaPageTransformer());
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
String[] effects = this.getResources().getStringArray(R.array.magic_effect);
for (String effect : effects)
menu.add(effect);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
String title = item.getTitle().toString();
mViewPager.setAdapter(mAdapter);
setTitle(title);
return true;
}
AlphaPageTransformer方法 实现接口 ViewPager.pagetransformer
private static final float DEFAULT_MAX_ROTATE = 15.0f;
private float mMaxRotate = DEFAULT_MAX_ROTATE;
@Override
public void transformPage(View view, float position) {
if (position < -1)
{ // [-Infinity,-1)
// This page is way off-screen to the left.
view.setRotation(mMaxRotate * -1);
view.setPivotX(view.getWidth());
view.setPivotY(view.getHeight());
} else if (position <= 1)
{ // [-1,1]
if (position < 0)//[0,-1]
{
view.setPivotX(view.getWidth() * (0.5f + 0.5f * (-position)));
view.setPivotY(view.getHeight());
view.setRotation(mMaxRotate * position);
} else//[1,0]
{
view.setPivotX(view.getWidth() * 0.5f * (1 - position));
view.setPivotY(view.getHeight());
view.setRotation(mMaxRotate * position);
}
} else
{ // (1,+Infinity]
// This page is way off-screen to the right.
view.setRotation(mMaxRotate);
view.setPivotX(view.getWidth() * 0);
view.setPivotY(view.getHeight());
}
}
配置menu文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
在strings中添加
<string name="action_settings">Settings</string>
<string-array name="magic_effect">
<item>RotateDown</item>
<item>RotateUp</item>
<item>RotateY</item>
<item>Standard</item>
<item>Alpha</item>
<item>ScaleIn</item>
<item>RotateDown and Alpha</item>
<item>RotateDown and Alpha And ScaleIn</item>
</string-array>
布局文件
<FrameLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:background="#aadc71ff">
<android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="250dp"
android:layout_height="150dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:clipChildren="false"
android:layout_gravity="center"
>
</android.support.v4.view.ViewPager>
</FrameLayout>