Пример #1
0
 /**
  * Set the tint of the ordering button.
  *
  * @param button
  * @param tint
  */
 private static void setButtonTint(Button button, ColorStateList tint) {
   if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
       && button instanceof AppCompatButton) {
     ((AppCompatButton) button).setSupportBackgroundTintList(tint);
   } else {
     ViewCompat.setBackgroundTintList(button, tint);
   }
 }
Пример #2
0
  public IndicatableFFAB(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    final View v = View.inflate(context, R.layout.view_indicatable_ffab, this);
    indicator = (ActionIndicatorView) v.findViewById(R.id.iffab_indicator);
    ffab = (FlingableFAB) v.findViewById(R.id.iffab_ffab);
    ViewCompat.setElevation(indicator, ffab.getCompatElevation());

    final TypedArray a =
        context.obtainStyledAttributes(
            attrs, R.styleable.IndicatableFFAB, defStyleAttr, R.style.Widget_FFAB_IndicatableFFAB);
    try {
      final Drawable fabIcon = a.getDrawable(R.styleable.IndicatableFFAB_fabIcon);
      ffab.setImageDrawable(fabIcon);
      final int fabTint = a.getColor(R.styleable.IndicatableFFAB_fabTint, NO_ID);
      if (fabTint != NO_ID) {
        ViewCompat.setBackgroundTintList(ffab, ColorStateList.valueOf(fabTint));
      }
      final int indicatorTint = a.getColor(R.styleable.IndicatableFFAB_indicatorTint, 0);
      indicator.setBackgroundColor(indicatorTint);
      indicatorMargin =
          a.getDimensionPixelSize(R.styleable.IndicatableFFAB_marginFabToIndicator, 0);
    } finally {
      a.recycle();
    }

    ffab.setOnTouchListener(
        new OnTouchListener() {
          private MotionEvent old;

          @Override
          public boolean onTouch(View view, MotionEvent motionEvent) {
            final int action = motionEvent.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
              old = MotionEvent.obtain(motionEvent);
              onStart();
              return false;
            }
            final Direction direction = Direction.getDirection(old, motionEvent);
            if (action == MotionEvent.ACTION_MOVE) {
              onMoving(direction);
            } else if (action == MotionEvent.ACTION_UP) {
              old.recycle();
              onFling(view.getHandler());
            }
            return false;
          }

          private Direction prevSelected = Direction.UNDEFINED;

          public void onStart() {
            indicator.onActionLeave(prevSelected);
            prevSelected = Direction.UNDEFINED;
            indicator.setVisibility(View.VISIBLE);
          }

          public void onMoving(Direction direction) {
            if (prevSelected == direction) {
              return;
            }
            indicator.onActionLeave(prevSelected);
            if (isDirectionEnabled(direction)) {
              indicator.onActionSelected(direction);
            }
            prevSelected = direction;
          }

          private boolean isDirectionEnabled(Direction direction) {
            for (Direction d : enableDirections) {
              if (d == direction) {
                return true;
              }
            }
            return false;
          }

          public void onFling(Handler handler) {
            handler.postDelayed(
                new Runnable() {
                  @Override
                  public void run() {
                    indicator.setVisibility(View.INVISIBLE);
                  }
                },
                200);
          }
        });

    if (isInEditMode()) {
      indicator.setVisibility(VISIBLE);
    }
  }