亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android仿微信底部按鈕滑動變色

發布時間:2020-09-29 11:53:31 來源:腳本之家 閱讀:171 作者:penguinfool 欄目:移動開發

Android仿微信底部按鈕滑動變色,這里只針對使用Fragment為Tab頁的滑動操作,進行簡單的變色講解。

首先說下OnPageChangeListener這個監聽

//這個監聽有三個方法
public abstract void onPageScrollStateChanged (int state) 

public abstract void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) 

public abstract void onPageSelected (int position) 

//第一個方法onPageScrollStateChanged 中的參數state,有三個可取的值

public static final int SCROLL_STATE_DRAGGING 
Constant Value: 1 (0x00000001) //手指按在ViewPager上滑動時

public static final int SCROLL_STATE_IDLE 
Constant Value: 0 (0x00000000) //手指松開后,ViewPager自動滑動期間

public static final int SCROLL_STATE_SETTLING 
Constant Value: 2 (0x00000002) //ViewPager進入了某個Page

//如果在onPageScrollStateChanged 中輸出state的值,你會發現每次都是按順序打印出“1---2---0”

//第二個方法onPageScrolled的三個參數

position://滑動時,屏幕左側顯示的第一個page

positionOffset://滑動比例,值的范圍為[0, 1),手指往左滑動,該值遞增,反之遞減

positionOffsetPixels://滑動距離,和屏幕有關,手指往左滑動,該值遞增,反之遞減

//我們經常需要檢查viewpager的滑動方向并作出一些操作,這時你只需要通過position和positionOffset兩個值即可實現該功能。

//比如實現微信底部圖標顏色漸變 向左滑動時

ChangeColorIconWithTextView left = mTabIndicator.get(position); //左側顯示的第一個page頁面上底部圖標和文字的初始化
ChangeColorIconWithTextView right = mTabIndicator.get(position + 1); //右側顯示的Page底部圖標和文字的初始化

left.setIconAlpha(1 - positionOffset); //設置圖標的透明度 此時positionOffset值遞增 左側圖標的顏色變淺
right.setIconAlpha(positionOffset); //右側圖標的顏色逐漸變深

//第三個方法onPageSelected的三個參數

position://當前選擇的page序號

//該方法被調用的時間比較特別,在上面的第一個方法中的“1---2----0”中的2執行之后,onPageSelected就執行,然后執行“1---2----0”中的0。就是手指松開屏幕之后,onPageSelected被執行。

上面提到了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;
  /**
   * 圖標
   */
  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);

    // 獲取設置的圖標
    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里面實現監聽操作

@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);
    }
  }

定義一個集合,將所有變色控件裝載進去

private List<ChangeColorIconWithTextView> mTabIndicator = new ArrayList<ChangeColorIconWithTextView>();

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);
  }

點擊監聽

@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;

    }
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

隆德县| 杭锦旗| 鄯善县| 安塞县| 调兵山市| 十堰市| 比如县| 漳浦县| 当阳市| 油尖旺区| 涿州市| 永嘉县| 福建省| 桦川县| 丹东市| 五寨县| 花莲县| 深泽县| 米林县| 上林县| 潞西市| 泰和县| 思南县| 敦化市| 晴隆县| 肥城市| 泰州市| 马边| 南木林县| 武清区| 咸宁市| 景泰县| 嘉定区| 舒兰市| 家居| 宜宾县| 金川县| 双柏县| 安岳县| 宁夏| 桦甸市|