您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“android怎么制作輪播圖組件”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“android怎么制作輪播圖組件”吧!
本文實(shí)例為大家分享了android輪播圖組件的制作方法,供大家參考,具體內(nèi)容如下
BannerLayout
package com.coral3.common_module.components; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; 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 androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.viewpager.widget.PagerAdapter; import androidx.viewpager.widget.ViewPager; import com.coral3.common_module.R; import com.coral3.common_module.utils.LogUtil; import com.coral3.common_module.viewPager.ChildViewPager; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; public class BannerLayout extends LinearLayout { private Context mContext; private View view; private ChildViewPager 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 BannerLayout(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mContext = context; initView(); initListener(); } private void initView(){ view = LayoutInflater.from(mContext).inflate(R.layout.layout_banner, this); group = view.findViewById(R.id.view_indicators); viewPager = view.findViewById(R.id.view_banners); // 動(dòng)態(tài)加入圖片 List<View> listPics = new ArrayList<>(); ImageView img1 = new ImageView(mContext); img1.setBackgroundResource(R.drawable.banner1); listPics.add(img1); ImageView img2 = new ImageView(mContext); img2.setBackgroundResource(R.drawable.banner2); listPics.add(img2); ImageView img3 = new ImageView(mContext); img3.setBackgroundResource(R.drawable.banner3); listPics.add(img3); ImageView img4 = new ImageView(mContext); img4.setBackgroundResource(R.drawable.banner4); listPics.add(img4); ImageView img5 = new ImageView(mContext); img5.setBackgroundResource(R.drawable.banner4); listPics.add(0, img5); ImageView img0 = new ImageView(mContext); img0.setBackgroundResource(R.drawable.banner1); listPics.add(img0); //動(dòng)態(tài)加入指示器 indicators = new ImageView[listPics.size()]; for(int i = 0; i < indicators.length; i++){ indicator = new ImageView(mContext); 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(); // 輪播 new Thread(new Runnable() { @Override public void run() { while (true){ if(isContinue){ handler.sendEmptyMessage(index.get()); whatOption(); } } } }).start(); } private void initListener(){ // 設(shè)置監(jiān)聽(tīng)器 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { LogUtil.d(positionOffset + "-" + positionOffsetPixels); // 無(wú)縫滾動(dòng)均滑 // if(positionOffset == 0.0){ // LogUtil.d(position + ""); // if(position == 5) { // viewPager.setCurrentItem(1, false); // } // if(position == 0) { // viewPager.setCurrentItem(4, false); // } // } } @Override public void onPageSelected(int position) { index.getAndSet(position); if(position == 5) { viewPager.setCurrentItem(1, false); } if(position == 0) { viewPager.setCurrentItem(4, false); } 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) {} }); // 設(shè)置觸摸時(shí)停止定時(shí) 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; } }); } 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)); } } private void whatOption(){ index.incrementAndGet(); if(index.get() > indicators.length - 2){ index.getAndAdd(-4); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }
layout_banner
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.coral3.common_module.viewPager.ChildViewPager android:id="@+id/view_banners" android:layout_width="match_parent" android:layout_height="200dp"/> <LinearLayout android:id="@+id/view_indicators" android:layout_below="@+id/view_banners" android:gravity="center" android:layout_marginTop="-15dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" /> </RelativeLayout> </LinearLayout>
ChildViewPager
package com.coral3.common_module.viewPager; import android.content.Context; import android.graphics.PointF; import android.util.AttributeSet; import android.view.MotionEvent; import androidx.viewpager.widget.ViewPager; public class ChildViewPager extends ViewPager { /** 觸摸時(shí)按下的點(diǎn) **/ PointF downP = new PointF(); /** 觸摸時(shí)當(dāng)前的點(diǎn) **/ PointF curP = new PointF(); public ChildViewPager(Context context) { super(context); } public ChildViewPager(Context context, AttributeSet attrs) { super(context, attrs); } private static final String TAG = "ChildViewpager"; @Override public boolean onTouchEvent(MotionEvent arg0) { //每次進(jìn)行onTouch事件都記錄當(dāng)前的按下的坐標(biāo) if(getChildCount()<=1) { return super.onTouchEvent(arg0); } curP.x = arg0.getX(); curP.y = arg0.getY(); if(arg0.getAction() == MotionEvent.ACTION_DOWN) { //記錄按下時(shí)候的坐標(biāo) //切記不可用 downP = curP ,這樣在改變curP的時(shí)候,downP也會(huì)改變 downP.x = arg0.getX(); downP.y = arg0.getY(); //此句代碼是為了通知他的父ViewPager現(xiàn)在進(jìn)行的是本控件的操作,不要對(duì)我的操作進(jìn)行干擾 getParent().requestDisallowInterceptTouchEvent(true); } if(arg0.getAction() == MotionEvent.ACTION_MOVE){ //此句代碼是為了通知他的父ViewPager現(xiàn)在進(jìn)行的是本控件的操作,不要對(duì)我的操作進(jìn)行干擾 getParent().requestDisallowInterceptTouchEvent(true); } if(arg0.getAction() == MotionEvent.ACTION_UP || arg0.getAction() == MotionEvent.ACTION_CANCEL){ //在up時(shí)判斷是否按下和松手的坐標(biāo)為一個(gè)點(diǎn) //如果是一個(gè)點(diǎn),將執(zhí)行點(diǎn)擊事件,這是我自己寫(xiě)的點(diǎn)擊事件,而不是onclick getParent().requestDisallowInterceptTouchEvent(false); if(downP.x==curP.x && downP.y==curP.y){ return true; } } super.onTouchEvent(arg0); //注意這句不能 return super.onTouchEvent(arg0); 否則觸發(fā)parent滑動(dòng) return true; } }
使用
<com.coral3.common_module.components.BannerLayout android:id="@+id/home_banner" android:layout_width="match_parent" android:layout_height="wrap_content"/>
到此,相信大家對(duì)“android怎么制作輪播圖組件”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。