本文主要是介绍android广告轮播图之匀速规律播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
之前在电商项目中用到广告轮播的效果,在app端实现广告图片的上传,然后轮播图片,使用handler发送消息然后在handlemessage中在发消息的循环发送可以实现广告轮播效果,但是当添加图片以后,广告轮播的速度变的不规律起来,异常的快,使用scheduleWithFixedDelay发送消息也是如此,因此在朋友的推荐下使用第三方框架RollViewPager,摒弃安卓原生的viewpager,完美的实现了图片的匀速规律轮播效果。https://github.com/Jude95/RollViewPager
布局文件
</pre><pre style="font-family: Consolas; font-size: 14.3pt; background-color: rgb(255, 255, 255);"><pre name="code" class="html"><com.jude.rollviewpager.RollPagerViewandroid:id="@+id/roll_view_pager"android:layout_width="match_parent"android:layout_height="180dp"app:rollviewpager_play_delay="3000"/>
MainActivity public class MainActivity extends AppCompatActivity {private RollPagerView mRollViewPager;private TestLoopAdapter mLoopAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mRollViewPager= (RollPagerView) findViewById(R.id.roll_view_pager);mRollViewPager.setPlayDelay(1000);mRollViewPager.setAdapter(mLoopAdapter = new TestLoopAdapter(mRollViewPager));mRollViewPager.setHintView(new IconHintView(this,R.drawable.point_focus,R.drawable.point_normal));}private class TestLoopAdapter extends LoopPagerAdapter{private int[] imgs = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,};private int count = imgs.length;public TestLoopAdapter(RollPagerView viewPager) {super(viewPager);}@Overridepublic View getView(ViewGroup container, int position) {ImageView view = new ImageView(container.getContext());view.setImageResource(imgs[position]);view.setScaleType(ImageView.ScaleType.CENTER_CROP);view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));return view;}@Overridepublic int getRealCount() {return count;}}private class TestNomalAdapter extends StaticPagerAdapter{private int[] imgs = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,};private int count = imgs.length;public void add(){Log.i("RollViewPager","Add");count++;if (count>imgs.length)count = imgs.length;notifyDataSetChanged();}public void minus(){Log.i("RollViewPager","Minus");count--;if (count<1)count=1;notifyDataSetChanged();}@Overridepublic View getView(ViewGroup container, int position) {ImageView view = new ImageView(container.getContext());view.setImageResource(imgs[position]);view.setScaleType(ImageView.ScaleType.CENTER_CROP);view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));return view;}@Overridepublic int getCount() {return count;}} }
补充:在使用过程中出现了向左滑动会出现空白页面的现象,以及IllegalStateException的问题,经过尝试找到了以下解决方法:
LoopPagerAdapter中
@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {//container.removeView((View) object);//注掉以后就不会出现空白页面}@Overridepublic Object instantiateItem(ViewGroup container, int position) {int realPosition = position%getRealCount();View itemView = findViewByPosition(container,realPosition);<span style="color:#ff0000;"> ViewGroup p = (ViewGroup) itemView.getParent();<span style="white-space:pre"> </span>//防止出现IllegalStateException异常if (p != null) {p.removeView(itemView);}<span style="white-space:pre"> </span></span>container.addView(itemView);return itemView;}
这篇关于android广告轮播图之匀速规律播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!