Android利用ViewPager实现滑动广告板实例源码

时间:2021-12-17 18:48:15 

•android-support-v4.jar,这是谷歌官方给我们提供的一个兼容低版本Android设备的软件包,里面包囊了只有在Android3.0以上可以使用的api。而ViewPager就是其中之一,利用它我们可以做很多事情,从最简单的导航,到页面切换菜单等等。

•ViewPager的功能就是可以使视图滑动,就像Lanucher左右滑动那样。
•本Demo向大家演示ViewPager的使用,并在用户未滑动View时,每隔5s钟自动切换到下一个View(循环切换),而当用户有Touch到View时取消5s自动切换功能。并有指示器指示当前处于哪一个View。

下面是运行的截图:
Android利用ViewPager实现滑动广告板实例源码 
这个是非常常见的应用
下面给出实现的代码:
1.源文件


package com.eyeandroid.viewpager;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
@SuppressLint("HandlerLeak")
public class MainActivity extends Activity {
private ImageView[] imageViews = null;
private ImageView imageView = null;
private ViewPager advPager = null;
private AtomicInteger what = new AtomicInteger(0);
private boolean isContinue = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initViewPager();
}

private void initViewPager() {
advPager = (ViewPager) findViewById(R.id.adv_pager);
ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
// 这里存放的是四张广告背景
List<View> advPics = new ArrayList<View>();
ImageView img1 = new ImageView(this);
img1.setBackgroundResource(R.drawable.advertising_default_1);
advPics.add(img1);
ImageView img2 = new ImageView(this);
img2.setBackgroundResource(R.drawable.advertising_default_2);
advPics.add(img2);
ImageView img3 = new ImageView(this);
img3.setBackgroundResource(R.drawable.advertising_default_3);
advPics.add(img3);
ImageView img4 = new ImageView(this);
img4.setBackgroundResource(R.drawable.advertising_default);
advPics.add(img4);

// 对imageviews进行填充
imageViews = new ImageView[advPics.size()];
//小图标
for (int i = 0; i < advPics.size(); i++) {
imageView = new ImageView(this);
imageView.setLayoutParams(new LayoutParams(20, 20));
imageView.setPadding(5, 5, 5, 5);
imageViews[i] = imageView;
if (i == 0) {
imageViews[i]
.setBackgroundResource(R.drawable.banner_dian_focus);
} else {
imageViews[i]
.setBackgroundResource(R.drawable.banner_dian_blur);
}
group.addView(imageViews[i]);
}
advPager.setAdapter(new AdvAdapter(advPics));
advPager.setOnPageChangeListener(new GuidePageChangeListener());
advPager.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
isContinue = false;
break;
case MotionEvent.ACTION_UP:
isContinue = true;
break;
default:
isContinue = true;
break;
}
return false;
}
});
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (isContinue) {
viewHandler.sendEmptyMessage(what.get());
whatOption();
}
}
}
}).start();
}

private void whatOption() {
what.incrementAndGet();
if (what.get() > imageViews.length - 1) {
what.getAndAdd(-4);
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {

}
}
private final Handler viewHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
advPager.setCurrentItem(msg.what);
super.handleMessage(msg);
}
};
private final class GuidePageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
what.getAndSet(arg0);
for (int i = 0; i < imageViews.length; i++) {
imageViews[arg0]
.setBackgroundResource(R.drawable.banner_dian_focus);
if (arg0 != i) {
imageViews[i]
.setBackgroundResource(R.drawable.banner_dian_blur);
}
}
}
}
private final class AdvAdapter extends PagerAdapter {
private List<View> views = null;
public AdvAdapter(List<View> views) {
this.views = views;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(views.get(arg1));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
return views.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(views.get(arg1), 0);
return views.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
}


2.布局文件
这个文件在屏幕上的图片显示具有至关重要的作用


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- ViewPager是一个控件 -->
<android.support.v4.view.ViewPager
android:id="@+id/adv_pager"
android:layout_width="fill_parent"
android:layout_height="160dp" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/viewGroup"
android:layout_below="@id/adv_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-25px"
android:gravity="right"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</LinearLayout>
标签:ViewPager,滑动广告
0
投稿

猜你喜欢

  • SpringBoot整合Elasticsearch并实现CRUD操作

    2021-10-28 07:27:31
  • UnityShader使用速度映射图实现运动模糊

    2021-11-23 03:53:21
  • Spring boot整合log4j2过程解析

    2023-11-29 10:47:53
  • Mybatis批量插入Oracle数据的方法实例

    2021-05-24 23:32:31
  • C#中使用反射遍历一个对象属性及值的小技巧

    2021-12-10 18:15:43
  • 时间处理函数工具分享(时间戳计算)

    2021-07-24 05:06:18
  • C#创建Excel多级分组的方法

    2022-03-21 15:20:34
  • springboot集成swagger3与knife4j的详细代码

    2023-11-27 18:22:58
  • Android中RecyclerView实现分页滚动的方法详解

    2023-11-13 13:59:53
  • Android 中Banner的使用详解

    2023-08-18 08:57:02
  • 必须了解的高阶JAVA枚举特性!

    2021-11-04 11:26:00
  • java使用listIterator逆序arraylist示例分享

    2022-06-16 05:33:10
  • Java数据结构之链表、栈、队列、树的实现方法示例

    2021-10-07 10:40:29
  • SpringBoot参数校验Validator框架详解

    2023-09-22 07:08:40
  • C/C++根据年月日计算星期几(蔡勒公式篇)

    2023-12-05 00:48:16
  • Android使用DrawerLayout实现仿QQ双向侧滑菜单

    2023-07-18 23:48:24
  • java多线程创建及线程安全详解

    2022-12-02 18:58:51
  • java.net.SocketException: Connection reset 解决方法

    2023-06-17 01:28:43
  • Java Base64算法实际应用之邮件发送实例分析

    2022-08-08 04:00:04
  • java selenium元素定位大全

    2023-10-23 01:07:42
  • asp之家 软件编程 m.aspxhome.com