Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014-2015 Umeng, Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.umeng.comm.ui.imagepicker.widgets; import android.content.Context; import android.graphics.Canvas; import android.graphics.CornerPathEffect; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.TextView; import com.umeng.comm.core.utils.ResFinder; public class ViewPagerIndicator extends LinearLayout { /** * */ private Paint mPaint; private Paint mLinePaint; /** * path? */ private Path mPath; private Paint mDividerLinePaint; /** * */ private int mTriangleWidth; /** * */ private int mTriangleHeight; /** * ?Tab1/6 */ private static final float RADIO_TRIANGEL = 1.0f / 6; /** * */ private final int DIMENSION_TRIANGEL_WIDTH = (int) (getScreenWidth() / 3 * RADIO_TRIANGEL); /** * ???? */ private int mInitTranslationX; /** * ??? */ private float mTranslationX; /** * Tab? */ private static final int COUNT_DEFAULT_TAB = 2; /** * tab? */ private int mTabVisibleCount = COUNT_DEFAULT_TAB; /** * tab */ private String[] mTabTitles; /** * ViewPager */ public ViewPager mViewPager; private int mHalfScreenWidth = 0; /** * */ private static final int COLOR_TEXT_NORMAL = ResFinder.getColor("umeng_comm_text_default_color"); /** * */ private static final int COLOR_TEXT_HIGHLIGHTCOLOR = ResFinder.getColor("umeng_comm_text_topic_light_color"); public ViewPagerIndicator(Context context) { this(context, null); } public ViewPagerIndicator(Context context, AttributeSet attrs) { super(context, attrs); // ? mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(COLOR_TEXT_HIGHLIGHTCOLOR); mPaint.setStyle(Style.FILL); mPaint.setPathEffect(new CornerPathEffect(3)); mLinePaint = new Paint(); mLinePaint.setAntiAlias(true); mLinePaint.setColor(COLOR_TEXT_HIGHLIGHTCOLOR); mLinePaint.setStyle(Style.FILL); mLinePaint.setStrokeWidth(10); mDividerLinePaint = new Paint(); mDividerLinePaint.setAntiAlias(true); mDividerLinePaint.setColor(COLOR_TEXT_HIGHLIGHTCOLOR); mDividerLinePaint.setStyle(Style.FILL); mDividerLinePaint.setStrokeWidth(5); } /** * */ @Override protected void dispatchDraw(Canvas canvas) { canvas.save(); // ? canvas.translate(mInitTranslationX + mTranslationX, getHeight() + 5); canvas.drawPath(mPath, mPaint); canvas.restore(); // tab canvas.drawLine(mTranslationX, getHeight() - 3, mTranslationX + mHalfScreenWidth, getHeight() - 3, mLinePaint); // canvas.drawLine(0, getHeight() - 1, 2 * mHalfScreenWidth, getHeight() - 1, mDividerLinePaint); super.dispatchDraw(canvas); } /** * ? */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mTriangleWidth = (int) (w / mTabVisibleCount * RADIO_TRIANGEL);// 1/6 of // width mTriangleWidth = Math.min(DIMENSION_TRIANGEL_WIDTH, mTriangleWidth); // ? initTriangle(); // ???? mInitTranslationX = getWidth() / mTabVisibleCount / 2 - mTriangleWidth / 2; } /** * ??tab? * * @param count */ public void setVisibleTabCount(int count) { this.mTabVisibleCount = count; } /** * tab ?? * * @param datas */ public void setTabItemTitles(String[] datas) { // listview if (datas != null && datas.length > 0) { this.removeAllViews(); this.mTabTitles = datas; for (String title : mTabTitles) { // view addView(generateTextView(title)); } // itemclick setItemClickEvent(); mHalfScreenWidth = getScreenWidth() / 2; } } /** * ViewPager? * * @author zhy */ public interface PageChangeListener { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels); public void onPageSelected(int position); public void onPageScrollStateChanged(int state); } // ViewPager? private PageChangeListener onPageChangeListener; // ViewPager? public void setOnPageChangeListener(PageChangeListener pageChangeListener) { this.onPageChangeListener = pageChangeListener; } // ?ViewPager public void setViewPager(ViewPager mViewPager, int pos) { this.mViewPager = mViewPager; mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { // resetTextViewColor(); highLightTextView(position); // if (onPageChangeListener != null) { onPageChangeListener.onPageSelected(position); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // scroll(position, positionOffset); // if (onPageChangeListener != null) { onPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels); } } @Override public void onPageScrollStateChanged(int state) { // if (onPageChangeListener != null) { onPageChangeListener.onPageScrollStateChanged(state); } } }); // ? mViewPager.setCurrentItem(pos); // highLightTextView(pos); } /** * * * @param position */ protected void highLightTextView(int position) { View view = getChildAt(position); if (view instanceof TextView) { ((TextView) view).setTextColor(COLOR_TEXT_HIGHLIGHTCOLOR); } } /** * ? */ private void resetTextViewColor() { for (int i = 0; i < getChildCount(); i++) { View view = getChildAt(i); if (view instanceof TextView) { ((TextView) view).setTextColor(COLOR_TEXT_NORMAL); } } } /** * */ public void setItemClickEvent() { int cCount = getChildCount(); for (int i = 0; i < cCount; i++) { final int j = i; View view = getChildAt(i); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mViewPager.setCurrentItem(j); } }); } } /** * ??TextView * * @param text * @return */ private TextView generateTextView(String text) { TextView tv = new TextView(getContext()); LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); lp.width = getScreenWidth() / mTabVisibleCount; tv.setGravity(Gravity.CENTER); tv.setTextColor(COLOR_TEXT_NORMAL); tv.setText(text); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); tv.setLayoutParams(lp); return tv; } /** * ? */ private void initTriangle() { mPath = new Path(); mTriangleHeight = (int) (mTriangleWidth * Math.sqrt(3) / 4); // mPath.moveTo(0, 0); mPath.lineTo(mTriangleWidth, 0); mPath.lineTo(mTriangleWidth / 2, -mTriangleHeight); mPath.close(); } /** * ?? * * @param position * @param offset */ public void scroll(int position, float offset) { /** * <pre> * 0-1:position=0 ;1-0:postion=0; * </pre> */ // ?????invalidate mTranslationX = getWidth() / mTabVisibleCount * (position + offset); int tabWidth = getScreenWidth() / mTabVisibleCount; // ? if (offset > 0 && position >= (mTabVisibleCount - 2) && getChildCount() > mTabVisibleCount) { if (mTabVisibleCount != 1) { this.scrollTo((position - (mTabVisibleCount - 2)) * tabWidth + (int) (tabWidth * offset), 0); } else { // count1 ? this.scrollTo(position * tabWidth + (int) (tabWidth * offset), 0); } } invalidate(); } /** * view?setTabTitlesview */ @Override protected void onFinishInflate() { Log.e("TAG", "onFinishInflate"); super.onFinishInflate(); int cCount = getChildCount(); if (cCount == 0) return; for (int i = 0; i < cCount; i++) { View view = getChildAt(i); LayoutParams lp = (LayoutParams) view.getLayoutParams(); lp.weight = 0; lp.width = getScreenWidth() / mTabVisibleCount; view.setLayoutParams(lp); } // setItemClickEvent(); } /** * ? * * @return */ public int getScreenWidth() { WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } }