注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

姑射道人的博客

博客新地址:nixuchen.com

 
 
 

日志

 
 

ViewFlipper实现广告栏  

2013-02-22 13:35:44|  分类: android |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
package cn.net.brisc.customview;

import java.io.Serializable;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.ViewFlipper;
import cn.net.brisc.expo.golf.AboutActivity;
import cn.net.brisc.expo.golf.M1010DataActivity;
import cn.net.brisc.expo.golf.MainActivity;
import cn.net.brisc.expo.golf.R;
import cn.net.brisc.expo.golf.db.BannerBean;
import cn.net.brisc.expo.golf.db.ExhibitorBean;
import cn.net.brisc.utils.ImageUtils;
import cn.net.brisc.utils.MConfig;
import cn.net.brisc.utils.SearchTool;

/**
 * 这是一个类似于ViewFlipper的Wiget。
 * 
 * @author haozi
 * 
 */
public class ViewFlipperView extends ViewFlipper {

private Context context; // 调用方的上下文
private int currentAdImgIndex=0; // 当前广告图片索引
private Animation left2RightInAnimation; // 广告图片从左到右进入屏幕动画
private Animation left2RightOutAnimation; // 广告图片从左到右出去屏幕动画
private Animation right2LeftInAnimation; // 广告图片从右到左进入屏幕动画
private Animation right2LeftOutAnimation; // 广告图片从右到左出去屏幕动画
private int animationDuration = 500; // 动画花费时间1000毫秒
private ViewFlipper mViewFlipper; // 滑动页面控件
private LinearLayout mTipLinearLayout; // 下方点点控件
private float startX = 0; // touch action down 时的x坐标
private float endX = 0; // touch action up 时的x坐标
private static final String TAG = "ViewFlipperView";
BannerBean bannerBean;
SearchTool searchTool;
List<BannerBean> bannerBeans;
PointF startPointF = new PointF();
PointF endPointF = new PointF();

public ViewFlipperView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;

setView();

}

@Override
public void setFlipInterval(int milliseconds) {

super.setFlipInterval(milliseconds);
}

public void setMyFilpInterval(final int milliseconds) {
Log.i(TAG, "setMyFilpInterval");

final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
showNext();
handler.postDelayed(this, milliseconds);
}
};
handler.postDelayed(runnable, milliseconds);// 每两秒执行一次runnable

}

public ViewFlipperView(Context context, List<BannerBean> bannerBeans) {
super(context);
this.context = context;
this.bannerBeans = bannerBeans;
searchTool = new SearchTool(context);

setView();
setMyFilpInterval(5000);

}

private void resetPointers() {
for (int i = 0; i < MainActivity.imagePointers.size(); i++) {

MainActivity.imagePointers.get(i)
.setBackgroundResource(R.drawable.point_normal);

}
MainActivity.imagePointers.get(currentAdImgIndex)
.setBackgroundResource(R.drawable.point_selected);
}

@Override
public void showNext() {
// TODO Auto-generated method stub

setInAnimation(right2LeftInAnimation);
setOutAnimation(right2LeftOutAnimation);
currentAdImgIndex++;
if (currentAdImgIndex >= getChildCount()) {
currentAdImgIndex = 0;
}
resetPointers();
super.showNext();
}

@Override
public void showPrevious() {
setInAnimation(left2RightInAnimation);
setOutAnimation(left2RightOutAnimation);
currentAdImgIndex--;
if (currentAdImgIndex < 0) {
currentAdImgIndex = 0;
}
resetPointers();
super.showPrevious();
}

/**
* 显示View
*/
private void setView() {

// 初始化
int screenWidth = getResources().getDisplayMetrics().widthPixels;

// mTipLinearLayout = new LinearLayout(context);
// 初始化动画
left2RightInAnimation = new TranslateAnimation(-screenWidth, 0, 0, 0);
left2RightInAnimation.setDuration(animationDuration);
left2RightOutAnimation = new TranslateAnimation(0, screenWidth, 0, 0);
left2RightOutAnimation.setDuration(animationDuration);
right2LeftInAnimation = new TranslateAnimation(screenWidth, 0, 0, 0);
right2LeftInAnimation.setDuration(animationDuration);
right2LeftOutAnimation = new TranslateAnimation(0, -screenWidth, 0, 0);
right2LeftOutAnimation.setDuration(animationDuration);
MainActivity.imagePointers.get(0).setBackgroundResource(R.drawable.point_selected);

}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:

startX = event.getX();
startPointF.x = event.getX();
startPointF.y = event.getY();
Log.i(TAG, "startX:" + startPointF.x + " startY:" + startPointF.y);
break;

case MotionEvent.ACTION_POINTER_UP:

break;
case MotionEvent.ACTION_UP:

endX = event.getX();
endPointF.x = event.getX();
endPointF.y = event.getY();

if (endX > startX) {// 查看前一页的广告

showPrevious();

}

if (endX < startX) {// 查看后一页的广告

showNext();
}

float xx = endPointF.x - startPointF.x;
float yy = endPointF.y - startPointF.y;
float distance = (float) Math.sqrt(Math.abs(yy * yy - xx * xx));
Log.i(TAG, "x1:" + startPointF.x + " y1:" + startPointF.y);
Log.i(TAG, "x2:" + endPointF.x + " y2:" + endPointF.y);
Log.i(TAG, "distance:" + distance);
if (distance < 10) {
bannerBean = bannerBeans.get(currentAdImgIndex);
if (bannerBean.getExhibitorid() == 0) {
Intent intent = new Intent(context, AboutActivity.class);
context.startActivity(intent);
} else {
ExhibitorBean exhibitorBean = searchTool
.getExhibitorBeanByExhibitorID(bannerBean
.getExhibitorid() + "");
startM1010DataActivity(exhibitorBean);
}
}

break;

}

return true;
}

private void startM1010DataActivity(ExhibitorBean bean) {
Intent intent = new Intent(context, M1010DataActivity.class);

Bundle bundle = new Bundle();
bundle.putSerializable("mybean", (Serializable) bean);
Drawable drawableLeftTop = Drawable.createFromPath(MConfig
.setImagePath(bean.getImageid() + ""));
if (drawableLeftTop == null)
new ImageUtils(context).downloadImageThById(bean.getImageid() + "");
intent.putExtras(bundle);
intent.putExtra("flag", "fromBanner");
context.startActivity(intent);
}

}

  评论这张
 
阅读(792)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017