class DroidProj::Android::Drawable

Constants

FINAL_FILE_PREFIX
SIZES

Attributes

active_state[RW]

The current state being set for this drawable

name[RW]

The final name for this drawable, used in your code

states[RW]

Hash of STATE_HASH =>

Public Class Methods

new() click to toggle source
# File lib/droidproj/drawable.rb, line 15
def initialize
  @active_state = {}
  @states = {}
end

Public Instance Methods

sorted_states() click to toggle source

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

# 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) click to toggle source

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

# 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() click to toggle source
# 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() click to toggle source

Public: The StateList XML representation of this drawable, used by Android

Returns the String representation

# 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