Android self drawing control

Due to the needs of the company's project, a vertical discontinuity line needs to be drawn as the division line. This is a pit. I've only done horizontal broken lines in the past. I can simply draw them through shape, but remember to turn off hardwareaccelerated, otherwise the horizontal dotted line will still be a solid line. This is also a pit I've been through before. What should I do? If I can't find a native solution, it's just a simple discontinuous line. Can it still embarrass me and other program apes? At this time, the first thing to think of is the self drawing control.

Self drawn control, as its name implies, is that the content displayed by the control is drawn by ourselves. All drawing operations are carried out in the OnDraw () method. Of course, our custom control is a direct subclass of view. For example, the most commonly used textview and ImageView are direct subclasses of view and can also be regarded as self drawing controls. All drawing operations are also in their own ondraw(). Interested students can read the source code of these basic controls. In the first lecture today, we also start to implement our own controls.

Directly on the code, more concise.

The first step is to define the properties of the control in attrs.xml

Define line_ The color attribute is mainly for the sake of control compatibility. You can customize the color of broken lines at will.

Step 2: define the member variables of dashline

Step 3: initialize variables in the construction method

Step 4: Drawing

Step 5: layout and use

The code is relatively concise and has comments, so I won't say much. Look at the results

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>