[Android UI]可自动切换、无限滑动的图片广告展示栏的实现分享(续1)

本文非技术类文章,博主只是想分享这样的一种实现方法,帮助有需要的开发人员减少工作量。我是学java出身的,做开发2年了,很理解一句话:工欲善其事必先利其器。

继另一篇博文:[Android初级]可自动切换、无限滑动的广告栏的实现  ,我觉得还有更好的方式来展示,这个比较合适更多开发人员。

本次实现的demo,用到了第三方的开源框架:1.JazzyViewPager https://github.com/jfeinstein10/JazzyViewPager(主要用它,效果比较多)

2.universal-image-loader(这个不说了)

下图为demo结构:

先看看是不是你想要的效果:

1.框架代码不多,算的上精简,有兴趣的自行研究。下面是主要的widget:JazzyViewPager.java,请自行下载。

public class JazzyViewPager extends ViewPager {

	public static final String TAG = "JazzyViewPager";

	private boolean mEnabled = true;
	private boolean mFadeEnabled = false;
	private boolean mOutlineEnabled = false;
	public static int sOutlineColor = Color.WHITE;
	private TransitionEffect mEffect = TransitionEffect.Standard;

	private HashMap<Integer, Object> mObjs = new LinkedHashMap<Integer, Object>();

	private static final float SCALE_MAX = 0.5f;
	private static final float ZOOM_MAX = 0.5f;
	private static final float ROT_MAX = 15.0f;

	public enum TransitionEffect {
		Standard, Tablet, CubeIn, CubeOut, FlipVertical, FlipHorizontal, Stack, ZoomIn, ZoomOut, RotateUp, RotateDown, Accordion
	}

	public JazzyViewPager(Context context) {
		this(context, null);
	}

	@SuppressWarnings("incomplete-switch")
	public JazzyViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		setClipChildren(false);
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.JazzyViewPager);
		int effect = ta.getInt(R.styleable.JazzyViewPager_style, 0);
		String[] transitions = getResources().getStringArray(
				R.array.jazzy_effects);
		setTransitionEffect(TransitionEffect.valueOf(transitions[effect]));
		setFadeEnabled(ta.getBoolean(R.styleable.JazzyViewPager_fadeEnabled,
				false));
		setOutlineEnabled(ta.getBoolean(
				R.styleable.JazzyViewPager_outlineEnabled, false));
		setOutlineColor(ta.getColor(R.styleable.JazzyViewPager_outlineColor,
				Color.WHITE));
		switch (mEffect) {
		case Stack:
		case ZoomOut:
			setFadeEnabled(true);
		}
		ta.recycle();
	}

	public void setTransitionEffect(TransitionEffect effect) {
		mEffect = effect;
	}

	public void setPagingEnabled(boolean enabled) {
		mEnabled = enabled;
	}

	public void setFadeEnabled(boolean enabled) {
		mFadeEnabled = enabled;
	}

	public boolean getFadeEnabled() {
		return mFadeEnabled;
	}

	public void setOutlineEnabled(boolean enabled) {
		mOutlineEnabled = enabled;
		wrapWithOutlines();
	}

	public void setOutlineColor(int color) {
		sOutlineColor = color;
	}

	private void wrapWithOutlines() {
		for (int i = 0; i < getChildCount(); i++) {
			View v = getChildAt(i);
			if (!(v instanceof OutlineContainer)) {
				removeView(v);
				super.addView(wrapChild(v), i);
			}
		}
	}

	private View wrapChild(View child) {
		if (!mOutlineEnabled || child instanceof OutlineContainer)
			return child;
		OutlineContainer out = new OutlineContainer(getContext());
		out.setLayoutParams(generateDefaultLayoutParams());
		child.setLayoutParams(new OutlineContainer.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		out.addView(child);
		return out;
	}

	public void addView(View child) {
		super.addView(wrapChild(child));
	}

	public void addView(View child, int index) {
		super.addView(wrapChild(child), index);
	}

	public void addView(View child, LayoutParams params) {
		super.addView(wrapChild(child), params);
	}

	public void addView(View child, int width, int height) {
		super.addView(wrapChild(child), width, height);
	}

	public void addView(View child, int index, LayoutParams params) {
		super.addView(wrapChild(child), index, params);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent arg0) {
		return mEnabled ? super.onInterceptTouchEvent(arg0) : false;
	}

	private State mState;
	private int oldPage;

	private View mLeft;
	private View mRight;
	private float mRot;
	private float mTrans;
	private float mScale;

	private enum State {
		IDLE, GOING_LEFT, GOING_RIGHT
	}

	private void logState(View v, String title) {
		Log.v(TAG,
				title + ": ROT (" + ViewHelper.getRotation(v) + ", "
						+ ViewHelper.getRotationX(v) + ", "
						+ ViewHelper.getRotationY(v) + "), TRANS ("
						+ ViewHelper.getTranslationX(v) + ", "
						+ ViewHelper.getTranslationY(v) + "), SCALE ("
						+ ViewHelper.getScaleX(v) + ", "
						+ ViewHelper.getScaleY(v) + "), ALPHA "
						+ ViewHelper.getAlpha(v));
	}

	protected void animateScroll(int position, float positionOffset) {
		if (mState != State.IDLE) {
			mRot = (float) (1 - Math.cos(2 * Math.PI * positionOffset)) / 2 * 30.0f;
			ViewHelper.setRotationY(this, mState == State.GOING_RIGHT ? mRot
					: -mRot);
			ViewHelper.setPivotX(this, getMeasuredWidth() * 0.5f);
			ViewHelper.setPivotY(this, getMeasuredHeight() * 0.5f);
		}
	}

	protected void animateTablet(View left, View right, float positionOffset) {
		if (mState != State.IDLE) {
			if (left != null) {
				mRot = 30.0f * positionOffset;
				mTrans = getOffsetXForRotation(mRot, left.getMeasuredWidth(),
						left.getMeasuredHeight());
				ViewHelper.setPivotX(left, left.getMeasuredWidth() / 2);
				ViewHelper.setPivotY(left, left.getMeasuredHeight() / 2);
				ViewHelper.setTranslationX(left, mTrans);
				ViewHelper.setRotationY(left, mRot);
				logState(left, "Left");
			}
			if (right != null) {
				mRot = -30.0f * (1 - positionOffset);
				mTrans = getOffsetXForRotation(mRot, right.getMeasuredWidth(),
						right.getMeasuredHeight());
				ViewHelper.setPivotX(right, right.getMeasuredWidth() * 0.5f);
				ViewHelper.setPivotY(right, right.getMeasuredHeight() * 0.5f);
				ViewHelper.setTranslationX(right, mTrans);
				ViewHelper.setRotationY(right, mRot);
				logState(right, "Right");
			}
		}
	}

	private void animateCube(View left, View right, float positionOffset,
			boolean in) {
		if (mState != State.IDLE) {
			if (left != null) {
				mRot = (in ? 90.0f : -90.0f) * positionOffset;
				ViewHelper.setPivotX(left, left.getMeasuredWidth());
				ViewHelper.setPivotY(left, left.getMeasuredHeight() * 0.5f);
				ViewHelper.setRotationY(left, mRot);
			}
			if (right != null) {
				mRot = -(in ? 90.0f : -90.0f) * (1 - positionOffset);
				ViewHelper.setPivotX(right, 0);
				ViewHelper.setPivotY(right, right.getMeasuredHeight() * 0.5f);
				ViewHelper.setRotationY(right, mRot);
			}
		}
	}

	private void animateAccordion(View left, View right, float positionOffset) {
		if (mState != State.IDLE) {
			if (left != null) {
				ViewHelper.setPivotX(left, left.getMeasuredWidth());
				ViewHelper.setPivotY(left, 0);
				ViewHelper.setScaleX(left, 1 - positionOffset);
			}
			if (right != null) {
				ViewHelper.setPivotX(right, 0);
				ViewHelper.setPivotY(right, 0);
				ViewHelper.setScaleX(right, positionOffset);
			}
		}
	}

	private void animateZoom(View left, View right, float positionOffset,
			boolean in) {
		if (mState != State.IDLE) {
			if (left != null) {
				mScale = in ? ZOOM_MAX + (1 - ZOOM_MAX) * (1 - positionOffset)
						: 1 + ZOOM_MAX - ZOOM_MAX * (1 - positionOffset);
				ViewHelper.setPivotX(left, left.getMeasuredWidth() * 0.5f);
				ViewHelper.setPivotY(left, left.getMeasuredHeight() * 0.5f);
				ViewHelper.setScaleX(left, mScale);
				ViewHelper.setScaleY(left, mScale);
			}
			if (right != null) {
				mScale = in ? ZOOM_MAX + (1 - ZOOM_MAX) * positionOffset : 1
						+ ZOOM_MAX - ZOOM_MAX * positionOffset;
				ViewHelper.setPivotX(right, right.getMeasuredWidth() * 0.5f);
				ViewHelper.setPivotY(right, right.getMeasuredHeight() * 0.5f);
				ViewHelper.setScaleX(right, mScale);
				ViewHelper.setScaleY(right, mScale);
			}
		}
	}

	private void animateRotate(View left, View right, float positionOffset,
			boolean up) {
		if (mState != State.IDLE) {
			if (left != null) {
				mRot = (up ? 1 : -1) * (ROT_MAX * positionOffset);
				mTrans = (up ? -1 : 1)
						* (float) (getMeasuredHeight() - getMeasuredHeight()
								* Math.cos(mRot * Math.PI / 180.0f));
				ViewHelper.setPivotX(left, left.getMeasuredWidth() * 0.5f);
				ViewHelper.setPivotY(left, up ? 0 : left.getMeasuredHeight());
				ViewHelper.setTranslationY(left, mTrans);
				ViewHelper.setRotation(left, mRot);
			}
			if (right != null) {
				mRot = (up ? 1 : -1) * (-ROT_MAX + ROT_MAX * positionOffset);
				mTrans = (up ? -1 : 1)
						* (float) (getMeasuredHeight() - getMeasuredHeight()
								* Math.cos(mRot * Math.PI / 180.0f));
				ViewHelper.setPivotX(right, right.getMeasuredWidth() * 0.5f);
				ViewHelper.setPivotY(right, up ? 0 : right.getMeasuredHeight());
				ViewHelper.setTranslationY(right, mTrans);
				ViewHelper.setRotation(right, mRot);
			}
		}
	}

	private void animateFlipHorizontal(View left, View right,
			float positionOffset, int positionOffsetPixels) {
		if (mState != State.IDLE) {
			if (left != null) {
				mRot = 180.0f * positionOffset;
				if (mRot > 90.0f) {
					left.setVisibility(View.INVISIBLE);
				} else {
					if (left.getVisibility() == View.INVISIBLE)
						left.setVisibility(View.VISIBLE);
					mTrans = positionOffsetPixels;
					ViewHelper.setPivotX(left, left.getMeasuredWidth() * 0.5f);
					ViewHelper.setPivotY(left, left.getMeasuredHeight() * 0.5f);
					ViewHelper.setTranslationX(left, mTrans);
					ViewHelper.setRotationY(left, mRot);
				}
			}
			if (right != null) {
				mRot = -180.0f * (1 - positionOffset);
				if (mRot < -90.0f) {
					right.setVisibility(View.INVISIBLE);
				} else {
					if (right.getVisibility() == View.INVISIBLE)
						right.setVisibility(View.VISIBLE);
					mTrans = -getWidth() - getPageMargin()
							+ positionOffsetPixels;
					ViewHelper
							.setPivotX(right, right.getMeasuredWidth() * 0.5f);
					ViewHelper.setPivotY(right,
							right.getMeasuredHeight() * 0.5f);
					ViewHelper.setTranslationX(right, mTrans);
					ViewHelper.setRotationY(right, mRot);
				}
			}
		}
	}

	private void animateFlipVertical(View left, View right,
			float positionOffset, int positionOffsetPixels) {
		if (mState != State.IDLE) {
			if (left != null) {
				mRot = 180.0f * positionOffset;
				if (mRot > 90.0f) {
					left.setVisibility(View.INVISIBLE);
				} else {
					if (left.getVisibility() == View.INVISIBLE)
						left.setVisibility(View.VISIBLE);
					mTrans = positionOffsetPixels;
					ViewHelper.setPivotX(left, left.getMeasuredWidth() * 0.5f);
					ViewHelper.setPivotY(left, left.getMeasuredHeight() * 0.5f);
					ViewHelper.setTranslationX(left, mTrans);
					ViewHelper.setRotationX(left, mRot);
				}
			}
			if (right != null) {
				mRot = -180.0f * (1 - positionOffset);
				if (mRot < -90.0f) {
					right.setVisibility(View.INVISIBLE);
				} else {
					if (right.getVisibility() == View.INVISIBLE)
						right.setVisibility(View.VISIBLE);
					mTrans = -getWidth() - getPageMargin()
							+ positionOffsetPixels;
					ViewHelper
							.setPivotX(right, right.getMeasuredWidth() * 0.5f);
					ViewHelper.setPivotY(right,
							right.getMeasuredHeight() * 0.5f);
					ViewHelper.setTranslationX(right, mTrans);
					ViewHelper.setRotationX(right, mRot);
				}
			}
		}
	}

	protected void animateStack(View left, View right, float positionOffset,
			int positionOffsetPixels) {
		if (mState != State.IDLE) {
			if (right != null) {
				mScale = (1 - SCALE_MAX) * positionOffset + SCALE_MAX;
				mTrans = -getWidth() - getPageMargin() + positionOffsetPixels;
				ViewHelper.setScaleX(right, mScale);
				ViewHelper.setScaleY(right, mScale);
				ViewHelper.setTranslationX(right, mTrans);
			}
			if (left != null) {
				left.bringToFront();
			}
		}
	}

	private Matrix mMatrix = new Matrix();
	private Camera mCamera = new Camera();
	private float[] mTempFloat2 = new float[2];

	protected float getOffsetXForRotation(float degrees, int width, int height) {
		mMatrix.reset();
		mCamera.save();
		mCamera.rotateY(Math.abs(degrees));
		mCamera.getMatrix(mMatrix);
		mCamera.restore();

		mMatrix.preTranslate(-width * 0.5f, -height * 0.5f);
		mMatrix.postTranslate(width * 0.5f, height * 0.5f);
		mTempFloat2[0] = width;
		mTempFloat2[1] = height;
		mMatrix.mapPoints(mTempFloat2);
		return (width - mTempFloat2[0]) * (degrees > 0.0f ? 1.0f : -1.0f);
	}

	protected void animateFade(View left, View right, float positionOffset) {
		if (left != null) {
			ViewHelper.setAlpha(left, 1 - positionOffset);
		}
		if (right != null) {
			ViewHelper.setAlpha(right, positionOffset);
		}
	}

	protected void animateOutline(View left, View right) {
		if (!(left instanceof OutlineContainer))
			return;
		if (mState != State.IDLE) {
			if (left != null) {
				((OutlineContainer) left).setOutlineAlpha(1.0f);
			}
			if (right != null) {
				((OutlineContainer) right).setOutlineAlpha(1.0f);
			}
		} else {
			if (left != null)
				((OutlineContainer) left).start();
			if (right != null)
				((OutlineContainer) right).start();
		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		if (mState == State.IDLE && positionOffset > 0) {
			oldPage = getCurrentItem();
			mState = position == oldPage ? State.GOING_RIGHT : State.GOING_LEFT;
		}
		boolean goingRight = position == oldPage;
		if (mState == State.GOING_RIGHT && !goingRight)
			mState = State.GOING_LEFT;
		else if (mState == State.GOING_LEFT && goingRight)
			mState = State.GOING_RIGHT;

		float effectOffset = isSmall(positionOffset) ? 0 : positionOffset;

		// mLeft = getChildAt(position);
		// mRight = getChildAt(position+1);
		mLeft = findViewFromObject(position);
		mRight = findViewFromObject(position + 1);

		if (mFadeEnabled)
			animateFade(mLeft, mRight, effectOffset);
		if (mOutlineEnabled)
			animateOutline(mLeft, mRight);

		switch (mEffect) {
		case Standard:
			break;
		case Tablet:
			animateTablet(mLeft, mRight, effectOffset);
			break;
		case CubeIn:
			animateCube(mLeft, mRight, effectOffset, true);
			break;
		case CubeOut:
			animateCube(mLeft, mRight, effectOffset, false);
			break;
		case FlipVertical:
			animateFlipVertical(mLeft, mRight, positionOffset,
					positionOffsetPixels);
			break;
		case FlipHorizontal:
			animateFlipHorizontal(mLeft, mRight, effectOffset,
					positionOffsetPixels);
		case Stack:
			animateStack(mLeft, mRight, effectOffset, positionOffsetPixels);
			break;
		case ZoomIn:
			animateZoom(mLeft, mRight, effectOffset, true);
			break;
		case ZoomOut:
			animateZoom(mLeft, mRight, effectOffset, false);
			break;
		case RotateUp:
			animateRotate(mLeft, mRight, effectOffset, true);
			break;
		case RotateDown:
			animateRotate(mLeft, mRight, effectOffset, false);
			break;
		case Accordion:
			animateAccordion(mLeft, mRight, effectOffset);
			break;
		}

		super.onPageScrolled(position, positionOffset, positionOffsetPixels);

		if (effectOffset == 0) {
			mState = State.IDLE;
		}

	}

	private boolean isSmall(float positionOffset) {
		return Math.abs(positionOffset) < 0.0001;
	}

	public void setObjectForPosition(Object obj, int position) {
		mObjs.put(Integer.valueOf(position), obj);
	}

	public View findViewFromObject(int position) {
		Object o = mObjs.get(Integer.valueOf(position));
		if (o == null) {
			return null;
		}
		PagerAdapter a = getAdapter();
		View v;
		for (int i = 0; i < getChildCount(); i++) {
			v = getChildAt(i);
			if (a.isViewFromObject(v, o))
				return v;
		}
		return null;
	}

}

2.主要的使用和实现,这里的思路就是利用Viewpager来展示imageView,切换效果是做一个延迟的循环调用方法如:handler.sendEmptyMessageDelayed 和handlerMessage方法配合使用。小圆圈的通过viewpager的index来同步自身的设置。是不是很简单,具体看代码“:

主布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="130dp" >

       <org.jan.adverpaper.widget.JazzyViewPager
            android:id="@+id/adviewpaper"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    <!--    <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/jd_ad_1" /> -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:gravity="center"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal" >

            <LinearLayout
                android:id="@+id/symblo_container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
            </LinearLayout>
        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

javaCode:MainActivity.java

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";
	private static int PAGER_START_PLAY = 0x123;
	//切换间隔时间3秒
	private static final int PLAY_TIME = 3 * 1000;
	//实现viewpager的控件
	private JazzyViewPager mViewPaper;
	//圆形标签的父层
	private LinearLayout symbolContainer;
	private ImageView[] images;
	private ImageView[] circleSymbols;
	private ArrayList<String> imageUrlList;
	//图片框架universalimageloader的图形帮助类
	private ImageLoader mImageLoader;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mImageLoader = ImageLoader.getInstance();
		initMockImages();
		setupMyHandler();
		initViews();
	}

	private void setupMyHandler() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (PAGER_START_PLAY == msg.what) {
					Log.d(TAG, "----PAGER_START_PLAY-----");
					int current = mViewPaper.getCurrentItem();
					if (current == images.length - 1) {
						current = -1;
					}
					Log.d(TAG, "play item = " + current);
					mViewPaper.setCurrentItem(current + 1);
					mHandler.sendEmptyMessageDelayed(PAGER_START_PLAY,
							PLAY_TIME);
				}
			}
		};
	}

	private void initViews() {
		symbolContainer = (LinearLayout) findViewById(R.id.symblo_container);
		circleSymbols = new ImageView[imageUrlList.size()];
		images = new ImageView[imageUrlList.size()];
		for (int i = 0; i < imageUrlList.size(); i++) {
			ImageView imageView = new ImageView(this);
			ImageView circle = new ImageView(this);
			imageView.setScaleType(ScaleType.CENTER_CROP);
			images[i] = imageView;
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			lp.setMargins(3, 0, 3,0);
			circle.setLayoutParams(lp);
			circle.setTag(i);
			circle.setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_normal));
			circleSymbols[i] = circle;
			symbolContainer.addView(circleSymbols[i]);
		}
		setViewPager(TransitionEffect.Standard);
	}

	private void setViewPager(TransitionEffect effect) {
		mViewPaper = (JazzyViewPager) findViewById(R.id.adviewpaper);
		mViewPaper.setTransitionEffect(effect);
		mViewPaper.setAdapter(new MyPagerAdapter());
		mViewPaper.setOnPageChangeListener(new MyPageViewChangeListener());
		mViewPaper.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (imageUrlList.size() == 0 || imageUrlList.size() == 1) {
					return true;
				} else {
					return false;
				}
			}
		});
		circleSymbols[0].setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_selected));
		mViewPaper.setCurrentItem(0);
		mHandler.sendEmptyMessageDelayed(PAGER_START_PLAY, PLAY_TIME);
	}
	/**
	 * 创建本地图片数据
	 */
	private void initMockImages() {
		imageUrlList = new ArrayList<String>();
		imageUrlList.add("drawable://" + R.drawable.jd_ad_0);
		imageUrlList.add("drawable://" + R.drawable.jd_ad_1);
		imageUrlList.add("drawable://" + R.drawable.jd_ad_2);
		imageUrlList.add("drawable://" + R.drawable.jd_ad_3);
		imageUrlList.add("drawable://" + R.drawable.jd_ad_4);
	}
	/**
	 * 设置圆形标签的状态
	 * @param index 当前标签的位置
	 */
	private void setSymbolImages(int index){
		for(ImageView image:circleSymbols){
			Integer i = (Integer) image.getTag();
			if(i==index){
				image.setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_selected));
			}else{
				image.setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_normal));
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private class MyPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return imageUrlList.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object obj) {
			if (view instanceof OutlineContainer) {
				return ((OutlineContainer) view).getChildAt(0) == obj;
			} else {
				return view == obj;
			}
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView(mViewPaper
					.findViewFromObject(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			mImageLoader.displayImage(imageUrlList.get(position),
					images[position]);
			container.addView(images[position], LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			final int index = position;
			images[position].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Log.e(TAG, "you clicked images position is" + index);
					Toast.makeText(MainActivity.this, "你点击了第"+(index+1)+"张图", Toast.LENGTH_SHORT).show();
				}
			});
			// 注意!不加这个方法要报IllegalStateException
			mViewPaper.setObjectForPosition(images[position], position);
			return images[position];
		}

	}

	private class MyPageViewChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int stateCode) {
			switch (stateCode) {
			case 0:
				// 你什么都没动
				break;
			case 1:
				// 正在滑动哦
				break;
			case 2:
				// 滑动完了
				break;
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int position) {
			Log.d(TAG, "onPageSelected-->position:" + position);
			setSymbolImages(position);
		}

	}
}

很简单吧!新手的话我建议自己去手把手的试一遍,对于快速开发有好处的。

最后附上demo下载去下载!打开如意门!

时间: 2024-09-30 02:00:00

[Android UI]可自动切换、无限滑动的图片广告展示栏的实现分享(续1)的相关文章

Android UI中英文自动显示问题

最近做了一个项目,其中有个视频和图片需要添加各种水印(日期,地点,经纬度,用户ID,产品ID等)问题,而且水印还要支持中英文自动切换显示.功能设计和实现算是比较顺利.昨天测试部给了一个小Bug,那就是在英文环境下,视频预览界面水印能正常显示英文,但通过拍照所获得的图片上的水印却显示为中文!而添加水印这一块代码是完全一样的啊?问题出在哪呢? 经过反复测试,没有发现代码有明显问题.于是凭直觉,就想难道是context上下文不同有关?于是检查代码发现context确实不同,在Activity View

android UI之ViewPager多页面滑动效果

  viewPager实现引导页 ViewPager多页面滑动效果 1.Android的左右滑动在实际编程经常能用到,比如查看多张图片,左右 切换tab页. 2.自android 3.0之后的SDK中提供了android-support-V4包用以实现 版本兼容,让老版本系统下的应用通过加入jar包实现扩展,其中有一 个可以实现左右滑动的类ViewPager 今天我们就用ViewPager类来实现引导页的实战案例 实现功能: 1.实现ViewPager多页面滑动效果. 2.下方的显示当前

[Android UI]ActionBar随ScorllView上下拖动而透明度渐变效果(续1)

根据上一篇ActionBar随ScorllView上下拖动而透明度渐变效果的基本描述,我们自定义的actionbar滚动透明的效果使用起来可能有点繁琐.这次想要在上次的代码内容上(可自动切换.无限滑动的图片广告展示栏的实现分享(续1)),博主太懒了,哈哈,实现这样的效果. 这次的demo很简单,老样子,你可以自己下载demo稍微看看,结构如下: 主界面代码: package org.jan.adviewpaper.demo; import android.app.ActionBar; impor

网络请求UI自动切换框架

1. 概述与分析 在实际项目中,我们不可避免的需要网络请求数据,由于网络或请求方式等主观或客观原因,导致我们请求的结果有时会出现一些偏差,从而导致我们UI界面显示也会有所不同.一般情况下,网络请求后我们的界面一般呈现三种页面状态:"正在加载中"."加载失败"和"加载成功".那么就可以通过网络请求后结果让UI界面自动切换, 2. 设计思路 通常情况下,正在加载和加载失败的界面都是相同的,只有加载成功后的界面不同.为了能让UI界面随着网络请求结果自动

高仿优酷Android客户端图片左右滑动(自动切换)

本例是用ViewPager去做的实现,支持自动滑动和手动滑动,不仅优酷网,实际上有很多商城和门户网站都有类似的实现: 具体思路: 1. 工程中需要添加android-support-v4.jar,才能使用ViewPager控件. 2. 图片的自动切换: 可使用Timer或者ScheduledExecutorService,这个有多重方式可以实现. 同时要切换底部的dots(园点) 3.Handler+Message机制更新UI,这个相信大家都很熟练,不再描述 4. 实现的一些细节:注意本例中的优

ViewPager之多页面滑动切换+游标滑动+ 自动无限轮转+触摸停止

我们先看下效果图 上图是我们需要实现的效果图,功能有如下三个: 1. 手势滑动可以切换界面,点击上面的引导一到引导四也可以切换到相应的画面. 2. 切换界面时,引导一到引导四下面的横线会相应的移动到对应的地方. 3. 没有手势时,界面会自动轮转,有种广告轮转的效果:有手势是,会停止轮转. 这次要实现的是第三个功能,实现第三个功能主要用到Handler和ViewPager的触摸监听器. Handler是为了实现隔一段时间自动切换页卡,因此需要定义一个Handler的对象,来处理自动切换,具体思想是

广告banner:手动滑动切换,自动切换,点击跳转,异步加载网络图片

效果图: 该banner功能有自动切换图片,点击图片可以自定义事件,手动滑动切换,异步加载图片 代码说话: 布局文件: <!-- 广告位 --> <FrameLayout android:id="@+id/new_recommend" android:layout_width="fill_parent" android:layout_height="wrap_content" > <com.cyou.cmall.ui

Android借助Handler,实现ViewPager中页面的自动切换(转)

在很多电商网页及app上都有自动切换的商品的推广快,感觉体验挺不错的,正好今天学习使用ViewPager,因此也实现了一个功能类似的demo. 下面是其中的两个截图:           实现一个自动播放功能的ViewPager,要做的主要有以下的几个部分: 实现一个ViewPagerAdapter,用于为ViewPager提供展示内容(例如上面的两张小猫图片) public class ViewPagerAdapter extends PagerAdapter { private List<V

Android之fragment点击切换和滑动切换结合

学了一小段时间的Android,主要接触的是UI设计,打交道最多莫过于fragment了吧.在Android3.0引入了fragment的概念后,几乎在所以的Android的应用中都可以看见其身影,已经有很多前辈高人都已经详细介绍过fragmrnt,我这里就不多说什么了. 这里本来是想模仿一下微信的切换效果,怎奈水平不足,这里就献丑贴出半成品的代码,希望有大神能指点一下.废话不多说,上代码.先从简单的开始吧,这里是我一共用了3个fragment,这里就只贴出第一个的fragment的布局,别的两