這篇文章主要介紹Android如何自定義Banner輪播效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
比如網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司自2013年起到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
具體內(nèi)容如下
自定義View布局
自定義View主體內(nèi)容
public class CustomBanner extends FrameLayout { @BindView(R.id.banner_view_pager) ViewPager bannerViewPager; @BindView(R.id.linear_bannner) LinearLayout linearBannner; private Listlist; private int time = 2; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { int currentItem = bannerViewPager.getCurrentItem(); bannerViewPager.setCurrentItem(currentItem + 1); //再次發(fā)送 sendEmptyMessageDelayed(0, time * 1000); } } }; private List listDoc; private OnClickLisner onClickLisner; public CustomBanner(@NonNull Context context) { super(context); init(); } public CustomBanner(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public CustomBanner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * 初始化 */ private void init() { View view = View.inflate(getContext(), R.layout.bannner_layout, this); ButterKnife.bind(this, view); } /** * 對外提供設(shè)置image路徑的方法 */ public void setImageUrls(List list) { this.list = list; if (list == null) { return; } //設(shè)置適配器 LunBoAdapter lunBoAdapter = new LunBoAdapter(getContext(), list); bannerViewPager.setAdapter(lunBoAdapter); initDoc(); //顯示中間某個位置 bannerViewPager.setCurrentItem(list.size() * 10000); //使用handler自動輪播 handler.sendEmptyMessageDelayed(0, time * 1000); //狀態(tài)改變的監(jiān)聽事件 bannerViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //在選中某一頁的時候,切換小圓點的背景 for (int i = 0; i < listDoc.size(); i++) { if (position % listDoc.size() == i) { listDoc.get(i).setBackgroundResource(R.drawable.shape_01); } else { listDoc.get(i).setBackgroundResource(R.drawable.shape_02); } } } @Override public void onPageScrollStateChanged(int state) { } }); } /** * 初始化小圓點 */ private void initDoc() { //創(chuàng)建一個集合,記錄這些小圓點 listDoc = new ArrayList<>(); //清空布局 linearBannner.removeAllViews(); for (int i = 0; i < list.size(); i++) { ImageView docImage = new ImageView(getContext()); if (i == 0) { docImage.setBackgroundResource(R.drawable.shape_01); } else { docImage.setBackgroundResource(R.drawable.shape_02); } //添加到集合 listDoc.add(docImage); //添加到線性布局 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(5, 0, 5, 0); linearBannner.addView(docImage, params); } } /** * 對外提供輪播的時間 */ public void setTimeSecond(int time) { this.time = time; } /** * 點擊事件 * * @param onClickLisner */ public void setClickListner(OnClickLisner onClickLisner) { this.onClickLisner = onClickLisner; } private class LunBoAdapter extends PagerAdapter { private List list; private Context context; public LunBoAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, final int position) { //創(chuàng)建imageView ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_XY); //加載這張圖片 Glide.with(context).load(list.get(position % list.size())).into(imageView); //點擊事件 imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { //觸發(fā) onClickLisner.onItemClick(position % list.size()); } }); //觸摸事件 imageView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: //取消handler身上的消息和回調(diào) handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_MOVE: handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_CANCEL: handler.sendEmptyMessageDelayed(0, time * 1000); break; case MotionEvent.ACTION_UP: handler.sendEmptyMessageDelayed(0, time * 1000); break; } return false; } }); //添加到容器 container.addView(imageView); //返回 return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } public interface OnClickLisner { void onItemClick(int position); } }
自定義小圓點
public class CountView extends View implements View.OnClickListener { private int count = 0; public CountView(Context context) { super(context); init(); } public CountView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public CountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } //初始化的方法 private void init() { this.setOnClickListener(this); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); //圓 canvas.drawCircle(300,300,200,paint); paint.setColor(Color.BLACK); paint.setTextSize(100); String text = String.valueOf(count); //拿到文本的寬度和高度 Rect rect = new Rect(); paint.getTextBounds(text,0,text.length(),rect); canvas.drawText(text,300-rect.width()/2,300+rect.height()/2,paint); } @Override public void onClick(View view) { count ++; //重新繪制 postInvalidate(); } }
小圓點shape
在Main中運行
public class MainActivity extends AppCompatActivity { private CustomBanner customBanner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); customBanner = findViewById(R.id.custom_banner); getDataFromNet(); } private void getDataFromNet() { OkHttpUtil.doGet("https://www.zhaoapi.cn/ad/getAd", new Callback() { private Listlist; @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ String json = response.body().string(); final HomeBean detalBean = new Gson().fromJson(json,HomeBean.class); list = new ArrayList<>(); List data = detalBean.getData(); for (int i = 0; i < data.size(); i++) { String icon = data.get(i).getIcon(); list.add(icon); } runOnUiThread(new Runnable() { @Override public void run() { //設(shè)置時間 customBanner.setTimeSecond(5); //設(shè)置顯示輪播 customBanner.setImageUrls(list); //banner的點擊跳轉(zhuǎn)詳情頁面的事件 banner.setOnBannerListener(new OnBannerListener() { @Override public void OnBannerClick(int position) { List datab = shouBean.getData(); if (datab.get(position).getType() == 0) { Intent intent = new Intent(getActivity(), WebViewActivity.class); intent.putExtra("databurl", datab.get(position).getUrl()); startActivity(intent); } else { Toast.makeText(getContext(), "即將跳轉(zhuǎn)到商品詳情頁面", Toast.LENGTH_SHORT).show(); } } }); } }); } } }); } }
WebView頁面
public class WebViewActivity extends AppCompatActivity { private WebView web_view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); web_view = findViewById(R.id.web_view); String databurl = getIntent().getStringExtra("databurl"); web_view.loadUrl(databurl); //webview一系列設(shè)置 web_view.setWebViewClient(new WebViewClient());//在當(dāng)前應(yīng)用打開,而不是去瀏覽器 WebSettings settings = web_view.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); } }
以上是“Android如何自定義Banner輪播效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!