自定义类似优酷首页的BannerView幻灯片展示

2023-12-20 15:20

本文主要是介绍自定义类似优酷首页的BannerView幻灯片展示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

   尼玛,最近自定义view玩hight了根本停不下来,今天想封装一个类似优酷首页title的banner栏,我会在这个基础上加一个title的展示,优酷的banner如下图:

        

        首先是布局文件,自然是ViewPager主打,配上底层的dot indicator和title栏:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/rl_main_advertisement"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="150dip" >  
  6.   
  7.     <android.support.v4.view.ViewPager  
  8.         android:id="@+id/vp_banner"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="150dip" />  
  11.   
  12.     <RelativeLayout  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="25dip"  
  15.         android:layout_alignParentBottom="true"  
  16.         android:background="@color/bkg_dot_area_of_banner" >  
  17.   
  18.         <LinearLayout  
  19.             android:id="@+id/ll_banner_dot_area"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:layout_alignParentRight="true"  
  23.             android:layout_centerVertical="true"  
  24.             android:layout_marginRight="10dip"  
  25.             android:orientation="horizontal" >  
  26.   
  27.         </LinearLayout>  
  28.   
  29.         <TextView  
  30.             android:id="@+id/tv_banner_title"  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_centerVertical="true"  
  34.             android:layout_marginLeft="20dip"  
  35.             android:layout_marginRight="10dip"  
  36.             android:layout_toLeftOf="@id/ll_banner_dot_area"  
  37.             android:singleLine="true"  
  38.             android:text="title1"  
  39.             android:textColor="@color/white"  
  40.             android:textSize="15sp" />  
  41.     </RelativeLayout>  
  42.   
  43. </RelativeLayout>  
        中间的线性布局是为了动态添加dot indecator,很简单,下面是代码:

[java]  view plain copy
  1. package com.amuro.banner_view;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.concurrent.Executors;  
  6. import java.util.concurrent.ScheduledExecutorService;  
  7. import java.util.concurrent.TimeUnit;  
  8.   
  9. import com.amuro.bannerviewtest.R;  
  10.   
  11. import android.annotation.SuppressLint;  
  12. import android.content.Context;  
  13. import android.os.Handler;  
  14. import android.os.Message;  
  15. import android.support.v4.view.ViewPager;  
  16. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  17. import android.util.AttributeSet;  
  18. import android.view.LayoutInflater;  
  19. import android.widget.FrameLayout;  
  20. import android.widget.ImageView;  
  21. import android.widget.ImageView.ScaleType;  
  22. import android.widget.LinearLayout;  
  23. import android.widget.TextView;  
  24.   
  25. public class BannerView extends FrameLayout  
  26. {  
  27.     private ViewPager viewPager;  
  28.     private List<ImageView> imageViews;  
  29.     private List<ImageView> imageViewDots;  
  30.     private LinearLayout linearLayoutDotArea;  
  31.     private TextView textViewBanner;  
  32.       
  33.     private ScheduledExecutorService scheduledExecutorService;  
  34.     private int currentItem = 0;  
  35.   
  36.     @SuppressLint("HandlerLeak")   
  37.     private Handler handler = new Handler()  
  38.     {  
  39.         public void handleMessage(Message msg)   
  40.         {  
  41.             viewPager.setCurrentItem(currentItem);  
  42.         }  
  43.     };  
  44.       
  45.     public BannerView(Context context, AttributeSet attrs)  
  46.     {  
  47.         super(context, attrs);  
  48.         LayoutInflater.from(context).inflate(R.layout.banner_view_layout, this);  
  49.         initView();  
  50.     }  
  51.   
  52.     private void initView()  
  53.     {  
  54.         viewPager = (ViewPager)findViewById(R.id.vp_banner);  
  55.         textViewBanner = (TextView)findViewById(R.id.tv_banner_title);  
  56.         linearLayoutDotArea = (LinearLayout)findViewById(R.id.ll_banner_dot_area);  
  57.     }  
  58.       
  59.     public void setView(final int[] imageIds, final String[] titles, int count)  
  60.     {  
  61.         initImageViews(imageIds, count);  
  62.         initImageViewDots(count);  
  63.           
  64.         viewPager.setAdapter(new BannerViewPagerAdapter(imageViews));  
  65.         viewPager.setOnPageChangeListener(new OnPageChangeListener()  
  66.         {  
  67.             private int oldPosition = 0;  
  68.               
  69.             @Override  
  70.             public void onPageSelected(int position)  
  71.             {  
  72.                 imageViewDots.get(position).setImageResource(R.drawable.icon_dot_selected);  
  73.                 imageViewDots.get(oldPosition).setImageResource(R.drawable.icon_dot_unselected);  
  74.                   
  75.                 oldPosition = position;  
  76.                 currentItem = position;  
  77.                   
  78.                 textViewBanner.setText(titles[position]);  
  79.             }  
  80.               
  81.             @Override  
  82.             public void onPageScrolled(int position, float positionOffset,  
  83.                     int positionOffsetPixels)  
  84.             {  
  85.                   
  86.             }  
  87.               
  88.             @Override  
  89.             public void onPageScrollStateChanged(int state)  
  90.             {  
  91.                   
  92.             }  
  93.         });  
  94.     }  
  95.       
  96.     private void initImageViews(int[] imageIds, int count)  
  97.     {  
  98.         imageViews = new ArrayList<ImageView>();  
  99.         for(int i = 0;i < count;i++)  
  100.         {  
  101.             ImageView imageView = new ImageView(getContext());  
  102.             imageView.setImageResource(imageIds[i]);  
  103.             imageView.setScaleType(ScaleType.FIT_XY);  
  104.             imageViews.add(imageView);  
  105.         }  
  106.     }  
  107.       
  108.     private void initImageViewDots(int count)  
  109.     {  
  110.         imageViewDots = new ArrayList<ImageView>();  
  111.           
  112.         for(int i = 0; i < count; i++)  
  113.         {  
  114.             ImageView imageView = new ImageView(getContext());  
  115.             if(i == 0)  
  116.             {  
  117.                 imageView.setImageResource(R.drawable.icon_dot_selected);  
  118.             }  
  119.             else  
  120.             {  
  121.                 android.widget.LinearLayout.LayoutParams params =   
  122.                         new android.widget.LinearLayout.LayoutParams(  
  123.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  124.                 params.setMargins(DisplayUtils.dip2px(getContext(), 5), 000);  
  125.                   
  126.                 imageView.setImageResource(R.drawable.icon_dot_unselected);  
  127.                 imageView.setLayoutParams(params);  
  128.                   
  129.             }  
  130.             linearLayoutDotArea.addView(imageView);  
  131.             imageViewDots.add(imageView);  
  132.         }  
  133.           
  134.     }  
  135.   
  136.     public void startPlay()  
  137.     {  
  138.         scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();    
  139.         scheduledExecutorService.scheduleAtFixedRate(new ViewPagerAutoScrollTask(), 33, TimeUnit.SECONDS);  
  140.     }  
  141.       
  142.     public void stopPlay()  
  143.     {  
  144.         scheduledExecutorService.shutdown();    
  145.     }  
  146.       
  147.     private class ViewPagerAutoScrollTask implements Runnable   
  148.     {    
  149.             
  150.         public void run()   
  151.         {    
  152.             synchronized (viewPager)   
  153.             {    
  154.                 currentItem = (currentItem + 1) % imageViews.size();    
  155.                 handler.obtainMessage().sendToTarget();  
  156.             }    
  157.         }    
  158.     
  159.     }    
  160. }  
        这个BannerView可由调用者自行设置数量,动态添加view,在setView的时候把资源文件传入就可以了。

        好了,今天不装逼了,上一下最终效果图:

        

        啊啊啊啊啊啊啊,炮姐好帅啊……

这篇关于自定义类似优酷首页的BannerView幻灯片展示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/516498

相关文章

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

SpringBoot自定义注解如何解决公共字段填充问题

《SpringBoot自定义注解如何解决公共字段填充问题》本文介绍了在系统开发中,如何使用AOP切面编程实现公共字段自动填充的功能,从而简化代码,通过自定义注解和切面类,可以统一处理创建时间和修改时间... 目录1.1 问题分析1.2 实现思路1.3 代码开发1.3.1 步骤一1.3.2 步骤二1.3.3

dubbo3 filter(过滤器)如何自定义过滤器

《dubbo3filter(过滤器)如何自定义过滤器》dubbo3filter(过滤器)类似于javaweb中的filter和springmvc中的intercaptor,用于在请求发送前或到达前进... 目录dubbo3 filter(过滤器)简介dubbo 过滤器运行时机自定义 filter第一种 @A

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容