Class: AMQStylesheetElement

Inherits:
Object
  • Object
show all
Defined in:
lib/android_motion_query/stylesheet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, layout_params) ⇒ AMQStylesheetElement

Returns a new instance of AMQStylesheetElement.



81
82
83
84
85
# File 'lib/android_motion_query/stylesheet.rb', line 81

def initialize(view, layout_params)
  self.view = view
  self.params = layout_params.new(LAYOUT_SIZE_OPTIONS[:mp], LAYOUT_SIZE_OPTIONS[:mp])
  self
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



79
80
81
# File 'lib/android_motion_query/stylesheet.rb', line 79

def params
  @params
end

#radiusObject

Returns the value of attribute radius.



79
80
81
# File 'lib/android_motion_query/stylesheet.rb', line 79

def radius
  @radius
end

#viewObject

Returns the value of attribute view.



79
80
81
# File 'lib/android_motion_query/stylesheet.rb', line 79

def view
  @view
end

Instance Method Details

#background_color=(color) ⇒ Object



247
248
249
# File 'lib/android_motion_query/stylesheet.rb', line 247

def background_color=(color)
  self.view.get.backgroundColor = AMQColor.parse_color(color.to_s)
end

#border_color=(color) ⇒ Object

TODO find a solution for rounded corners (with or without images) def corner_radius=(radius)

self.radius ||= radius
drawable = self.view.get.getDrawable
if drawable
  self.draw_image_with_radius(drawable, self.radius)
else
  shape = Android::Graphics::GradientDrawable.new
  shape.cornerRadius = self.radius
  self.view.get.background = shape
end

end TODO find a solution for rounded corners (with or without images) def draw_image_with_radius(image, radius)

self.radius ||= radius
width = image.getWidth
height = image.getHeight
result = Android::Graphics::Bitmap.createBitmap(width, height, Android::Graphics::Bitmap::Config::ARGB_8888)
canvas = Android::Graphics::Canvas.new(result)
canvas.drawARGB(0, 0, 0, 0)
paint = Android::Graphics::Paint.new
paint.antiAlias = true
paint.color = AQColor.parse_color('#000000')
rect = Android::Graphics::Rect.new(0, 0, width, height)
rect_f = Android::Graphics::RectF.new(rect)
canvas.drawRoundRect(rect_f, self.radius, self.radius, paint)
paint.xfermode = Android::Graphics::PorterDuffXfermode.new(Android::Graphics::PorterDuff::Mode::SRC_IN)
canvas.drawBitmap(raw, rect, rect, paint)
result

end



238
239
240
241
242
243
244
245
# File 'lib/android_motion_query/stylesheet.rb', line 238

def border_color=(color)
  shape = Android::Graphics::Drawable::ShapeDrawable.new
  shape.shape = Android::Graphics::Drawable::Shapes::RectShape.new
  shape.paint.color = AMQColor.parse_color(color.to_s)
  shape.paint.strokeWidth = 1
  shape.paint.style = Android::Graphics::Paint::Style::STROKE
  self.view.get.background = shape
end

#click=(method_name) ⇒ Object



203
204
205
# File 'lib/android_motion_query/stylesheet.rb', line 203

def click=(method_name)
  self.view.get.onClickListener = AMQClickListener.new(self.view.activity, method_name)
end

#extra=(something) ⇒ Object



199
200
201
# File 'lib/android_motion_query/stylesheet.rb', line 199

def extra=(something)
  self.view.extra = something
end

#get_marginsObject



183
184
185
186
# File 'lib/android_motion_query/stylesheet.rb', line 183

def get_margins
  lp = self.params
  [lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin]
end

#get_paddingObject



158
159
160
161
# File 'lib/android_motion_query/stylesheet.rb', line 158

def get_padding
  v = self.view.get
  [v.getPaddingLeft, v.getPaddingTop, v.getPaddingRight, v.getPaddingBottom]
end

#gravity=(alignment) ⇒ Object



294
295
296
297
298
299
300
301
# File 'lib/android_motion_query/stylesheet.rb', line 294

def gravity=(alignment)
  if GRAVITY_OPTIONS.keys.include? alignment
    self.view.get.gravity = GRAVITY_OPTIONS[alignment]
  else
    puts "The value #{alignment} is not a supported gravity value. Defaulting to center."
    self.gravity = :center
  end
end

#height=(h) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/android_motion_query/stylesheet.rb', line 105

def height=(h)
  if h == :mp || h == :wc
    self.params.height = LAYOUT_SIZE_OPTIONS[h]
  else
    self.params.height = h
  end
end

#hint=(t) ⇒ Object

def font=(font)

if TYPE_FACE_OPTIONS.keys.include? font
  self.view.get.typeFace = TYPE_FACE_OPTIONS[font]
else
  raise "The value #{font} is not a supported font value. Use one of #{TYPE_FACE_OPTIONS.keys}"
end

end



281
282
283
# File 'lib/android_motion_query/stylesheet.rb', line 281

def hint=(t)
  self.view.get.hint = t
end

#id=(number) ⇒ Object



87
88
89
90
# File 'lib/android_motion_query/stylesheet.rb', line 87

def id=(number)
  
  self.view.get.id = number
end

#image=(image_name) ⇒ Object



251
252
253
254
255
# File 'lib/android_motion_query/stylesheet.rb', line 251

def image=(image_name)
  context = self.view.get.getContext
  resource_id = context.getResources.getIdentifier(image_name, "drawable", context.getPackageName)
  self.view.get.setImageResource(resource_id)
end

#input_type=(text_type) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/android_motion_query/stylesheet.rb', line 285

def input_type=(text_type)
  if INPUT_TYPES.keys.include? text_type
    self.view.get.inputType = INPUT_TYPES[text_type]
  else
    puts "The value #{text_type} is not a supported input_type value. Defaulting to normal text."
    self.input_type = :normal
  end
end

#layout_gravity=(alignment) ⇒ Object



303
304
305
306
307
308
309
310
# File 'lib/android_motion_query/stylesheet.rb', line 303

def layout_gravity=(alignment)
  if GRAVITY_OPTIONS.keys.include? alignment
    self.params.gravity = GRAVITY_OPTIONS[alignment]
  else
    puts "The value #{alignment} is not a supported gravity value. Defaulting to center."
    self.params.gravity = :center
  end
end

#margin=(number) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/android_motion_query/stylesheet.rb', line 188

def margin=(number)
  if number.class == Array && number.count == 4
    left, top, right, bottom = number
    self.params.setMargins(left, top, right, bottom)
  elsif number.class == Fixnum
    self.params.setMargins(number, number, number, number)
  else
    raise "Invalid value (#{number}) set as margin for #{self.view.get}"
  end
end

#margin_bottom=(number) ⇒ Object



178
179
180
181
# File 'lib/android_motion_query/stylesheet.rb', line 178

def margin_bottom=(number)
  left, top, right, bottom = get_margins
  self.margin = [left, top, right, number]
end

#margin_left=(number) ⇒ Object



163
164
165
166
# File 'lib/android_motion_query/stylesheet.rb', line 163

def margin_left=(number)
  left, top, right, bottom = get_margins
  self.margin = [number, top, right, bottom]
end

#margin_right=(number) ⇒ Object



168
169
170
171
# File 'lib/android_motion_query/stylesheet.rb', line 168

def margin_right=(number)
  left, top, right, bottom = get_margins
  self.margin = [left, top, number, bottom]
end

#margin_top=(number) ⇒ Object



173
174
175
176
# File 'lib/android_motion_query/stylesheet.rb', line 173

def margin_top=(number)
  left, top, right, bottom = get_margins
  self.margin = [left, number, right, bottom]
end

#orientation=(o) ⇒ Object



113
114
115
116
117
# File 'lib/android_motion_query/stylesheet.rb', line 113

def orientation=(o)
  if o == :vertical || o == :horizontal
    self.view.get.orientation = ORIENTATION_OPTIONS[o]
  end
end

#padding=(number) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/android_motion_query/stylesheet.rb', line 147

def padding=(number)
  if number.class == Array && number.count == 4
    left, top, right, bottom = number
    self.view.get.setPadding(left, top, right, bottom)
  elsif number.class == Fixnum
    self.view.get.setPadding(number, number, number, number)
  else
    raise "Invalid value (#{number}) set as padding for #{self.view.get}"
  end
end

#padding_bottom=(number) ⇒ Object



142
143
144
145
# File 'lib/android_motion_query/stylesheet.rb', line 142

def padding_bottom=(number)
  left, top, right, bottom = get_padding
  self.padding = [left, top, right, number]
end

#padding_left=(number) ⇒ Object



127
128
129
130
# File 'lib/android_motion_query/stylesheet.rb', line 127

def padding_left=(number)
  left, top, right, bottom = get_padding
  self.padding = [number, top, right, bottom]
end

#padding_right=(number) ⇒ Object



132
133
134
135
# File 'lib/android_motion_query/stylesheet.rb', line 132

def padding_right=(number)
  left, top, right, bottom = get_padding
  self.padding = [left, top, number, bottom]
end

#padding_top=(number) ⇒ Object



137
138
139
140
# File 'lib/android_motion_query/stylesheet.rb', line 137

def padding_top=(number)
  left, top, right, bottom = get_padding
  self.padding = [left, number, right, bottom]
end

#scale_type=(option) ⇒ Object



257
258
259
# File 'lib/android_motion_query/stylesheet.rb', line 257

def scale_type=(option)
  self.view.get.scaleType = SCALE_TYPES[option]
end

#text=(t) ⇒ Object



92
93
94
95
# File 'lib/android_motion_query/stylesheet.rb', line 92

def text=(t)
  self.view.get.text = t
  self
end

#text_alignment=(alignment) ⇒ Object



261
262
263
# File 'lib/android_motion_query/stylesheet.rb', line 261

def text_alignment=(alignment)
  self.gravity = alignment
end

#text_color=(color) ⇒ Object



265
266
267
# File 'lib/android_motion_query/stylesheet.rb', line 265

def text_color=(color)
  self.view.get.textColor = AMQColor.parse_color(color.to_s)
end

#text_size=(size) ⇒ Object



269
270
271
# File 'lib/android_motion_query/stylesheet.rb', line 269

def text_size=(size)
  self.view.get.textSize = size
end

#weight=(number) ⇒ Object



123
124
125
# File 'lib/android_motion_query/stylesheet.rb', line 123

def weight=(number)
  self.params.weight = number
end

#weight_sum=(number) ⇒ Object



119
120
121
# File 'lib/android_motion_query/stylesheet.rb', line 119

def weight_sum=(number)
  self.view.get.weightSum = number
end

#width=(w) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/android_motion_query/stylesheet.rb', line 97

def width=(w)
  if w == :mp || w == :wc
    self.params.width = LAYOUT_SIZE_OPTIONS[w]
  else
    self.params.width = w
  end
end