Android 中 ViewFlipper 的简单实例

main.xml

==”match_parent”tools:context=”.Main” ><TextView==”@string/hello_world” /></RelativeLayout>

Main.java

package com.wty.mydemoviewflipper;import android.app.Activity;import android.os.Bundle;import com.wty.mydemoviewflipper.wiget.ViewFlipperView;{(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(new ViewFlipperView(this));}}

IAdImages.java

package import /** * 图片信息接口 * @author vhreal * */public interface IAdImages {/*** 广告图片*/int[] adImages = {R02, R04, R.drawable.img05,R07, R09, R.drawable.img10};}

ViewFlipperView.java

package com.wty.mydemoviewflipper.wiget;import android.content.Context;import android.view.Gravity;import android.view.MotionEvent;import android.view.animation.Animation;import android.view.animation.TranslateAnimation;import android.widget.FrameLayout;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ViewFlipper;import com.wty.mydemoviewflipper.R;/** * 这是一个类似于ViewFlipper的Wiget。 * @author vhreal * */{currentAdImgIndex;Animation left2RightOutAnimation;Animation right2LeftOutAnimation;animationDuration = LinearLayout mTipLinearLayout;startX = endX = (Context context) {super(context);this.context = context;setView();}/*** 显示View*/(){// 初始化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);// 将广告图片加入ViewFlipperfor(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);}(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;}}

,一直有记日记的习惯,可是,旅行回来,都懒得写日记来记录,

Android 中 ViewFlipper 的简单实例

相关文章:

你感兴趣的文章:

标签云: