这篇文章将为大家详细讲解有关Android如何实现ViewPage轮播图效果,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
首先当然是新建一个项目代码如下:
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.yzp.viewpagedemo.MainActivity" android:background="#f6f6f6"> <!--轮播图--> <RelativeLayout android:id="@+id/topPanel_vpg" android:layout_width="match_parent" android:layout_height="190dp" > <android.support.v4.view.ViewPager android:id="@+id/ViewPage_Detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:flipInterval="30" android:persistentDrawingCache="animation" /> <LinearLayout android:id="@+id/point_detail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" > </LinearLayout> </RelativeLayout> </RelativeLayout>MainActivity
package com.yzp.viewpagedemo; import android.content.Context; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ViewPager ViewPage_Detail; private LinearLayout point_detail; private Context context; private View view; //轮播图图片资源 private final int[] viewpage_images = {R.mipmap.two, R.mipmap.three, R.mipmap.four, R.mipmap.fifve}; private ArrayList<ImageView> viewpage_imageList; //判断是否自动滚动ViewPage private boolean isRunning = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Instantiation(); } public void Instantiation() { ViewPage_Detail = (ViewPager)findViewById(R.id.ViewPage_Detail); point_detail = (LinearLayout)findViewById(R.id.point_detail); //初始化图片资源 viewpage_imageList = new ArrayList<ImageView>(); for (int i : viewpage_images) { // 初始化图片资源 ImageView imageView = new ImageView(this); imageView.setBackgroundResource(i); viewpage_imageList.add(imageView); // 添加指示小点 ImageView point = new ImageView(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15); params.rightMargin = 10; params.bottomMargin = 15; point.setLayoutParams(params); point.setBackgroundResource(R.mipmap.light_blue_point); if (i == R.mipmap.two) { //默认聚焦在第一张 point.setBackgroundResource(R.mipmap.blue_point); point.setEnabled(true); } else { point.setEnabled(false); } point_detail.addView(point); } //首页轮播 CarouselShow carouselShow = new CarouselShow(context, viewpage_imageList); carouselShow.CarouselShow_Info_Detail(this); handler.sendEmptyMessageDelayed(0, 3000); } private Handler handler = new Handler() { public void handleMessage(Message msg) { // 执行滑动到下一个页面 ViewPage_Detail.setCurrentItem(ViewPage_Detail.getCurrentItem() + 1); if (isRunning) { // 在发一个handler延时 handler.sendEmptyMessageDelayed(0, 3000); } } }; @Override public void onDestroy() { // 停止滚动 isRunning = false; super.onDestroy(); } }郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。