资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

banner广告及viewpager的小圆点指示器

一、主界面

站在用户的角度思考问题,与客户深入沟通,找到伊宁网站设计与伊宁网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册、网络空间、企业邮箱。业务覆盖伊宁地区。

package com.example.t;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

public class MainActivity extends Activity implements OnPageChangeListener {
 private CirclePageIndicator indicator;
 private ViewPager viewpager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initview();
 }

 private void initview() {
  viewpager = (ViewPager) findViewById(R.id.viewpager);
  indicator = (CirclePageIndicator) findViewById(R.id.indicator);
  indicator.setStrokeWidth(0);
  indicator.setPageCircleColor(0xFFFFFFFF);
  viewpager.setAdapter(new Myadapter(this));
  indicator.setViewPager(viewpager);
  indicator.setOnPageChangeListener(this);
 }

 @Override
 public void onPageScrollStateChanged(int arg0) {
 }

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

 @Override
 public void onPageSelected(int arg0) {
 }
}

二、广告适配器

package com.example.t;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class Myadapter extends PagerAdapter {
 private Context context;
 private int[] p_w_picpaths;

 public Myadapter(Context context) {
  this.context = context;
  p_w_picpaths = new int[] {R.drawable.default_img_mid, R.drawable.default_img_mid, R.drawable.default_img_mid };
 }

 @Override
 public int getCount() {
  return p_w_picpaths.length;
 }

 // 判断View和Object是否为同一个View
 @Override
 public boolean isViewFromObject(View view, Object object) {
  return view == object;
 }

 // 控制当某个View不需要的时候的回收处理
 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
  container.removeView((View) object);
 }

 // 实例化Item:即 得到每个View
 @Override
 public Object instantiateItem(ViewGroup container, int position) {
  ImageView iv = new ImageView(context);
  iv.setScaleType(ScaleType.CENTER_CROP);
  android.view.ViewGroup.LayoutParams params = new android.view.ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  iv.setLayoutParams(params);
  iv.setImageResource(p_w_picpaths[position]);
  container.addView(iv);
  return iv;
 }
}

三、小圆点指示器

package com.example.t;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;
import static android.widget.LinearLayout.HORIZONTAL;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

/**
 * @describe view pager 的小圆点指示器
 */
public class CirclePageIndicator extends View implements OnPageChangeListener {
 private static final int INVALID_POINTER = -1;

 private float mRadius;
 private final Paint mPaintPageFill = new Paint(ANTI_ALIAS_FLAG);
 private final Paint mPaintStroke = new Paint(ANTI_ALIAS_FLAG);
 private final Paint mPaintFill = new Paint(ANTI_ALIAS_FLAG);
 private ViewPager mViewPager;
 private ViewPager.OnPageChangeListener mListener;
 private int mCurrentPage;
 private int mSnapPage;
 private float mPageOffset;
 private int mScrollState;
 private int mOrientation;
 private boolean mCentered;
 private boolean mSnap;

 private int mTouchSlop;
 private float mLastMotionX = -1;
 private int mActivePointerId = INVALID_POINTER;
 private boolean mIsDragging;

 private Context context;

 public CirclePageIndicator(Context context) {
  super(context);
  this.context = context;
  init(context);
 }

 public CirclePageIndicator(Context context, AttributeSet attrs) {
  super(context, attrs);
  init(context);
 }

 private void init(Context context) {
  mCentered = true;
  mOrientation = 0;
  mPaintPageFill.setStyle(Style.FILL);
  mPaintPageFill.setColor(0x00000000);

  mPaintStroke.setStyle(Style.STROKE);
  mPaintStroke.setColor(0xFFFF6600);
  mPaintStroke.setStrokeWidth(getResources().getDimension(R.dimen.indicator_stroke_width));

  mPaintFill.setStyle(Style.FILL);
  mPaintFill.setColor(0xFFFF6600);
  mRadius = getResources().getDimension(R.dimen.indicator_radius);
  mSnap = false;

  final ViewConfiguration configuration = ViewConfiguration.get(context);
  mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
 }

 public void setRadius(int dpValue) {
  float scale=context.getResources().getDisplayMetrics().density;
  mRadius = (int)(dpValue*scale+0.5f);
  invalidate();
 }

 public float fraction;

 private float parseRadiusIfNeed(float pageFillRadius) {
  return fraction == 0 ? pageFillRadius : pageFillRadius + pageFillRadius / fraction;
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);

  if (mViewPager == null) {
   return;
  }
  final int count = mViewPager.getAdapter().getCount();
  if (count == 0) {
   return;
  }

  if (mCurrentPage >= count) {
   setCurrentItem(count - 1);
   return;
  }

  int longSize;
  int longPaddingBefore;
  int longPaddingAfter;
  int shortPaddingBefore;
  if (mOrientation == HORIZONTAL) {
   longSize = getWidth();
   longPaddingBefore = getPaddingLeft();
   longPaddingAfter = getPaddingRight();
   shortPaddingBefore = getPaddingTop();
  } else {
   longSize = getHeight();
   longPaddingBefore = getPaddingTop();
   longPaddingAfter = getPaddingBottom();
   shortPaddingBefore = getPaddingLeft();
  }

  final float threeRadius = mRadius * 5;
  final float shortOffset = shortPaddingBefore + mRadius;
  float longOffset = longPaddingBefore + mRadius;
  if (mCentered) {
   longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f);
  }

  float dX;
  float dY;

  float pageFillRadius = mRadius;
  if (mPaintStroke.getStrokeWidth() > 0) {
   pageFillRadius -= mPaintStroke.getStrokeWidth() * 1.5f; // 实心圆的半径
  }

  // Draw stroked circles
  for (int iLoop = 0; iLoop < count; iLoop++) {
   float drawLong = longOffset + (iLoop * threeRadius);
   if (mOrientation == HORIZONTAL) {
    dX = drawLong;
    dY = shortOffset;
   } else {
    dX = shortOffset;
    dY = drawLong;
   }

   // Only paint fill if not completely transparent
   if (mPaintPageFill.getAlpha() > 0) {
    canvas.drawCircle(dX, dY, pageFillRadius, mPaintPageFill);
   }

   // Only paint stroke if a stroke width was non-zero
   if (pageFillRadius != mRadius) {
    canvas.drawCircle(dX, dY, mRadius, mPaintStroke);
   }
  }

  // Draw the filled circle according to the current scroll
  float cx = (mSnap ? mSnapPage : mCurrentPage) * threeRadius;
  if (!mSnap) {
   cx += mPageOffset * threeRadius;
  }
  if (mOrientation == HORIZONTAL) {
   dX = longOffset + cx;
   dY = shortOffset;
  } else {
   dX = shortOffset;
   dY = longOffset + cx;
  }

  canvas.drawCircle(dX, dY, parseRadiusIfNeed(pageFillRadius), mPaintFill);
 }

 public boolean onTouchEvent(android.view.MotionEvent ev) {
  if (super.onTouchEvent(ev)) {
   return true;
  }
  if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
   return false;
  }

  final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
  switch (action) {
  case MotionEvent.ACTION_DOWN:
   mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
   mLastMotionX = ev.getX();
   break;

  case MotionEvent.ACTION_MOVE: {
   final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
   final float x = MotionEventCompat.getX(ev, activePointerIndex);
   final float deltaX = x - mLastMotionX;

   if (!mIsDragging) {
    if (Math.abs(deltaX) > mTouchSlop) {
     mIsDragging = true;
    }
   }

   if (mIsDragging) {
    mLastMotionX = x;
    if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
     mViewPager.fakeDragBy(deltaX);
    }
   }

   break;
  }

  case MotionEvent.ACTION_CANCEL:
  case MotionEvent.ACTION_UP:
   if (!mIsDragging) {
    final int count = mViewPager.getAdapter().getCount();
    final int width = getWidth();
    final float halfWidth = width / 2f;
    final float sixthWidth = width / 6f;

    if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
     if (action != MotionEvent.ACTION_CANCEL) {
      mViewPager.setCurrentItem(mCurrentPage - 1);
     }
     return true;
    } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
     if (action != MotionEvent.ACTION_CANCEL) {
      mViewPager.setCurrentItem(mCurrentPage + 1);
     }
     return true;
    }
   }

   mIsDragging = false;
   mActivePointerId = INVALID_POINTER;
   if (mViewPager.isFakeDragging())
    mViewPager.endFakeDrag();
   break;

  case MotionEventCompat.ACTION_POINTER_DOWN: {
   final int index = MotionEventCompat.getActionIndex(ev);
   mLastMotionX = MotionEventCompat.getX(ev, index);
   mActivePointerId = MotionEventCompat.getPointerId(ev, index);
   break;
  }

  case MotionEventCompat.ACTION_POINTER_UP:
   final int pointerIndex = MotionEventCompat.getActionIndex(ev);
   final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
   if (pointerId == mActivePointerId) {
    final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
    mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
   }
   mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
   break;
  }

  return true;
 }

 public void setViewPager(ViewPager view) {
  if (mViewPager == view) {
   return;
  }
  if (mViewPager != null) {
   mViewPager.setOnPageChangeListener(null);
  }
  if (view.getAdapter() == null) {
   throw new IllegalStateException("ViewPager does not have adapter instance.");
  }
  mViewPager = view;
  mViewPager.setOnPageChangeListener(this);
  invalidate();
 }

 public void setViewPager(ViewPager view, int initialPosition) {
  setViewPager(view);
  setCurrentItem(initialPosition);
 }

 public void setCurrentItem(int item) {
  if (mViewPager == null) {
   throw new IllegalStateException("ViewPager has not been bound.");
  }
  mViewPager.setCurrentItem(item);
  mCurrentPage = item;
  invalidate();
 }

 public void notifyDataSetChanged() {
  invalidate();
 }

 @Override
 public void onPageScrollStateChanged(int state) {
  mScrollState = state;

  if (mListener != null) {
   mListener.onPageScrollStateChanged(state);
  }
 }

 @Override
 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  mCurrentPage = position;
  mPageOffset = positionOffset;
  invalidate();

  if (mListener != null) {
   mListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
  }
 }

 @Override
 public void onPageSelected(int position) {
  if (mSnap || mScrollState == ViewPager.SCROLL_STATE_IDLE) {
   mCurrentPage = position;
   mSnapPage = position;
   invalidate();
  }

  if (mListener != null) {
   mListener.onPageSelected(position);
  }
 }

 public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
  mListener = listener;
 }

 /**
  * 设置圆边框的颜色
  */
 public void setStrokeColor(int color) {
  try {
   mPaintStroke.setColor(color);
  } catch (Exception e) {
   mPaintStroke.setColor(0xFFFF6600);
   e.printStackTrace();
  }
 }

 /**
  * 设置中心圆的颜色
  *
  * @param color
  */
 public void setCircleColor(int color) {
  try {
   mPaintFill.setColor(color);
  } catch (Exception e) {
   mPaintFill.setColor(0xFFFF6600);
   e.printStackTrace();
  }
 }

 public void setPageCircleColor(int color) {
  try {
   mPaintPageFill.setColor(color);
  } catch (Exception e) {
   mPaintPageFill.setColor(0x00000000);
   e.printStackTrace();
  }
 }

 public void setStrokeWidth(float width) {
  mPaintStroke.setStrokeWidth(width);
 }

 /*
  * (non-Javadoc)
  *
  * @see android.view.View#onMeasure(int, int)
  */
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  if (mOrientation == HORIZONTAL) {
   setMeasuredDimension(measureLong(widthMeasureSpec), measureShort(heightMeasureSpec));
  } else {
   setMeasuredDimension(measureShort(widthMeasureSpec), measureLong(heightMeasureSpec));
  }
 }

 /**
  * Determines the width of this view
  *
  * @param measureSpec
  *            A measureSpec packed into an int
  * @return The width of the view, honoring constraints from measureSpec
  */
 private int measureLong(int measureSpec) {
  int result;
  int specMode = MeasureSpec.getMode(measureSpec);
  int specSize = MeasureSpec.getSize(measureSpec);

  if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
   // We were told how big to be
   result = specSize;
  } else {
   // Calculate the width according the views count
   final int count = mViewPager.getAdapter().getCount();
   result = (int) (getPaddingLeft() + getPaddingRight() + (count * 2 * mRadius) + (count - 1) * mRadius * 4 + 1);
   // Respect AT_MOST value if that was what is called for by measureSpec
   if (specMode == MeasureSpec.AT_MOST) {
    result = Math.min(result, specSize);
   }
  }
  return result;
 }

 /**
  * Determines the height of this view
  *
  * @param measureSpec
  *            A measureSpec packed into an int
  * @return The height of the view, honoring constraints from measureSpec
  */
 private int measureShort(int measureSpec) {
  int result;
  int specMode = MeasureSpec.getMode(measureSpec);
  int specSize = MeasureSpec.getSize(measureSpec);

  if (specMode == MeasureSpec.EXACTLY) {
   // We were told how big to be
   result = specSize;
  } else {
   // Measure the height
   result = (int) (2 * mRadius + getPaddingTop() + getPaddingBottom() + 1);
   // Respect AT_MOST value if that was what is called for by measureSpec
   if (specMode == MeasureSpec.AT_MOST) {
    result = Math.min(result, specSize);
   }
  }
  return result;
 }

 @Override
 public void onRestoreInstanceState(Parcelable state) {
  SavedState savedState = (SavedState) state;
  super.onRestoreInstanceState(savedState.getSuperState());
  mCurrentPage = savedState.currentPage;
  mSnapPage = savedState.currentPage;
  requestLayout();
 }

 @Override
 public Parcelable onSaveInstanceState() {
  Parcelable superState = super.onSaveInstanceState();
  SavedState savedState = new SavedState(superState);
  savedState.currentPage = mCurrentPage;
  return savedState;
 }

 static class SavedState extends BaseSavedState {
  int currentPage;

  public SavedState(Parcelable superState) {
   super(superState);
  }

  private SavedState(Parcel in) {
   super(in);
   currentPage = in.readInt();
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
   super.writeToParcel(dest, flags);
   dest.writeInt(currentPage);
  }

  public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
   @Override
   public SavedState createFromParcel(Parcel in) {
    return new SavedState(in);
   }

   @Override
   public SavedState[] newArray(int size) {
    return new SavedState[size];
   }
  };
 }
}

四、布局xml

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#55000000"
    android:orientation="vertical" >

            android:id="@+id/indicator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" />

            android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
   

 

五、用到的资源

    1dp
    4dp


分享标题:banner广告及viewpager的小圆点指示器
分享URL:http://cdkjz.cn/article/gdcggg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220