安卓广告图轮播banner

广告轮播图,能够轮询播放图片。导航点有点不明显,修改下布局透明度就可以了。

这里写图片描述

这里写图片描述

这里写图片描述
布局

<code class="hljs xml has-numbering"><span class="hljs-pi"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag"><<span class="hljs-title">LinearLayout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attribute">xmlns:tools</span>=<span class="hljs-value">"http://schemas.android.com/tools"</span>
    <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span>
    <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span>
    <span class="hljs-attribute">android:orientation</span>=<span class="hljs-value">"vertical"</span>
    <span class="hljs-attribute">tools:context</span>=<span class="hljs-value">"teaen.com.example.cgx_pc.autoplay_forcsdn.MainActivity"</span>></span>

<span class="hljs-tag"><<span class="hljs-title">RelativeLayout
</span>    <span class="hljs-attribute">android:layout_marginTop</span>=<span class="hljs-value">"10dp"</span>
    <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span>
    <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"200dp"</span>></span>

    <span class="hljs-tag"><<span class="hljs-title">android.support.v4.view.ViewPager
</span>        <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/viewPager"</span>
        <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span>
        <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">android.support.v4.view.ViewPager</span>></span>

   <span class="hljs-comment"><!--导航点容器--></span>
    <span class="hljs-tag"><<span class="hljs-title">LinearLayout
</span>        <span class="hljs-attribute">android:orientation</span>=<span class="hljs-value">"horizontal"</span>
        <span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@+id/dot_container"</span>
        <span class="hljs-attribute">android:background</span>=<span class="hljs-value">"#20c0c0c0"</span>
        <span class="hljs-attribute">android:gravity</span>=<span class="hljs-value">"center"</span>
        <span class="hljs-attribute">android:layout_alignParentBottom</span>=<span class="hljs-value">"true"</span>
        <span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span>
        <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"40dp"</span>></span>
    <span class="hljs-tag"></<span class="hljs-title">LinearLayout</span>></span>

<span class="hljs-tag"></<span class="hljs-title">RelativeLayout</span>></span>

<span class="hljs-tag"></<span class="hljs-title">LinearLayout</span>></span>
</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li></ul>

pagerAdapter

<code class="hljs java has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAdpater</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">PagerAdapter</span> {</span>
    <span class="hljs-keyword">private</span> List<ImageView> list;


    <span class="hljs-keyword">public</span> <span class="hljs-title">MyAdpater</span>(List<ImageView> list){
       <span class="hljs-keyword">this</span>.list=list;
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getCount</span>() {
        <span class="hljs-keyword">return</span> list.size();
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isViewFromObject</span>(View view, Object object) {
        <span class="hljs-keyword">return</span> view==object;
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> Object <span class="hljs-title">instantiateItem</span>(ViewGroup container, <span class="hljs-keyword">int</span> position) {
        container.addView(list.get(position));
        <span class="hljs-keyword">return</span> list.get(position);
    }

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroyItem</span>(ViewGroup container, <span class="hljs-keyword">int</span> position, Object object) {
        container.removeView((View) object);
    }
}
</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li></ul>

main

<code class="hljs java has-numbering"><span class="hljs-keyword">import</span> android.os.SystemClock;
<span class="hljs-keyword">import</span> android.support.v4.view.ViewPager;
<span class="hljs-keyword">import</span> android.support.v7.app.AppCompatActivity;
<span class="hljs-keyword">import</span> android.os.Bundle;
<span class="hljs-keyword">import</span> android.widget.ImageView;
<span class="hljs-keyword">import</span> android.widget.LinearLayout;
<span class="hljs-keyword">import</span> java.util.ArrayList;
<span class="hljs-keyword">import</span> java.util.List;


<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AppCompatActivity</span>  {</span>

    <span class="hljs-keyword">private</span> ViewPager viewPager;
    <span class="hljs-keyword">private</span> LinearLayout container;
    <span class="hljs-keyword">private</span> ArrayList<ImageView> list;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span>[] picSource={R.mipmap.another_girl,R.mipmap.gao_yuanyuan,R.mipmap.liu_shishi};
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> currentNum;

    <span class="hljs-annotation">@Override</span>
    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        container = (LinearLayout) findViewById(R.id.dot_container);
        <span class="hljs-comment">//给viewpager设置适配器</span>
        MyAdpater adpater = <span class="hljs-keyword">new</span> MyAdpater(getList());
        viewPager.setAdapter(adpater);
        <span class="hljs-comment">//初始化导航点</span>
        initDotImagView();
        <span class="hljs-comment">//给Viewpager设置监听</span>
        viewPager.addOnPageChangeListener(<span class="hljs-keyword">new</span> ViewPager.SimpleOnPageChangeListener(){
            <span class="hljs-annotation">@Override</span>
            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onPageSelected</span>(<span class="hljs-keyword">int</span> position) {
                currentNum=position;
                <span class="hljs-comment">//page改变后重新刷新dotImageView</span>
                <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i <container.getChildCount() ; i++) {
                    ImageView dot = (ImageView) container.getChildAt(i);
                    dot.setImageResource(R.mipmap.page);
                    <span class="hljs-keyword">if</span>(i==position)
                        dot.setImageResource(R.mipmap.page_now);
                }
            }
        });
        <span class="hljs-comment">//自动播放</span>
        initAuotPlay();
    }

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initDotImagView</span>() {
        <span class="hljs-comment">//获取布局属性</span>
        LinearLayout.LayoutParams params = <span class="hljs-keyword">new</span> LinearLayout.LayoutParams(-<span class="hljs-number">2</span>, -<span class="hljs-number">2</span>);
        params.leftMargin=<span class="hljs-number">20</span>;
        params.rightMargin=<span class="hljs-number">20</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i <picSource.length ; i++) {
            ImageView imageView = <span class="hljs-keyword">new</span> ImageView(<span class="hljs-keyword">this</span>);
            imageView.setImageResource(R.mipmap.page);
            <span class="hljs-comment">//第一张图</span>
            <span class="hljs-keyword">if</span>(i==<span class="hljs-number">0</span>)
                imageView.setImageResource(R.mipmap.page_now);
            imageView.setLayoutParams(params);
            container.addView(imageView);
        }
    }


    <span class="hljs-comment">//返回List<ImageView></span>
    <span class="hljs-keyword">private</span> List<ImageView> <span class="hljs-title">getList</span>() {
        list = <span class="hljs-keyword">new</span> ArrayList<>();
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i <picSource.length ; i++) {
            ImageView imageView = <span class="hljs-keyword">new</span> ImageView(<span class="hljs-keyword">this</span>);
            imageView.setImageResource(picSource[i]);
            list.add(imageView);
        }
     <span class="hljs-keyword">return</span> list;

    }


    <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initAuotPlay</span>() {
       <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> Runnable() {
           <span class="hljs-annotation">@Override</span>
           <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span>() {
          <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>){
              viewPager.post(<span class="hljs-keyword">new</span> Runnable() {
                  <span class="hljs-annotation">@Override</span>
                  <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span>() {
                      viewPager.setCurrentItem(currentNum);
                  }
              });
              <span class="hljs-comment">//播放间隔3s</span>
              SystemClock.sleep(<span class="hljs-number">3000</span>);
              currentNum=(currentNum+<span class="hljs-number">1</span>)%picSource.length;
          }}
       }).start();
    }
}
</code>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值