Skip to content

Commit

Permalink
Add RoboView.
Browse files Browse the repository at this point in the history
  • Loading branch information
zbsz committed Jun 10, 2015
1 parent 3ef07e0 commit 906999d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
Binary file added src/main/assets/Roboto-Light.ttf
Binary file not shown.
Binary file added src/main/assets/Roboto-Regular.ttf
Binary file not shown.
Binary file added src/main/assets/Roboto-Thin.ttf
Binary file not shown.
9 changes: 9 additions & 0 deletions src/main/res/values/attrs.xml
Expand Up @@ -9,4 +9,13 @@
<attr name="errorDrawable" format="reference" />
</declare-styleable>


<declare-styleable name="RoboView">
<attr name="fontFamily" format="enum" >
<enum name="regular" value="0" />
<enum name="light" value="1" />
<enum name="thin" value="2" />
</attr>
</declare-styleable>

</resources>
33 changes: 33 additions & 0 deletions src/main/scala/com/geteit/view/GtViewGroup.scala
@@ -0,0 +1,33 @@
package com.geteit.view

import android.view.View.MeasureSpec
import android.view.{ViewGroup, View}
import com.geteit.util.GtObjHandler

trait GtViewGroup extends Seq[View] {
self: ViewGroup =>

def length = getChildCount
def apply(i: Int) = getChildAt(i)
def iterator = new Iterator[View] {
var index = 0
def hasNext = index < getChildCount
def next() = {
val v = getChildAt(index)
index += 1
v
}
}

def relayout() {
forceLayout()
GtViewGroup.relayoutHandler !! this
}
}

object GtViewGroup {
lazy val relayoutHandler = new GtObjHandler({ (view: View) =>
view.measure(MeasureSpec.makeMeasureSpec(view.getWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(view.getHeight, MeasureSpec.EXACTLY))
view.layout(view.getLeft, view.getTop, view.getRight, view.getBottom)
})
}
47 changes: 47 additions & 0 deletions src/main/scala/com/geteit/view/RoboView.scala
@@ -0,0 +1,47 @@
package com.geteit.view

import android.content.Context
import android.graphics.Typeface
import android.support.v7.widget.{AppCompatCheckBox, AppCompatTextView}
import android.util.AttributeSet
import android.widget.TextView
import com.geteit.app.R

import scala.collection.mutable

trait RoboView { self: TextView =>
import RoboView._

protected def initFont(attrs: AttributeSet)(implicit context: Context) {
val a = context.obtainStyledAttributes(attrs, R.styleable.RoboView)
val typeId = a.getInt(R.styleable.RoboView_fontFamily, 0)
setTypeface(roboTypeface(typeId))
a.recycle()
}
}

object RoboView {
val typefaceNames = Array("Roboto-Regular.ttf", "Roboto-Light.ttf", "Roboto-Thin.ttf")
val typefaces = new mutable.HashMap[String, Typeface]

def roboTypeface(id: Int)(implicit context: Context) = {
val name = typefaceNames(id)
typefaces.getOrElseUpdate(name, Typeface.createFromAsset(context.getAssets, name))
}
}

class RoboTextView(context: Context, attrs: AttributeSet, style: Int) extends AppCompatTextView(context, attrs, style) with RoboView {

def this(context: Context, attrs: AttributeSet) = this(context, attrs, 0)
def this(context: Context) = this(context, null, 0)

initFont(attrs)(context)
}

class RoboCheckBox(context: Context, attrs: AttributeSet, style: Int) extends AppCompatCheckBox(context, attrs, style) with RoboView {

def this(context: Context, attrs: AttributeSet) = this(context, attrs, 0)
def this(context: Context) = this(context, null, 0)

initFont(attrs)(context)
}

0 comments on commit 906999d

Please sign in to comment.