Android仿微信底部按鈕滑動(dòng)變色,這里只針對使用Fragment為Tab頁的滑動(dòng)操作,進(jìn)行簡單的變色講解。
創(chuàng)新互聯(lián)公司專注于赫章網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供赫章營銷型網(wǎng)站建設(shè),赫章網(wǎng)站制作、赫章網(wǎng)頁設(shè)計(jì)、赫章網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造赫章網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供赫章網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
首先說下OnPageChangeListener這個(gè)監(jiān)聽
//這個(gè)監(jiān)聽有三個(gè)方法 public abstract void onPageScrollStateChanged (int state) public abstract void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) public abstract void onPageSelected (int position) //第一個(gè)方法onPageScrollStateChanged 中的參數(shù)state,有三個(gè)可取的值 public static final int SCROLL_STATE_DRAGGING Constant Value: 1 (0x00000001) //手指按在ViewPager上滑動(dòng)時(shí) public static final int SCROLL_STATE_IDLE Constant Value: 0 (0x00000000) //手指松開后,ViewPager自動(dòng)滑動(dòng)期間 public static final int SCROLL_STATE_SETTLING Constant Value: 2 (0x00000002) //ViewPager進(jìn)入了某個(gè)Page //如果在onPageScrollStateChanged 中輸出state的值,你會(huì)發(fā)現(xiàn)每次都是按順序打印出“1---2---0” //第二個(gè)方法onPageScrolled的三個(gè)參數(shù) position://滑動(dòng)時(shí),屏幕左側(cè)顯示的第一個(gè)page positionOffset://滑動(dòng)比例,值的范圍為[0, 1),手指往左滑動(dòng),該值遞增,反之遞減 positionOffsetPixels://滑動(dòng)距離,和屏幕有關(guān),手指往左滑動(dòng),該值遞增,反之遞減 //我們經(jīng)常需要檢查viewpager的滑動(dòng)方向并作出一些操作,這時(shí)你只需要通過position和positionOffset兩個(gè)值即可實(shí)現(xiàn)該功能。 //比如實(shí)現(xiàn)微信底部圖標(biāo)顏色漸變 向左滑動(dòng)時(shí) ChangeColorIconWithTextView left = mTabIndicator.get(position); //左側(cè)顯示的第一個(gè)page頁面上底部圖標(biāo)和文字的初始化 ChangeColorIconWithTextView right = mTabIndicator.get(position + 1); //右側(cè)顯示的Page底部圖標(biāo)和文字的初始化 left.setIconAlpha(1 - positionOffset); //設(shè)置圖標(biāo)的透明度 此時(shí)positionOffset值遞增 左側(cè)圖標(biāo)的顏色變淺 right.setIconAlpha(positionOffset); //右側(cè)圖標(biāo)的顏色逐漸變深 //第三個(gè)方法onPageSelected的三個(gè)參數(shù) position://當(dāng)前選擇的page序號 //該方法被調(diào)用的時(shí)間比較特別,在上面的第一個(gè)方法中的“1---2----0”中的2執(zhí)行之后,onPageSelected就執(zhí)行,然后執(zhí)行“1---2----0”中的0。就是手指松開屏幕之后,onPageSelected被執(zhí)行。
上面提到了ChangeColorIconWithTextView 主要類
package com.mhealth.nursestation.app.pda.business.login.base.ui.fragmentutil; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.os.Looper; import android.os.Parcelable; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import com.mhealth.nursestation.app.pda.R; /** * 此類用于修改顏色漸變 */ public class ChangeColorIconWithTextView extends View { private Bitmap mBitmap; private Canvas mCanvas; private Paint mPaint; /** * 顏色 */ private int mColor = 0XFF07B7C4; /** * 透明度 0.0-1.0 初始化必須是0 不是0就廢了 */ private float mAlpha = 0f; /** * 圖標(biāo) */ private Bitmap mIconBitmap; /** * 限制繪制icon的范圍 */ private Rect mIconRect; /** * icon底部文本 */ private String mText = "微信"; private int mTextSize = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()); private Paint mTextPaint; private Rect mTextBound = new Rect(); public ChangeColorIconWithTextView(Context context) { super(context); } /** * 初始化自定義屬性值 * * @param context * @param attrs */ public ChangeColorIconWithTextView(Context context, AttributeSet attrs) { super(context, attrs); // 獲取設(shè)置的圖標(biāo) TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorIconView); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.ChangeColorIconView_icon: BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr); mIconBitmap = drawable.getBitmap(); break; case R.styleable.ChangeColorIconView_color: mColor = a.getColor(attr, 0x07B7C4); break; case R.styleable.ChangeColorIconView_text: mText = a.getString(attr); break; case R.styleable.ChangeColorIconView_text_size: mTextSize = (int) a.getDimension(attr, TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics())); break; } } a.recycle(); mTextPaint = new Paint(); mTextPaint.setTextSize(mTextSize); mTextPaint.setColor(0xff07B7C4); mTextPaint.setAntiAlias(true); // 得到text繪制范圍 mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 得到繪制icon的寬 int bitmapWidth = Math.min(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(), getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - mTextBound.height()); int left = getMeasuredWidth() / 2 - bitmapWidth / 2; int top = (getMeasuredHeight() - mTextBound.height()) / 2 - bitmapWidth / 2; // 得到繪制icon的寬 mIconRect = new Rect(left, top, left + bitmapWidth, top + bitmapWidth); } @Override protected void onDraw(Canvas canvas) { int alpha = (int) Math.ceil((255 * mAlpha)); canvas.drawBitmap(mIconBitmap, null, mIconRect, null); setupTargetBitmap(alpha); drawSourceText(canvas, alpha); drawTargetText(canvas, alpha); canvas.drawBitmap(mBitmap, 0, 0, null); } private void setupTargetBitmap(int alpha) { mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Config.ARGB_8888); mCanvas = new Canvas(mBitmap); mPaint = new Paint(); mPaint.setColor(mColor); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setAlpha(alpha); mCanvas.drawRect(mIconRect, mPaint); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); mPaint.setAlpha(255); mCanvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint); } /** * 透明度 255 * * @param canvas * @param alpha */ private void drawSourceText(Canvas canvas, int alpha) { mTextPaint.setTextSize(mTextSize); mTextPaint.setColor(0x00333333); mTextPaint.setAlpha(255 - alpha); canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2 - mTextBound.width() / 2, mIconRect.bottom + mTextBound.height(), mTextPaint); } private void drawTargetText(Canvas canvas, int alpha) { mTextPaint.setColor(mColor); mTextPaint.setAlpha(alpha); canvas.drawText(mText, mIconRect.left + mIconRect.width() / 2 - mTextBound.width() / 2, mIconRect.bottom + mTextBound.height(), mTextPaint); } public void setIconAlpha(float alpha) { this.mAlpha = alpha; invalidateView(); } private void invalidateView() { if (Looper.getMainLooper() == Looper.myLooper()) { invalidate(); } else { postInvalidate(); } } public void setIconColor(int color) { mColor = color; } public void setIcon(int resId) { this.mIconBitmap = BitmapFactory.decodeResource(getResources(), resId); if (mIconRect != null) invalidateView(); } public void setIcon(Bitmap iconBitmap) { this.mIconBitmap = iconBitmap; if (mIconRect != null) invalidateView(); } private static final String INSTANCE_STATE = "instance_state"; private static final String STATE_ALPHA = "state_alpha"; @Override protected Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState()); bundle.putFloat(STATE_ALPHA, mAlpha); return bundle; } @Override protected void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; mAlpha = bundle.getFloat(STATE_ALPHA); super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE)); } else { super.onRestoreInstanceState(state); } } }
在Activity里面實(shí)現(xiàn)監(jiān)聽操作
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (positionOffset > 0) { ChangeColorIconWithTextView left = mTabIndicator.get(position); ChangeColorIconWithTextView right = mTabIndicator.get(position + 1); left.setIconAlpha(1 - positionOffset); right.setIconAlpha(positionOffset); } }
定義一個(gè)集合,將所有變色控件裝載進(jìn)去
private ListmTabIndicator = new ArrayList (); private void initTabIndicator() { ChangeColorIconWithTextView one = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_one); ChangeColorIconWithTextView two = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_two); ChangeColorIconWithTextView three = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_three); ChangeColorIconWithTextView four = (ChangeColorIconWithTextView) findViewById(R.id.id_indicator_four); mTabIndicator.add(one); mTabIndicator.add(two); mTabIndicator.add(three); mTabIndicator.add(four); one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this); four.setOnClickListener(this); one.setIconAlpha(1.0f); }
點(diǎn)擊監(jiān)聽
@Override public void onClick(View v) { resetOtherTabs(); switch (v.getId()) { case R.id.id_indicator_one: mTabIndicator.get(0).setIconAlpha(1.0f); mViewPager.setCurrentItem(0, false); break; case R.id.id_indicator_two: mTabIndicator.get(1).setIconAlpha(1.0f); mViewPager.setCurrentItem(1, false); break; case R.id.id_indicator_three: mTabIndicator.get(2).setIconAlpha(1.0f); mViewPager.setCurrentItem(2, false); mAppContext.has_new_talk = false; break; case R.id.id_indicator_four: mTabIndicator.get(3).setIconAlpha(1.0f); mViewPager.setCurrentItem(3, false); break; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。