Android实现单页显示3个Item的ViewPager炫酷切换效果

Android实现单页显示3个Item的ViewPager炫酷切换效果,第1张

概述单页显示3个Item的ViewPager炫酷切换效果,适用于Banner等。效果图RotateYRotateDownRotateUp

单页显示3个Item的VIEwPager炫酷切换效果,适用于Banner等。

效果图

Rotate Y

Rotate Down

Rotate Up

Alpha

ScaleIn

ScaleIn + Alpha + Rotate Down

使用

###(1)引入

compile `com.zhy:magic-vIEwpager:1.0.1`

###(2)示例

布局文件

<FrameLayout  androID:layout_wIDth="match_parent"  androID:layout_height="160dp"  androID:clipChildren="false"  androID:layout_centerInParent="true"  androID:background="#aadc71ff"  >  <androID.support.v4.vIEw.VIEwPager    androID:ID="@+ID/ID_vIEwpager"    androID:layout_wIDth="match_parent"    androID:layout_marginleft="60dp"    androID:clipChildren="false"    androID:layout_marginRight="60dp"    androID:layout_height="120dp"    androID:layout_gravity="center"    >  </androID.support.v4.vIEw.VIEwPager></FrameLayout>

注意外层androID:layout_centerInParent="true".(貌似其实没什么影响

编码

mVIEwPager.setPagemargin(20);//设置page间间距,自行根据需求设置mVIEwPager.setoffscreenPagelimit(3);//>=3mVIEwPager.setAdapter...//写法不变//setPagetransformer 决定动画效果mVIEwPager.setPagetransformer(true,new RotateDownPagetransformer());

##目前可选动画

AlphaPagetransformer
RotateDownPagetransformer
RotateUpPagetransformer
RotateYtransformer
NonPagetransformer
ScaleIntransformer

动画间可以自由组合,例如:

mVIEwPager.setPagetransformer(true,new RotateDownPagetransformer(new AlphaPagetransformer(new ScaleIntransformer()))); 

需求:

1.一个页面显示三个item

2.vIEwpager无限循环

3.定时滑动

4.长条形小点

<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  xmlns:tools="http://schemas.androID.com/tools"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:background="#aadc71ff"  androID:clipChildren="false"  tools:context="com.example.administrator.banner2.MainActivity">  <androID.support.v4.vIEw.VIEwPager    androID:ID="@+ID/vIEwpager"    androID:layout_wIDth="match_parent"    androID:layout_height="150dp"    androID:layout_gravity="center"    androID:layout_marginBottom="10dp"    androID:layout_marginleft="30dp"    androID:layout_marginRight="30dp"    androID:layout_margintop="10dp"    androID:clipChildren="false"></androID.support.v4.vIEw.VIEwPager>  <linearLayout    androID:ID="@+ID/dotLayout"    androID:layout_wIDth="match_parent"    androID:layout_height="10dp"    androID:layout_gravity="bottom"    androID:layout_marginBottom="10dp"    androID:gravity="center"    androID:orIEntation="horizontal"></linearLayout></FrameLayout>
import androID.os.Bundle;import androID.os.Handler;import androID.support.v4.vIEw.PagerAdapter;import androID.support.v4.vIEw.VIEwPager;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.relativeLayout;import com.zhy.magicvIEwpager.transformer.ScaleIntransformer;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends AppCompatActivity {  private VIEwPager mVIEwPager;  private PagerAdapter mAdapter;  private linearLayout dotLayout;  private Timer timer;  private Handler mHandler = new Handler();  private int preposition = 0;  int[] imgRes = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.i};  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    mVIEwPager = (VIEwPager) findVIEwByID(R.ID.vIEwpager);    dotLayout = (linearLayout) findVIEwByID(R.ID.dotLayout);    initDots();    mVIEwPager.setPagemargin(20);    mVIEwPager.setoffscreenPagelimit(3);    mVIEwPager.setAdapter(mAdapter = new PagerAdapter() {      @OverrIDe      public Object instantiateItem(VIEwGroup container,int position) {        ImageVIEw vIEw = new ImageVIEw(MainActivity.this);        vIEw.setScaleType(ImageVIEw.ScaleType.FIT_XY);        vIEw.setimageResource(imgRes[position % imgRes.length]);        container.addVIEw(vIEw);        return vIEw;      }      @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;      }    });    mVIEwPager.addOnPagechangelistener(new VIEwPager.OnPagechangelistener() {      @OverrIDe      public voID onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {      }      @OverrIDe      public voID onPageSelected(int position) {        dotLayout.getChildAt(preposition).setEnabled(false);        dotLayout.getChildAt(position % imgRes.length).setEnabled(true);        preposition = position % imgRes.length;      }      @OverrIDe      public voID onPageScrollStateChanged(int state) {      }    });    mVIEwPager.setPagetransformer(true,new ScaleIntransformer());    if (null != timer) {      timer.cancel();    }    timer = new Timer();    timer.schedule(new TimerTask() {      @OverrIDe      public voID run() {        mHandler.post(new Runnable() {          @OverrIDe          public voID run() {            mVIEwPager.setCurrentItem(mVIEwPager.getCurrentItem() + 1);          }        });      }    },1000);  }  private voID initDots() {    if (null != dotLayout) {      dotLayout.removeAllVIEws();    }    for (int i = 0; i < imgRes.length; i++) {      ImageVIEw dot = new ImageVIEw(this);      dot.setEnabled(false);      dot.setimageResource(R.drawable.dot);      linearLayout.LayoutParams params = new linearLayout.LayoutParams(relativeLayout.LayoutParams.WRAP_CONTENT,relativeLayout.LayoutParams.WRAP_CONTENT);      params.leftmargin = 10;      dot.setLayoutParams(params);      dotLayout.addVIEw(dot);    }    dotLayout.getChildAt(0).setEnabled(true);  }}
<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID">  <item androID:state_enabled="true">    <shape androID:shape="rectangle">      <size androID:wIDth="15dp" androID:height="3dp"></size>      <corners androID:radius="10dp" />      <solID androID:color="#ff0000" />    </shape>  </item>  <item androID:state_enabled="false">    <shape androID:shape="rectangle">      <size androID:wIDth="15dp" androID:height="3dp"></size>      <corners androID:radius="10dp" />      <solID androID:color="#ffffff" />    </shape>  </item></selector>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实现单页显示3个Item的ViewPager炫酷切换效果全部内容,希望文章能够帮你解决Android实现单页显示3个Item的ViewPager炫酷切换效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144166.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存