Class: DroidProj::Android::Drawable
- Inherits:
-
Object
- Object
- DroidProj::Android::Drawable
- Defined in:
- lib/droidproj/drawable.rb
Constant Summary collapse
- FINAL_FILE_PREFIX =
"x_"- SIZES =
[:hdpi, :ldpi, :mdpi, :xhdpi]
Instance Attribute Summary collapse
-
#active_state ⇒ Object
The current state being set for this drawable.
-
#name ⇒ Object
The final name for this drawable, used in your code.
-
#states ⇒ Object
Hash of STATE_HASH =>.
Instance Method Summary collapse
-
#initialize ⇒ Drawable
constructor
A new instance of Drawable.
-
#sorted_states ⇒ Object
Internal: Sort
self.statesby how long each state is (because Android wants the XML file to be sorted by specificity). -
#state(active_state, &block) ⇒ Object
Public: The DSL for creating drawables corresponding to a certain state.
- #to_size_buckets ⇒ Object
-
#xml_string ⇒ Object
Public: The StateList XML representation of this drawable, used by Android.
Constructor Details
#initialize ⇒ Drawable
Returns a new instance of Drawable.
15 16 17 18 |
# File 'lib/droidproj/drawable.rb', line 15 def initialize @active_state = {} @states = {} end |
Instance Attribute Details
#active_state ⇒ Object
The current state being set for this drawable
10 11 12 |
# File 'lib/droidproj/drawable.rb', line 10 def active_state @active_state end |
#name ⇒ Object
The final name for this drawable, used in your code
8 9 10 |
# File 'lib/droidproj/drawable.rb', line 8 def name @name end |
#states ⇒ Object
Hash of STATE_HASH =>
6 7 8 |
# File 'lib/droidproj/drawable.rb', line 6 def states @states end |
Instance Method Details
#sorted_states ⇒ Object
Internal: Sort self.states by how long each state is
(because Android wants the XML file to be sorted by specificity)
Returns an Array relating to self.states as [:key, :value], sorted by :key length
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/droidproj/drawable.rb', line 126 def sorted_states keys = self.states.keys keys.sort_by! {|x| x.to_s.length }.reverse! sorted_hash = keys.map {|key| [key, self.states[key]] } sorted_hash end |
#state(active_state, &block) ⇒ Object
Public: The DSL for creating drawables corresponding to a certain state
active_state - The Hash of options to set the active_state &block - A block to be execute with the new active_state
Examples
drawable.state(enabled: true, focused: false) do
# In this block, all drawables are set with the above state
hdpi 'image.png'
end
Returns the Drawable instance being used
33 34 35 36 37 38 |
# File 'lib/droidproj/drawable.rb', line 33 def state(active_state, &block) self.active_state = Hash[active_state.map {|k, v| [k.to_sym, v] }] block.call self.active_state = {} self end |
#to_size_buckets ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/droidproj/drawable.rb', line 81 def to_size_buckets size_buckets = {} self.states.each do |state, drawable_states| drawable_states.each do |drawable_state| size_buckets[drawable_state.size] ||= [] size_buckets[drawable_state.size] << drawable_state end end size_buckets end |
#xml_string ⇒ Object
Public: The StateList XML representation of this drawable, used by Android
Returns the String representation
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/droidproj/drawable.rb', line 95 def xml_string str = %Q{<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> } self.sorted_states.each do |state, drawable_states| drawable_states.each do |drawable_state| if !str.include?(drawable_state.xml_string) str << %Q{ #{drawable_state.xml_string} } end end end default_drawable = "<item android:drawable=\"@drawable/#{FINAL_FILE_PREFIX}#{self.name}\" />\n" if !str.include?(default_drawable) str << %Q{ #{default_drawable}} end str << %Q{</selector>} end |