Class: RMQViewStyler

Inherits:
Object show all
Defined in:
lib/project/ruby_motion_query/stylers/rmq_view_styler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, context) ⇒ RMQViewStyler

Returns a new instance of RMQViewStyler.



7
8
9
10
11
12
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 7

def initialize(view, context)
  @view = view
  @context = context
  @bg_color = nil
  @corner_radius = nil
end

Instance Attribute Details

#bg_colorObject

Returns the value of attribute bg_color.



4
5
6
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 4

def bg_color
  @bg_color
end

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 3

def context
  @context
end

#corner_radius(corner_radius) ⇒ Object

Returns the value of attribute corner_radius.



5
6
7
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 5

def corner_radius
  @corner_radius
end

#viewObject

Returns the value of attribute view.



2
3
4
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 2

def view
  @view
end

Instance Method Details

#background_color=(color) ⇒ Object Also known as: backgroundColor=



14
15
16
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 14

def background_color=(color)
  @view.backgroundColor = @bg_color = convert_color(color)
end

#background_resource=(bg) ⇒ Object



19
20
21
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 19

def background_resource=(bg)
  @view.backgroundResource = bg
end

#convert_color(color) ⇒ Object



140
141
142
143
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 140

def convert_color(color)
  return ColorFactory.from_hex(color) if color.is_a?(String)
  color
end

#convert_gravity(gravity) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 145

def convert_gravity(gravity)
  case gravity
  when :center then Android::View::Gravity::CENTER
  when :left then Android::View::Gravity::LEFT
  else
    gravity
  end
end

#create_drawable(corner_radius) ⇒ Object



162
163
164
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 162

def create_drawable(corner_radius)
  createDrawable(corner_radius)
end

#densityObject



158
159
160
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 158

def density
  @density ||= context.getResources.getDisplayMetrics.density
end

#dp2px(dp_val) ⇒ Object



154
155
156
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 154

def dp2px(dp_val)
  (dp_val * density + 0.5).to_i
end

#finalizeObject

use this if you need to do something after all style methods have been called (e.g. applying layout params)



126
127
128
129
130
131
132
133
134
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 126

def finalize
  create_rounded_bg if corner_radius
  @view.setPadding(padding[:left], padding[:top], padding[:right], padding[:bottom])
  layout_params.setMargins(margin[:left],
    margin[:top],
    margin[:right],
    margin[:bottom]) if layout_params.respond_to?(:setMargins)
  @view.setLayoutParams(layout_params)
end

#gravity=(gravity) ⇒ Object



31
32
33
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 31

def gravity=(gravity)
  layout_params.gravity = convert_gravity(gravity)
end

#layout_align_parent_left=(left_in_parent) ⇒ Object Also known as: layout_alignParentLeft=



49
50
51
52
53
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 49

def layout_align_parent_left=(left_in_parent)
  left = left_in_parent ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::ALIGN_PARENT_LEFT, left)
end

#layout_align_parent_right=(right_in_parent) ⇒ Object Also known as: layout_alignParentRight=



56
57
58
59
60
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 56

def layout_align_parent_right=(right_in_parent)
  right = right_in_parent ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::ALIGN_PARENT_RIGHT, right)
end

#layout_center_in_parent=(center_in_parent) ⇒ Object Also known as: layout_centerInParent=, layout_centerVertical=



35
36
37
38
39
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 35

def layout_center_in_parent=(center_in_parent)
  center = center_in_parent ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::CENTER_IN_PARENT, center)
end

#layout_center_vertical=(center_vertical) ⇒ Object



42
43
44
45
46
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 42

def layout_center_vertical=(center_vertical)
  center = center_vertical ? Android::Widget::RelativeLayout::TRUE :
    Android::Widget::RelativeLayout::FALSE
  layout_params.addRule(Android::Widget::RelativeLayout::CENTER_VERTICAL, center)
end

#layout_gravity=(gravity) ⇒ Object



116
117
118
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 116

def layout_gravity=(gravity)
  layout_params.gravity = convert_gravity(gravity)
end

#layout_height=(layout_height) ⇒ Object



27
28
29
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 27

def layout_height=(layout_height)
  layout_params.height = convert_dimension_value(layout_height)
end

#layout_paramsObject



136
137
138
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 136

def layout_params
  @layout_params ||= @view.getLayoutParams()
end

#layout_weight=(weight) ⇒ Object

this can only be used on a widget that’s within a LinearLayout



112
113
114
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 112

def layout_weight=(weight)
  layout_params.weight = weight
end

#layout_width=(layout_width) ⇒ Object



23
24
25
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 23

def layout_width=(layout_width)
  layout_params.width = convert_dimension_value(layout_width)
end

#margin=(m) ⇒ Object



107
108
109
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 107

def margin=(m)
  margin[:left] = margin[:top] = margin[:right] = margin[:bottom] = dp2px(m)
end

#margin_bottom=(m_bottom) ⇒ Object Also known as: marginBottom=



102
103
104
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 102

def margin_bottom=(m_bottom)
  margin[:bottom] = dp2px(m_bottom)
end

#margin_left=(m_left) ⇒ Object Also known as: marginLeft=



87
88
89
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 87

def margin_left=(m_left)
  margin[:left] = dp2px(m_left)
end

#margin_right=(m_right) ⇒ Object Also known as: marginRight=



97
98
99
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 97

def margin_right=(m_right)
  margin[:right] = dp2px(m_right)
end

#margin_top=(m_top) ⇒ Object Also known as: marginTop=



92
93
94
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 92

def margin_top=(m_top)
  margin[:top] = dp2px(m_top)
end

#padding=(pad) ⇒ Object



83
84
85
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 83

def padding=(pad)
  padding[:left] = padding[:top] = padding[:right] = padding[:bottom] = dp2px(pad)
end

#padding_bottom=(pad_bottom) ⇒ Object Also known as: paddingBottom=



78
79
80
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 78

def padding_bottom=(pad_bottom)
  padding[:bottom] = dp2px(pad_bottom)
end

#padding_left=(pad_left) ⇒ Object Also known as: paddingLeft=



63
64
65
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 63

def padding_left=(pad_left)
  padding[:left] = dp2px(pad_left)
end

#padding_right=(pad_right) ⇒ Object Also known as: paddingRight=



73
74
75
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 73

def padding_right=(pad_right)
  padding[:right] = dp2px(pad_right)
end

#padding_top=(pad_top) ⇒ Object Also known as: paddingTop=



68
69
70
# File 'lib/project/ruby_motion_query/stylers/rmq_view_styler.rb', line 68

def padding_top=(pad_top)
  padding[:top] = dp2px(pad_top)
end