臨時轉載 全部代碼貼上來,學習關閉後整理或刪除。
主界面只有一個Activity,無XML文件。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewFlipperView(this));
}
}
ViewFlipperView搭配一個接口IAdImages實現主界面的View。
/**
* 圖片信息接口
* @author haozi
*
*/
public interface IAdImages {
/**
* 廣告圖片
*/
int[] adImages = {
R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05,
R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09, R.drawable.img10
};
}
/**
* 這是一個類似於ViewFlipper的Wiget。
* @author haozi
*
*/
public class ViewFlipperView extends FrameLayout implements IAdImages {
private Context context; // 調用方的上下文
private int currentAdImgIndex; // 當前廣告圖片索引
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座標
public ViewFlipperView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
setView();
}
public ViewFlipperView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
setView();
}
public ViewFlipperView(Context context) {
super(context);
this.context = context;
setView();
}
/**
* 顯示View
*/
private void setView(){
// 初始化
int screenWidth = getResources().getDisplayMetrics().widthPixels;
mViewFlipper = new ViewFlipper(context);
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);
// 將廣告圖片加入ViewFlipper
for(int i=0; i<adImages.length; i++){
ImageView image = new ImageView(context);
image.setImageResource(adImages[i]);
mViewFlipper.addView(image);
}
addView(mViewFlipper);
// 將圖片索引點動態加入
for(int i=0; i<adImages.length; i++){
ImageView image = new ImageView(context);
if(i == 0){
image.setImageResource(R.drawable.point_selected);
}else{
image.setImageResource(R.drawable.point_normal);
}
image.setPadding(5, 0, 5, 20);
mTipLinearLayout.addView(image);
}
// 放置在左下角
mTipLinearLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
addView(mTipLinearLayout);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
break;
case MotionEvent.ACTION_UP:
endX = event.getX();
// 先保存上一個點
ImageView lastTipImageView = (ImageView) mTipLinearLayout.getChildAt(currentAdImgIndex);
if(currentAdImgIndex > 0 && endX > startX){// 查看前一頁的廣告
mViewFlipper.setInAnimation(left2RightInAnimation);
mViewFlipper.setOutAnimation(left2RightOutAnimation);
mViewFlipper.showPrevious();
currentAdImgIndex--;
if(currentAdImgIndex < 0){
currentAdImgIndex = 0;
}
}
if(currentAdImgIndex < adImages.length-1 && endX < startX){// 查看後一頁的廣告
mViewFlipper.setInAnimation(right2LeftInAnimation);
mViewFlipper.setOutAnimation(right2LeftOutAnimation);
mViewFlipper.showNext();
currentAdImgIndex++;
if(currentAdImgIndex > adImages.length-1){
currentAdImgIndex = adImages.length-1;
}
}
// 根據currentAdImgIndex改變底部點的狀態
ImageView currTipImageView = (ImageView) mTipLinearLayout.getChildAt(currentAdImgIndex);
lastTipImageView.setImageResource(R.drawable.point_normal);
currTipImageView.setImageResource(R.drawable.point_selected);
break;
}
return true;
}
}
由於工作原因,無法下載代碼。因此貼出代碼僅供自己學習。學習結束整理分析或者刪除博文。