ViewPagerIndicator-master源码分析 1
来源:互联网 发布:不忘初心的理解 知乎 编辑:程序博客网 时间:2024/06/06 18:10
1.ListActivity的使用
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.viewpagerindicator.sample/.ListSamples }
public class ListSamples extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String path = intent.getStringExtra("com.jakewharton.android.viewpagerindicator.sample.Path"); if (path == null) { path = ""; }// 参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文// 参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致// 参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。// 这个布局中必须包括了to中定义的控件id// 参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称// 参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应// 从from中获取数据填充到to指定的id 的 view里 setListAdapter(new SimpleAdapter(this, getData(path), android.R.layout.simple_list_item_1, new String[] { "title" }, new int[] { android.R.id.text1 })); // Enables or disables the type filter window. If enabled, typing when this view has focus //will filter the children to match the users input. Note that the Adapter used by this view must //implement the Filterable interface. getListView().setTextFilterEnabled(true); } protected List<Map<String, Object>> getData(String prefix) { List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>(); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory("com.jakewharton.android.viewpagerindicator.sample.SAMPLE"); // <activity 原因,除了主Activity,其余的配置 // android:name=".SampleCirclesDefault" // android:label="Circles/Default"> // <intent-filter> // <action android:name="android.intent.action.MAIN" /> // <category android:name="com.jakewharton.android.viewpagerindicator.sample.SAMPLE" /> // </intent-filter> // </activity> PackageManager pm = getPackageManager(); List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0); if (null == list) return myData; String[] prefixPath; String prefixWithSlash = prefix; if (prefix.equals("")) {// 主Activity prefixPath = null; } else { // 种类的Activity prefixPath = prefix.split("/"); prefixWithSlash = prefix + "/"; } int len = list.size(); Map<String, Boolean> entries = new HashMap<String, Boolean>(); // 此处要分两种情况,第一是启动页面的Activity时,会将同一类的进行一次存储,里面的总数就是种类的个数, // 如 android:label="Circles/Default"---存储Circles // 而对于某一种类的Activity,还会进入这个页面的,此时存储的是同一种类下的所有Activity // 如Circles/Default,Circles/Initial Page等等 for (int i = 0; i < len; i++) { ResolveInfo info = list.get(i); CharSequence labelSeq = info.loadLabel(pm); String label = labelSeq != null ? labelSeq.toString() : info.activityInfo.name; if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) { String[] labelPath = label.split("/"); // 主Activity的prefixPath == null String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length]; if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) { addItem(myData, nextLabel, activityIntent( info.activityInfo.applicationInfo.packageName, info.activityInfo.name)); } else { // 主Activity,只存储种类 if (entries.get(nextLabel) == null) { addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel)); entries.put(nextLabel, true); } } } } Collections.sort(myData, NAME_COMPARATOR); return myData; } // 比较字符串的比较器,返回负数,则map1在前,map2在后 private final static Comparator<Map<String, Object>> NAME_COMPARATOR = new Comparator<Map<String, Object>>() { private final Collator collator = Collator.getInstance(); public int compare(Map<String, Object> map1, Map<String, Object> map2) { return collator.compare(map1.get("title"), map2.get("title")); } }; protected Intent activityIntent(String pkg, String componentName) { Intent result = new Intent(); // 具体的子Activity result.setClassName(pkg, componentName); return result; } protected Intent browseIntent(String path) { Intent result = new Intent(); // 某一种类的activity result.setClass(this, ListSamples.class); result.putExtra("com.jakewharton.android.viewpagerindicator.sample.Path", path); return result; } protected void addItem(List<Map<String, Object>> data, String name, Intent intent) { Map<String, Object> temp = new HashMap<String, Object>(); temp.put("title", name); temp.put("intent", intent); // down data.add(temp); } @Override @SuppressWarnings("unchecked") protected void onListItemClick(ListView l, View v, int position, long id) { Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position); Intent intent = (Intent) map.get("intent"); //up startActivity(intent); }}
2 CirclePageIndicator
public class CirclePageIndicator extends View implements PageIndicator { private static final int INVALID_POINTER = -1; private float mRadius;// 圆的半径,两个圆心之间的距离是3个R,所有外间距是R 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; public CirclePageIndicator(Context context) { this(context, null); } public CirclePageIndicator(Context context, AttributeSet attrs) { this(context, attrs, R.attr.vpiCirclePageIndicatorStyle);// 必要时从主题中获取属性 } public CirclePageIndicator(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); if (isInEditMode()) return; //Load defaults from resources final Resources res = getResources(); final int defaultPageColor = res.getColor(R.color.default_circle_indicator_page_color); // 略.... //Retrieve styles attributes // 注意,defStyle代表如果优先级满足了则从主题中获取需要的属性,因为有些Activity的主题中配置了CirclePageIndicator的属性 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CirclePageIndicator, defStyle, 0); mCentered = a.getBoolean(R.styleable.CirclePageIndicator_centered, defaultCentered); mOrientation = a.getInt(R.styleable.CirclePageIndicator_android_orientation, defaultOrientation); mPaintPageFill.setStyle(Style.FILL); mPaintPageFill.setColor(a.getColor(R.styleable.CirclePageIndicator_pageColor, defaultPageColor)); mPaintPageFill.setColor(Color.parseColor("#FFFF0000")); mPaintStroke.setStyle(Style.STROKE); mPaintStroke.setColor(a.getColor(R.styleable.CirclePageIndicator_strokeColor, defaultStrokeColor)); mPaintStroke.setStrokeWidth(a.getDimension(R.styleable.CirclePageIndicator_strokeWidth, defaultStrokeWidth)); mPaintFill.setStyle(Style.FILL); mPaintFill.setColor(a.getColor(R.styleable.CirclePageIndicator_fillColor, defaultFillColor)); mRadius = a.getDimension(R.styleable.CirclePageIndicator_radius, defaultRadius); mSnap = a.getBoolean(R.styleable.CirclePageIndicator_snap, defaultSnap); Drawable background = a.getDrawable(R.styleable.CirclePageIndicator_android_background); if (background != null) { setBackgroundDrawable(background); } a.recycle(); // ViewConfigurationCompat的后缀Compat注意,v4 final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration); } // get set 方法 略... @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 * 3; //除了第一个,其余每个圆要占据3R的长度,即左边距R和自身的2R // 但是第一个圆左边是半个R,最后一个圆右边是半个R,这样才完美 final float shortOffset = shortPaddingBefore + mRadius; //高度中心值,参考下面onMeasure的设置 // 前提1,移动到了第一个圆的圆心位置处,之所以是圆心,因为在这个横坐标就开始画圆了,这样左边距是半R float longOffset = longPaddingBefore + mRadius; if (mCentered) { // 先不考虑上面前提1,这里即计算出了圆们的开始位置,包括边距 longOffset += ((longSize - longPaddingBefore - longPaddingAfter) / 2.0f) - ((count * threeRadius) / 2.0f); // 这里会有一处小bug,即当宽度设定为wrap-content时,如果mCentered仍为true,当在xml中配置padding // 为0时,上面计算的结果就是负的了,即第一个圆左边会有一小块看不到了,原因就在下面的onMeasure, // 因为longSize的值小于count *threeRadius,longSize就是getWidth(),就是onMeasure设定的 } float dX; float dY; float pageFillRadius = mRadius; if (mPaintStroke.getStrokeWidth() > 0) { //内部实心小圆 pageFillRadius -= mPaintStroke.getStrokeWidth() / 2.0f; } //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) { // mSnap为true代表pager切换以后再动,切换的结果,而false为切换的过程 cx += mPageOffset * threeRadius;//跟随pager的细微滑动而动,否则就是pager切换以后再动 } if (mOrientation == HORIZONTAL) { dX = longOffset + cx; dY = shortOffset; } else { dX = shortOffset; dY = longOffset + cx; } canvas.drawCircle(dX, dY, mRadius, mPaintFill); } public boolean onTouchEvent(android.view.MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; } if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) { return false; } // 同样,注意MotionEventCompat的后缀Compat, v4 // 总结,根据index拿id和x坐标的方法,MotionEventCompat.getPointerId和MotionEventCompat.getX // 而根据id需要找index的,因此MotionEventCompat.findPointerIndex // 根据多手指事件down或者up时,寻找index为MotionEventCompat.getActionIndex final int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case MotionEvent.ACTION_DOWN: mActivePointerId = MotionEventCompat.getPointerId(ev, 0);// index肯定是0啦 mLastMotionX = ev.getX();// down时直接拿就是了 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) { // 如何代码操纵viewpager滑动!!!!!!!!!!!!! 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; } @Override 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(); } @Override public void setViewPager(ViewPager view, int initialPosition) { setViewPager(view); setCurrentItem(initialPosition); } @Override public void setCurrentItem(int item) { if (mViewPager == null) { throw new IllegalStateException("ViewPager has not been bound."); } mViewPager.setCurrentItem(item); mCurrentPage = item; invalidate(); } @Override 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; // 重要,滑动时随时更新mCurrentPage 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); } } /* * (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(); // 实际占据的宽度值,从第一个圆左边到最后一个圆右边,加1为了误差吧 // count * 2 * mRadius为圆的长度,(count - 1)为间距的长度 result = (int) (getPaddingLeft() + getPaddingRight() + (count * 2 * mRadius) + (count - 1) * mRadius + 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 加1是为了颜值高吧,对应上面的高度设置,正好处于中心位置 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); } @SuppressWarnings("UnusedDeclaration") public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() { @Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); } @Override public SavedState[] newArray(int size) { return new SavedState[size]; } }; }}
3 TestFragment
public final class TestFragment extends Fragment { private static final String KEY_CONTENT = "TestFragment:Content"; public static TestFragment newInstance(String content) { TestFragment fragment = new TestFragment(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < 20; i++) { builder.append(content).append(" "); } builder.deleteCharAt(builder.length() - 1); fragment.mContent = builder.toString(); return fragment; } private String mContent = "???"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) { mContent = savedInstanceState.getString(KEY_CONTENT); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView text = new TextView(getActivity()); text.setGravity(Gravity.CENTER); text.setText(mContent); text.setTextSize(20 * getResources().getDisplayMetrics().density); text.setPadding(20, 20, 20, 20); LinearLayout layout = new LinearLayout(getActivity()); layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); layout.setGravity(Gravity.CENTER); layout.addView(text); return layout; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(KEY_CONTENT, mContent); }}
0 0
- ViewPagerIndicator-master源码分析 1
- ViewPagerIndicator-master源码分析 2
- ViewPagerIndicator-master源码分析 3
- 开源项目ViewPagerIndicator源码分析
- disruptor---master源码分析
- spark源码学习(二)---Master源码分析(1)-master的主备切换机制
- Nginx源码分析--master进程
- Spark源码分析-master启动
- 源码-spark Master(1)
- ViewPagerindicator 源码解析
- ViewPagerindicator 源码解析
- ViewPagerindicator 源码解析
- ViewPagerindicator 源码解析
- ViewPagerindicator 源码解析
- ViewPagerindicator 源码解析
- ViewPagerIndicator源码解读
- Hbase 源码分析5--Master启动过程
- MooseFS源码分析----master服务器初始化过程
- 2015计划
- Gradle学习(1)
- C语言关键字学习总结
- Error: Error parsing D:\android-sdk-windows\sdk\system-images\android-22\android-wear\x86\devices.xm
- Web远程调试
- ViewPagerIndicator-master源码分析 1
- PHP 基础
- 第三周 项目4-顺序表应用1
- 8位存储-128引发的思考
- 12306采用Pivotal GemFire分布式解决方案 解决尖峰高流量并发问题
- Jquery实际应用,判断radio,selelct,checkbox是否选中及选中的值
- nodeJs 笔记
- 看似简单的if-else
- featuretype