Class: AndroidMotionQuery::StylesheetElement

Inherits:
Object
  • Object
show all
Defined in:
lib/android_query/views.rb

Constant Summary collapse

LAYOUT_SIZE_OPTIONS =
{
  mp: Android::View::ViewGroup::LayoutParams::MATCH_PARENT,
  wc: Android::View::ViewGroup::LayoutParams::WRAP_CONTENT,
}
ORIENTATION_OPTIONS =
{
  vertical: Android::Widget::LinearLayout::VERTICAL,
  horizontal: Android::Widget::LinearLayout::HORIZONTAL,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, layout_params) ⇒ StylesheetElement

Returns a new instance of StylesheetElement.



95
96
97
98
99
# File 'lib/android_query/views.rb', line 95

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

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



83
84
85
# File 'lib/android_query/views.rb', line 83

def params
  @params
end

#radiusObject

Returns the value of attribute radius.



83
84
85
# File 'lib/android_query/views.rb', line 83

def radius
  @radius
end

#viewObject

Returns the value of attribute view.



83
84
85
# File 'lib/android_query/views.rb', line 83

def view
  @view
end

Instance Method Details

#background_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



251
252
253
# File 'lib/android_query/views.rb', line 251

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

#background_image=(image_name) ⇒ Object



255
256
257
258
259
# File 'lib/android_query/views.rb', line 255

def background_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

#click=(method_name) ⇒ Object



216
217
218
# File 'lib/android_query/views.rb', line 216

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

#extra=(something) ⇒ Object



212
213
214
# File 'lib/android_query/views.rb', line 212

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

#get_marginsObject



196
197
198
199
# File 'lib/android_query/views.rb', line 196

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

#get_paddingObject



171
172
173
174
# File 'lib/android_query/views.rb', line 171

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

#gravity=(alignment) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/android_query/views.rb', line 283

def gravity=(alignment)
  gravity_options = {
    top: Android::View::Gravity::TOP,
    left: Android::View::Gravity::LEFT,
    right: Android::View::Gravity::RIGHT,
    bottom: Android::View::Gravity::BOTTOM,
    center: Android::View::Gravity::CENTER,
    bottom_right: Android::View::Gravity::BOTTOM | Android::View::Gravity::RIGHT,
    bottom_left: Android::View::Gravity::BOTTOM | Android::View::Gravity::LEFT,
    center_right: Android::View::Gravity::CENTER | Android::View::Gravity::RIGHT,
    center_left: Android::View::Gravity::CENTER | Android::View::Gravity::LEFT,
    top_right: Android::View::Gravity::TOP | Android::View::Gravity::RIGHT,
    top_left: Android::View::Gravity::TOP | Android::View::Gravity::LEFT,
  }
  self.view.get.gravity = gravity_options[alignment]
end

#height=(h) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/android_query/views.rb', line 118

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

#id=(number) ⇒ Object



101
102
103
104
# File 'lib/android_query/views.rb', line 101

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

#margin=(number) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/android_query/views.rb', line 201

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



191
192
193
194
# File 'lib/android_query/views.rb', line 191

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

#margin_left=(number) ⇒ Object



176
177
178
179
# File 'lib/android_query/views.rb', line 176

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

#margin_right=(number) ⇒ Object



181
182
183
184
# File 'lib/android_query/views.rb', line 181

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

#margin_top=(number) ⇒ Object



186
187
188
189
# File 'lib/android_query/views.rb', line 186

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

#orientation=(o) ⇒ Object



126
127
128
129
130
# File 'lib/android_query/views.rb', line 126

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

#padding=(number) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/android_query/views.rb', line 160

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



155
156
157
158
# File 'lib/android_query/views.rb', line 155

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

#padding_left=(number) ⇒ Object



140
141
142
143
# File 'lib/android_query/views.rb', line 140

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

#padding_right=(number) ⇒ Object



145
146
147
148
# File 'lib/android_query/views.rb', line 145

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

#padding_top=(number) ⇒ Object



150
151
152
153
# File 'lib/android_query/views.rb', line 150

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

#scale_type=(option) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/android_query/views.rb', line 261

def scale_type=(option)
  scale_types = {
    center: Android::Widget::ImageView::ScaleType::CENTER,
    center_crop: Android::Widget::ImageView::ScaleType::CENTER_CROP,
    center_inside: Android::Widget::ImageView::ScaleType::CENTER_INSIDE,
    fit_center: Android::Widget::ImageView::ScaleType::FIT_CENTER,
    fit_end: Android::Widget::ImageView::ScaleType::FIT_END,
    fit_start: Android::Widget::ImageView::ScaleType::FIT_START,
    fit_xy: Android::Widget::ImageView::ScaleType::FIT_XY,
    matrix: Android::Widget::ImageView::ScaleType::MATRIX,
  }
  self.view.get.scaleType = scale_types[option]
end

#text=(t) ⇒ Object



106
107
108
# File 'lib/android_query/views.rb', line 106

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

#text_alignment=(alignment) ⇒ Object



275
276
277
# File 'lib/android_query/views.rb', line 275

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

#text_color=(color) ⇒ Object



279
280
281
# File 'lib/android_query/views.rb', line 279

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

#weight=(number) ⇒ Object



136
137
138
# File 'lib/android_query/views.rb', line 136

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

#weight_sum=(number) ⇒ Object



132
133
134
# File 'lib/android_query/views.rb', line 132

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

#width=(w) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/android_query/views.rb', line 110

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