首页 > 代码库 > webview自动循环播放

webview自动循环播放

<?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="com.bigou.tuhuo.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/lunbotu"
        android:layout_marginTop="-10dp"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="180dp" />

    <LinearLayout
        android:id="@+id/points"
        android:layout_marginTop="130dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"/>
</RelativeLayout>
package com.bigou.tuhuo;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bigou.tuhuo.adapters.LunbotuAdapter;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by cunli on 2017/2/11.
 */
public class MainActivity extends AppCompatActivity {
    private Context context = MainActivity.this;
    private ViewPager viewPager;//轮播图组件
    private int currentPage = 0;//轮播图的当前页
    private List<ImageView> images;//首页轮播的图片
    private boolean isRunning = true;
    ImageView image;
    LinearLayout linearLayouPoints;//当前页指示器
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    int curpage = msg.arg1;
                    viewPager.setCurrentItem(curpage);
                    if (curpage > 0) {
                        ((ImageView) linearLayouPoints.getChildAt(curpage - 1)).setImageResource(R.drawable.point_normal);
                    } else if (curpage == 0) {
                        ((ImageView) linearLayouPoints.getChildAt(images.size() - 1)).setImageResource(R.drawable.point_normal);
                    }
                    ((ImageView) linearLayouPoints.getChildAt(curpage)).setImageResource(R.drawable.point_check);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
    }

    //初始化界面布局
    private void initUI() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        viewPager = (ViewPager) findViewById(R.id.lunbotu);

//        首页轮播的图片
        images = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            String url = "http://192.168.191.1:8080/b" + (i + 1) + ".jpg";
            image = new ImageView(context);
            Picasso.with(context).load(url).into(image);
            images.add(image);
        }
        linearLayouPoints = (LinearLayout) findViewById(R.id.points);
        for (int i = 0; i < images.size(); i++) {
            ImageView iv = (ImageView) LayoutInflater.from(context).inflate(R.layout.point, null);
            iv.setImageResource(R.drawable.point_normal);
            linearLayouPoints.addView(iv);
        }

        viewPager.setAdapter(new LunbotuAdapter(context, images));

        new Thread() {
            int cp = 0;

            @Override
            public void run() {
                super.run();
                while (isRunning) {
                    Message message = Message.obtain();
                    message.what = 1;
                    if (cp == images.size()) {
                        cp = 0;
                    }
                    message.arg1 = cp;
                    handler.sendMessage(message);
                    cp++;
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }.start();


    }

    @Override
    protected void onDestroy() {
        isRunning = false;
        handler = null;
        super.onDestroy();
        Log.e("MainActivity", "-----------停止线程--------------");
    }
}
package com.bigou.tuhuo.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by cunli on 2017/2/11.
 */
//给首页轮播图填充图片
public class LunbotuAdapter extends PagerAdapter {
    private Context context;
    private List<ImageView> images;

    public LunbotuAdapter(Context context, List<ImageView> images) {
        this.context = context;
        this.images = images;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView linearLayout = images.get(position);
        container.addView(linearLayout);
        return linearLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(images.get(position));
    }
}

 lunbotu.xml

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

point.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingEnd="5dp"/>

当前显示页圆点:point_check.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp" />
    <solid android:color="@color/colorPointCheck" />
    <size
        android:width="10dp"
        android:height="10dp" />
</shape>

默认圆点:point_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp" />
    <solid android:color="@color/colorPointNomal" />
    <size
        android:width="10dp"
        android:height="10dp" />
</shape>

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorPointCheck">#FF4081</color>
    <color name="colorPointNomal">#55fffaff</color>
</resources>

 

webview自动循环播放