android 开屏广告设计_android

//经常在安卓app中页面上方放置一个广告栏,用到的无限轮播代码:

public class MainActivity extends Activity {
// 广告控件
private MyPagerGalleryView gallery;
// 圆点容器
private LinearLayout ovalLayout;
// 图片上面的文字
private TextView adgallerytxt;
/** 图片id的数组,本地测试用 */
private int[] imageId = new int[] { R.drawable.img02, R.drawable.img03,
R.drawable.img07 , R.drawable.img06, R.drawable.img09};
private String[] txtViewpager = { "天下美景","人间仙境","绿野仙踪","美轮美奂","醉里梦影"}; 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (MyPagerGalleryView) findViewById(R.id.adgallery); // 获取Gallery组件
ovalLayout = (LinearLayout) findViewById(R.id.ovalLayout1);// 获取圆点容器
adgallerytxt = (TextView) findViewById(R.id.adgallerytxt);
gallery.start(this, imageId, 3000, ovalLayout,
R.drawable.dot_focused, R.drawable.dot_normal, adgallerytxt,
txtViewpager);
gallery.setMyOnItemClickListener(new MyOnItemClickListener() {
public void onItemClick(int curIndex) {
Toast.makeText(MainActivity.this, "点击的图片下标为:" + curIndex,
Toast.LENGTH_SHORT).show();
// System.out.println(curIndex);
}
});

}


@Override
protected void onStop() {
gallery.stopTimer();
super.onStop();
}


@Override
protected void onRestart() {
gallery.startTimer();
super.onRestart();
}


 }//主函数布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/llayout"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="#FFFFFF"
     android:orientation="vertical" >


     <ScrollView
         android:layout_width="match_parent"
         android:layout_height="match_parent" >


         <RelativeLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >


             <LinearLayout
                 android:id="@+id/qweqweqweqwe"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="vertical" >


                 <com.example.guanggaolan.MyPagerGalleryView
                     android:id="@+id/adgallery"
                     android:layout_width="match_parent"
                     android:layout_height="200dip"
                     android:background="#FFFFFF"
                     android:focusable="true"
                     android:focusableInTouchMode="true" />
             </LinearLayout>


             <RelativeLayout 
                android:layout_width="match_parent"
                 android:layout_height="60dip"
                 android:layout_alignBottom="@+id/qweqweqweqwe"
                 android:background="@drawable/progresslayout_bg" 
                 >
             <TextView
                 android:id="@+id/adgallerytxt"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_centerVertical="true"
                 android:layout_marginLeft="20dip"                
                 android:textColor="#cecece"
                 android:textSize="18sp"
                 android:text="广告" />
</RelativeLayout>
             <LinearLayout
                 android:id="@+id/ovalLayout1"
                 android:layout_width="match_parent"
                 android:layout_height="10dip"
                 android:layout_below="@+id/qweqweqweqwe"
                 android:background="#00FFFFFF"
                 android:gravity="center"
                 android:orientation="horizontal" >
             </LinearLayout>
         </RelativeLayout>
     </ScrollView>


 </LinearLayout>//需要的工具类:
/**
  * 无限滚动广告栏组件
  */
 @SuppressWarnings("deprecation")
 public class MyPagerGalleryView extends Gallery implements
android.widget.AdapterView.OnItemClickListener,
android.widget.AdapterView.OnItemSelectedListener, OnTouchListener {
/** 显示的Activity */
private Context mContext;
/** 条目单击事件接口 */
private MyOnItemClickListener mMyOnItemClickListener;
/** 图片切换时间 */
private int mSwitchTime;
/** 自动滚动的定时器 */
private Timer mTimer;
/** 圆点容器 */
private LinearLayout mOvalLayout;
/** 当前选中的数组索引 */
private int curIndex = 0;
/** 上次选中的数组索引 */
private int oldIndex = 0;
/** 圆点选中时的背景ID */
private int mFocusedId;
/** 圆点正常时的背景ID */
private int mNormalId;
/** 图片资源ID组 */
private int[] mAdsId;

/** ImageView组 */
private List<ImageView> listImgs;
/** 广告条上面textView控件 */
private TextView adgallerytxt;
/** 广告条上的每一条文字的数组 */
private String[] txtViewpager;


public MyPagerGalleryView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}


public MyPagerGalleryView(Context context) {
super(context);
}


public MyPagerGalleryView(Context context, AttributeSet attrs) {
super(context, attrs);
}


/**
* @param context
*            显示的Activity ,不能为null 
* @param adsId
*            图片组资源ID ,测试用
* @param switchTime
*            图片切换时间 写0 为不自动切换
* @param ovalLayout
*            圆点容器 ,可为空
* @param focusedId
*            圆点选中时的背景ID,圆点容器可为空写0
* @param normalId
*            圆点正常时的背景ID,圆点容器为空写0
* @param adgallerytxt
*            广告条上面textView控件
* @param txtViewpager
*            广告条上的每一条文字的数组
* 
*/
public void start(Context context, int[] adsId,
int switchTime, LinearLayout ovalLayout, int focusedId,
int normalId, TextView adgallerytxt, String[] txtViewpager) {
this.mContext = context; 
this.mAdsId = adsId;
this.mSwitchTime = switchTime;
this.mOvalLayout = ovalLayout;
this.mFocusedId = focusedId;
this.mNormalId = normalId;
this.adgallerytxt = adgallerytxt;
this.txtViewpager = txtViewpager;


ininImages();// 初始化图片组
setAdapter(new AdAdapter());
this.setOnItemClickListener(this);
this.setOnTouchListener(this);
this.setOnItemSelectedListener(this); 
setSpacing(0); 
setFocusableInTouchMode(true);
initOvalLayout();// 初始化圆点
startTimer();// 开始自动滚动任务
}


/** 初始化图片组 */
private void ininImages() {
listImgs = new ArrayList<ImageView>(); // 图片组
int len =mAdsId.length;
for (int i = 0; i < len; i++) {
ImageView imageview = new ImageView(mContext); // 实例化ImageView的对象
imageview.setScaleType(ImageView.ScaleType.FIT_XY); // 设置缩放方式
imageview.setLayoutParams(new Gallery.LayoutParams(
Gallery.LayoutParams.MATCH_PARENT,
Gallery.LayoutParams.MATCH_PARENT));
// 本地加载图片
imageview.setImageResource(mAdsId[i]); // 为ImageView设置要显示的图片 
listImgs.add(imageview);
}


}


/** 初始化圆点 */
private void initOvalLayout() {


if (mOvalLayout != null && listImgs.size() < 2) {// 如果只有一第图时不显示圆点容器
mOvalLayout.removeAllViews();
mOvalLayout.getLayoutParams().height = 0;
} else if (mOvalLayout != null) {
mOvalLayout.removeAllViews();
// 圆点的大小是 圆点窗口的 70%;
int Ovalheight = (int) (mOvalLayout.getLayoutParams().height * 0.7);
// 圆点的左右外边距是 圆点窗口的 20%;
int Ovalmargin = (int) (mOvalLayout.getLayoutParams().height * 0.2);
android.widget.LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
Ovalheight, Ovalheight);
layoutParams.setMargins(Ovalmargin, 0, Ovalmargin, 0);
for (int i = 0; i < listImgs.size(); i++) {
View v = new View(mContext); // 员点
v.setLayoutParams(layoutParams);
v.setBackgroundResource(mNormalId);
mOvalLayout.addView(v);
}
// 选中第一个
mOvalLayout.getChildAt(0).setBackgroundResource(mFocusedId);
}
}


/** 无限循环适配器 */
class AdAdapter extends BaseAdapter {
@Override
public int getCount() {
if (listImgs.size() < 2)// 如果只有一张图时不滚动
return listImgs.size();
return Integer.MAX_VALUE;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
return listImgs.get(position % listImgs.size()); // 返回ImageView
}


@Override
public Object getItem(int position) {
return null;
}


@Override
public long getItemId(int position) {
return position;
}
}


public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int kEvent;
if (isScrollingLeft(e1, e2)) { // 检查是否往左滑动
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else { // 检查是否往右滑动
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;


}


/** 检查是否往左滑动 */
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > (e1.getX() + 50);
}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return super.onScroll(e1, e2, distanceX, distanceY);
}


@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()
|| MotionEvent.ACTION_CANCEL == event.getAction()) {
startTimer();// 开始自动滚动任务
} else {
stopTimer();// 停止自动滚动任务
}
return false;
}


/** 图片切换事件 */
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
curIndex = position % listImgs.size();
if (mOvalLayout != null && listImgs.size() > 1) { // 切换圆点
mOvalLayout.getChildAt(oldIndex).setBackgroundResource(mNormalId); // 圆点取消
mOvalLayout.getChildAt(curIndex).setBackgroundResource(mFocusedId);// 圆点选中
adgallerytxt.setText("" + txtViewpager[curIndex]);
oldIndex = curIndex;
}
// adgallerytxt.setText("" + curIndex);
}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
}


/** 项目点击事件 */
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
if (mMyOnItemClickListener != null) {
mMyOnItemClickListener.onItemClick(curIndex);
}
}


/** 设置项目点击事件监听器 */
public void setMyOnItemClickListener(MyOnItemClickListener listener) {
mMyOnItemClickListener = listener;
}


/** 项目点击事件监听器接口 */
public interface MyOnItemClickListener {
/**
* @param curIndex
*            //当前条目在数组中的下标
*/
void onItemClick(int curIndex);
}


/** 停止自动滚动任务 */
public void stopTimer() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}


/** 开始自动滚动任务 图片大于1张才滚动 */
public void startTimer() {
if (mTimer == null && listImgs.size() > 1 && mSwitchTime > 0) {
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
public void run() {
handler.sendMessage(handler.obtainMessage(1));
}
}, mSwitchTime, mSwitchTime);
}
}


/** 处理定时滚动任务 */
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 不包含spacing会导致onKeyDown()失效!!!
// 失效onKeyDown()前先调用onScroll(null,1,0)可处理
onScroll(null, null, 1, 0);
onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
};
 }