Android仿微信底部按钮滑动变色

发布时间:2020-09-29 11:53:31 作者:penguinfool
来源:脚本之家 阅读:172

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;

    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. 如何实现flutter仿微信底部图标渐变功能
  2. Android仿微信实现左滑显示删除按钮功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android 变色

上一篇:什么人适合用云服务器

下一篇:python调用staf自动化框架的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》