Android Development

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.

Guowei Lv

5 minute read

In this article we talk about animation, specifically property animation. ViewPropertyAnimator View has many properties, like position on screen, color and size. Property animation is the type of animation that animate on those properties. In other words, the animator’s job is to set a specific set of properties of the view to different values many many times in a short period of time. For example, the following code will move the view’s position 500px to the right: