Android中的大图无限自动轮播

本文介绍了在Android应用中实现大图无限自动轮播的原理和步骤,包括设置ViewPager的最大页面值实现无限轮播,使用Handler发送消息实现自动轮播。详细讲解了布局文件的设置、适配器的编写以及滑动监听的实现,提供了MainActivity的完整代码示例。
摘要由CSDN通过智能技术生成

大图轮播是Android应用中常用的功能,例如网易新闻.总体上的需求分为两个方面:1.无限轮播.2.自动轮播

下面我们来分析以下原理:

无限轮播:将ViewPager的最大的页面值设为Integer.MAX_VALUE,当前的页面值设为Integer.MAX_VALUE/2,如此一来无论用户往左还是往右滑动图片都不可能滑到尽头,这就实现了无限轮播

自动轮播:利用handler发送一个空消息,在handleMessage中将ViewPager的当前页面值+1,在handleMessage中再发送一个延时空消息,循环往复,实现了自动轮播.

1.布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


<!--ViewPager-->
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0ff"
        android:id="@+id/vp"/>


    <!--底部圆点容器-->
    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/dot"
        android:orientation="horizontal"
        android:background="#ff0"
        android:gravity="center"/>

</RelativeLayout>

2.MainActivity代码


//初始化布局
private void initView() {
    vp = ((ViewPager) findViewById(R.id.vp));
    vp.setOnPageChangeListener(this);
    dot_view = ((LinearLayout) findViewById(R.id.dot));
}


准备轮播图片和底部圆点

//初始化数据
private void initData() {
    //存放用于轮播的图片
    imageViews = new ArrayList<>();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package xatu.cn.androidtest_one; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.FileNotFoundException; /* 从手机相册选择一幅图片并显示在屏幕上 */ public class MainActivity extends AppCompatActivity { private Button btn_choose; private ImageView img_show; private final int REQUEST_PICTURE_CHOOSE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); event(); } private void init() { btn_choose = findViewById(R.id.btn_choose_picture); img_show = findViewById(R.id.imgview_src); } private void event() { btn_choose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_PICK); startActivityForResult(intent, REQUEST_PICTURE_CHOOSE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK){ Uri uri = data.getData(); Log.e("uri", uri.toString()); ContentResolver cr = this.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); ImageView imageView = (ImageView) findViewById(R.id.imgview_src); /* 将Bitmap设定到ImageView */ imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } } 页面布局代码: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_choose_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择图片" /> <ImageView android:id="@+id/imgview_src" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值