/**
*自定义imageview,主要是为了释放touch传�?事件到viewpager�?
* @author liao
*
*/
public class MyImageView extends ImageView{
public MyImageView(Context context) {
super(context);
init();
}
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init()
{
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
});
}
}
import java.util.ArrayList;
import com.hx.zsnc.R;
import com.hx.zsnc.adapter.MyViewPagerAdapater;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
/**
* 1.自定义viewpager实现广告界面,支持手势滑动和点击事�?不支持无限向�?��方向循环 2.播放到两边之后会循环来回播放
*
*/
public class MyViewPager extends ViewPager {
Context context;
LayoutInflater inflater;
View view_fir, view_sec, view_thi, view_four, view_five, view_six;
MyImageView imageView_fir, imageView_sec, imageView_thi, imageView_four,
imageView_five;
ArrayList<View> arrayList;
MyViewPagerAdapater adapater;
long fir_time, last_time;
MyViewPagerClick click;
boolean isClick = true;
boolean isPlay = true;
int start = 100;
boolean isStartRight = true;
Handler handler;
float MoveX, MoveY;
int time;// 切换间隔时间
int times = 4;// for循环添加view次数
View viewAll[] = { view_fir, view_sec, view_thi, view_four, view_five,
view_six };
int pic[] = { R.drawable.photo01, R.drawable.photo02,
R.drawable.play_photo01, R.drawable.play_photo01 };
int view_id[] = { R.layout.home_ad1, R.layout.home_ad2, R.layout.home_ad3,
R.layout.home_ad4 };
int image_id[] = { R.id.imageview_one, R.id.imageview_two,
R.id.imageview_three, R.id.imageview_four };
MyImageView imageViewAll[] = { imageView_fir, imageView_sec, imageView_thi,
imageView_four };
public MyViewPager(Context context) {
super(context);
this.context = context;
init();
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private void init() {
arrayList = new ArrayList<View>();
inflater = LayoutInflater.from(context);
for (int i = 0; i < times; i++) {
viewAll[i] = inflater.inflate(view_id[i], null);
imageViewAll[i] = (MyImageView) viewAll[i]
.findViewById(image_id[i]);
imageViewAll[i].setImageResource(pic[i]);
arrayList.add(viewAll[i]);
}
adapater = new MyViewPagerAdapater(arrayList);
this.setAdapter(adapater);
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
Log.i("ljs", "this is a touch");
isPlay = false;
float X = arg1.getX();
float Y = arg1.getY();
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
fir_time = System.nanoTime();
MoveX = X;
MoveY = Y;
break;
case MotionEvent.ACTION_UP:
last_time = System.nanoTime();
if ((last_time - fir_time) / 1000000l > 120
|| last_time - fir_time <= 0 || !isClick) {
Log.i("ljs", "this is a click cancel");
} else {
Log.i("ljs", "this is a click ");
click.OnClick();
}
isPlay = true;
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) (MoveX - X);
int moveY = (int) (MoveY - Y);
if (moveX * moveX + moveY * moveY > 100) {
isClick = false;
}
break;
}
return false;
}
});
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.push_right_in);
this.setAnimation(animation);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == start && isPlay) {
if (isStartRight) {
if (getCurrentItem() + 1 == arrayList.size()) {
isStartRight = false;
Animation animation = AnimationUtils.loadAnimation(
context, R.anim.push_right_out);
viewAll[getCurrentItem()].setAnimation(animation);
setCurrentItem(getCurrentItem() - 1);
} else {
setCurrentItem(getCurrentItem() + 1);
}
} else {
if (getCurrentItem() == 0) {
isStartRight = true;
setCurrentItem(getCurrentItem() + 1);
} else {
setCurrentItem(getCurrentItem() - 1);
}
}
}
sendMess();
}
};
}
/**
* 自定义点击事件接�?
*
* @param click
*/
public void SetMysetOnClickListener(MyViewPagerClick click) {
this.click = click;
}
/**
* 通过此方法启动hanlder实现无限循环来播放view,time 为每次播放间隔时�?
*/
public void start(int time) {
this.time = time;
sendMess();
}
private void sendMess() {
Message message = new Message();
message.what = start;
handler.sendMessageDelayed(message, time);
}
public void setImageall(MyImageView[] imageViews) {
this.imageViewAll = imageViews;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean ret = super.dispatchTouchEvent(ev);
if (ret) {
requestDisallowInterceptTouchEvent(true);
}
return ret;
}
}
public interface MyViewPagerClick {
public void OnClick();
}