單頁顯示3個Item的ViewPager炫酷切換效果,適用于Banner等。
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、惠山網(wǎng)站定制設(shè)計、自適應品牌網(wǎng)站建設(shè)、H5開發(fā)、電子商務商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應式網(wǎng)頁設(shè)計等建站業(yè)務,價格優(yōu)惠性價比高,為惠山等各大城市提供網(wǎng)站開發(fā)制作服務。
效果圖
Rotate Y
Rotate Down
Rotate Up
Alpha
ScaleIn
ScaleIn + Alpha + Rotate Down
使用
###(1)引入
compile `com.zhy:magic-viewpager:1.0.1`
###(2)示例
布局文件
注意外層android:layout_centerInParent="true".(貌似其實沒什么影響)
編碼
mViewPager.setPageMargin(20);//設(shè)置page間間距,自行根據(jù)需求設(shè)置 mViewPager.setOffscreenPageLimit(3);//>=3 mViewPager.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無限循環(huán)
3.定時滑動
4.長條形小點
<?xml version="1.0" encoding="utf-8"?>
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); } }); } }, 0, 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"?>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。