Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MenuToggle.java « menu « widget « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89bacf939f8db174690643a14312b56566267adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.mapswithme.maps.widget.menu;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.support.annotation.DimenRes;
import android.support.annotation.DrawableRes;
import android.view.View;
import android.widget.ImageView;

import com.mapswithme.maps.R;
import com.mapswithme.maps.widget.RotateByAlphaDrawable;
import com.mapswithme.maps.widget.TrackedTransitionDrawable;
import com.mapswithme.util.UiUtils;

class MenuToggle
{
  private final ImageView mButton;
  private final boolean mAlwaysShow;

  private final TransitionDrawable mOpenImage;
  private final TransitionDrawable mCollapseImage;

  MenuToggle(View frame, @DimenRes int heightRes)
  {
    this(frame, heightRes, R.drawable.ic_menu_open, R.drawable.ic_menu_close);
  }

  private MenuToggle(View frame, @DimenRes int heightRes, @DrawableRes int src, @DrawableRes int dst)
  {
    mButton = (ImageView) frame.findViewById(R.id.toggle);
    mAlwaysShow = (frame.findViewById(R.id.disable_toggle) == null);

    int sz = UiUtils.dimen(heightRes);
    Rect bounds = new Rect(0, 0, sz, sz);

    mOpenImage = new TrackedTransitionDrawable(new Drawable[]{
        new RotateByAlphaDrawable(frame.getContext(), src, R.attr.iconTint, false)
            .setInnerBounds(bounds),
        new RotateByAlphaDrawable(frame.getContext(), dst, R.attr.iconTintLight, true)
            .setInnerBounds(bounds)
            .setBaseAngle(-90)});
    mCollapseImage = new TrackedTransitionDrawable(new Drawable[]{
        new RotateByAlphaDrawable(frame.getContext(), src, R.attr.iconTint, false)
            .setInnerBounds(bounds),
        new RotateByAlphaDrawable(frame.getContext(), dst, R.attr.iconTintLight, true)
            .setInnerBounds(bounds)});
    mOpenImage.setCrossFadeEnabled(true);
    mCollapseImage.setCrossFadeEnabled(true);
  }

  private void transitImage(TransitionDrawable image, boolean forward, boolean animate)
  {
    if (!UiUtils.isVisible(mButton))
      animate = false;

    mButton.setImageDrawable(image);

    if (forward)
      image.startTransition(animate ? BaseMenu.ANIMATION_DURATION : 0);
    else
      image.reverseTransition(animate ? BaseMenu.ANIMATION_DURATION : 0);

    if (!animate)
      image.getDrawable(forward ? 1 : 0).setAlpha(0xFF);
  }

  void show(boolean show)
  {
    //TODO: refactor mAlwaysShow logic, because now we shouldn't display
    // the toggle button when we are in prepare routing state (create JIRA item for that)
    // A temporary solution is the hide() method.
    UiUtils.showIf(mAlwaysShow || show, mButton);
  }

  void hide()
  {
    UiUtils.hide(mButton);
  }

  void setOpen(boolean open, boolean animate)
  {
    transitImage(mOpenImage, open, animate);
  }

  void setCollapsed(boolean collapse, boolean animate)
  {
    transitImage(mCollapseImage, collapse, animate);
  }
}