Android使用RollViewPager实现轮播图
记录Android中轮播图的学习与使用,供大家参考,具体内容如下
1、build.gradle(:app)文件里面加依赖
implementation 'com.jude:rollviewpager:1.2.9'
2、activity_main.xml
<com.jude.rollviewpager.RollPagerView android:id="@+id/roll_view_pager" android:layout_width="match_parent" android:layout_height="180dp" app:rollviewpager_play_delay="3000" />
3、MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.jude.rollviewpager.RollPagerView; import com.jude.rollviewpager.adapter.StaticPagerAdapter; import com.jude.rollviewpager.hintview.ColorPointHintView; public class MainActivity extends AppCompatActivity { private RollPagerView mRollPagerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRollPagerView=findViewById(R.id.roll_view_pager); //设置播放时间间隔 mRollPagerView.setPlayDelay(3000); //设置透明度 mRollPagerView.setAnimationDurtion(500); //设置适配器 mRollPagerView.setAdapter(new TestNormalAdapter()); //设置圆点指示器颜色 mRollPagerView.setHintView(new ColorPointHintView(this, Color.YELLOW,Color.WHITE)); } //自定义适配器 private class TestNormalAdapter extends StaticPagerAdapter{ //轮播所需照片 private int[] imgs={ R.drawable.daxuesheng, R.drawable.daxuesheng2, }; @Override public 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; } @Override public int getCount() { return imgs.length; } }; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)