直播视频app源码,android无缝滚动轮播图
直播视频app源码,android无缝滚动轮播图实现的相关代码
```cpp package com.coral3.common_module.ui.fragment; import android.app.Activity; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import com.coral3.common_module.R; import com.coral3.common_module.utils.LogUtil; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import static com.coral3.common_module.R.layout.fragment_banner; /** * @description 制作无缝滚动轮播图 * @author WTY * @date 2021/7/16/11/05/63 */ public class BannerFragment extends Fragment { private Activity mActivity; private ViewPager viewPager; private ImageView indicator; private ImageView[] indicators; private Boolean isContinue = true; private ViewGroup group; private AtomicInteger index = new AtomicInteger(); private Handler handler = new Handler(new Handler.Callback(){ @Override public boolean handleMessage(Message message) { viewPager.setCurrentItem(message.what); return false; } }); public BannerFragment() {} public static BannerFragment newInstance() { BannerFragment fragment = new BannerFragment(); return fragment; } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mActivity = getActivity(); initViews(); } public void initViews(){ group = mActivity.findViewById(R.id.view_indicators); viewPager = getActivity().findViewById(R.id.view_banners); // 动态加入图片 List<View> listPics = new ArrayList<>(); ImageView img1 = new ImageView(mActivity); img1.setBackgroundResource(R.drawable.banner1); listPics.add(img1); ImageView img2 = new ImageView(mActivity); img2.setBackgroundResource(R.drawable.banner2); listPics.add(img2); ImageView img3 = new ImageView(mActivity); img3.setBackgroundResource(R.drawable.banner3); listPics.add(img3); ImageView img4 = new ImageView(mActivity); img4.setBackgroundResource(R.drawable.banner4); listPics.add(img4); ImageView img5 = new ImageView(mActivity); img5.setBackgroundResource(R.drawable.banner4); listPics.add(0, img5); ImageView img0 = new ImageView(mActivity); img0.setBackgroundResource(R.drawable.banner1); listPics.add(img0); //动态加入指示器 indicators = new ImageView[listPics.size()]; for(int i = 0; i < indicators.length; i++){ indicator = new ImageView(mActivity); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(15, 15); layoutParams.setMargins(0, 0, 10, 0); indicator.setLayoutParams(layoutParams); indicators[i] = indicator; if(i == 1){ indicators[i].setBackgroundResource(R.drawable.shape_banner_checked); }else{ indicators[i].setBackgroundResource(R.drawable.shape_banner_unchecked); } if(i == 0 || i == 5){ indicators[i].setVisibility(View.INVISIBLE); } group.addView(indicators[i]); } viewPager.setAdapter(new MyPagerAdapter(listPics)); index.incrementAndGet(); // 设置监听器 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(positionOffset == 0.0){ if(position == 5) { viewPager.setCurrentItem(1, false); } if(position == 0) { viewPager.setCurrentItem(4, false); } } } @Override public void onPageSelected(int position) { LogUtil.d(position + "---"); index.getAndSet(position); for(int i = 0; i < indicators.length; i++){ if(i == index.get()){ indicators[i].setBackgroundResource(R.drawable.shape_banner_checked); }else{ indicators[i].setBackgroundResource(R.drawable.shape_banner_unchecked); } } if(position == 0) indicators[4].setBackgroundResource(R.drawable.shape_banner_checked); if(position == 5) indicators[1].setBackgroundResource(R.drawable.shape_banner_checked); } @Override public void onPageScrollStateChanged(int state) {} }); // 轮播 new Thread(new Runnable() { @Override public void run() { while (true){ if(isContinue){ handler.sendEmptyMessage(index.get()); whatOption(); } } } }).start(); // 设置触摸时停止定时 viewPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()){ case MotionEvent.ACTION_DOWN: isContinue = false; break; case MotionEvent.ACTION_UP: isContinue = true; break; } return false; } }); } private void whatOption(){ index.incrementAndGet(); if(index.get() > indicators.length - 2){ index.getAndAdd(-4); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } class MyPagerAdapter extends PagerAdapter { private List<View> listView; @Override public int getCount() { return listView.size(); } public MyPagerAdapter(List<View> listView){ this.listView = listView; } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { container.addView(listView.get(position)); return listView.get(position); } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView(listView.get(position)); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_banner, container, false); } } ```
以上就是 直播视频app源码,android无缝滚动轮播图实现的相关代码,更多内容欢迎关注之后的文章