Android Development

Android Custom View 102 (Part 9)

A RatingBar Example

Guowei Lv

1 minute read

There is nothing new here, just another exercise. Two things to be careful with: Try to call validate() as fewer times as possible, it is expensive. If you want to handle the touch events in this manner, remember to return true from onTouchEvent(). Otherwise, only the first event will be triggered(most likely DOWN in this case) and not the succeeding ones (like the MOVES). Source Code

Guowei Lv

1 minute read

Let’s take a look at what we are going to build. The secret is to clip canvas before drawing the text! private fun drawText(canvas: Canvas, paint: Paint, start: Int, end: Int) { canvas.save() rect.set(start, 0, end, height) canvas.clipRect(rect) val textString = text.toString() paint.getTextBounds(textString, 0, textString.length, bounds) val x = width / 2 - bounds.width() / 2 val fontMetrics = changePaint.fontMetrics val dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.

Guowei Lv

1 minute read

Today we are going to write a custom view that count your steps.

There are 2 parts to draw: the arcs and the text. The arcs are easy to draw, only thing to keep in mind is to remember the border width. The tricky part is to draw the text. Suppose we want the text to be in the center of the view. Since the canvas.drawText() will use the baseline to draw.

Realm Guide: From Zero to Give Up

Curated list of realm learning resources

Guowei Lv

1 minute read

You know nothing about realm, then watch this video first -> Now you should know the basic basics, then take a look at the official doc. You think you know it all until you try to combine with RxJava. Now you are super puzzled, look at some examples with full source code. Finally you realize that you have to do all this in clean architecture and decided to give up by turning Realm into a NoSQL version SQLite.

Guowei Lv

2 minute read

In this article we talk about drawing text. Let’s see how to implement a view which simply displays some text. Let’s call it MyTextView. This is how it will look like: <!– Omitted constraintlayout related stuff–> <com.example.MyTextView.MyTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:background="@color/colorAccent" app:myTextColor="#000000" app:myText="Hello World! My love!" app:myTextSize="24sp" /> Let’s first define the custom attributes of the view, in values/attrs.xml <resources> <declare-styleable name="MyTextView"> <attr format="string" name="myText" /> <attr format="color" name="myTextColor" /> <attr format="dimension" name="myTextSize" /> </declare-styleable> </resources> Then in the MyTextView.