Android custom view realizes rotating circular pictures

Custom view is an important skill in Android development. Using 2 / 3D rendering related classes provided by Android can achieve many cool effects, which requires a solid programming foundation.

However, custom view is my weakness, so I have been exploring and practicing custom view recently. Today, I wrote a circular image of a rotatecircleimageview that rotates at a constant speed. The implementation method is what I want, but it is certainly not the best implementation method.

There are four steps to customize the view.

1: Custom attributes; 2: Create a custom view and get the custom attributes in the construction method; 3: Override the onmeasure method; 4: Override OnDraw method

Let's start with a renderings

First, create a new attrs.xml custom attribute under RES / values /

Create rotatecircleimageview

Rewrite the three constructors of view, call two parameters with one parameter, and call three parameters with two parameters. Initialize parameters in the three parameter structure.

Rewrite the measurement method: mainly measure the width and height of the package

If the width is the specified size and I want the height to be scaled according to this size, then I need to get the original size of the picture, so I need a tempimage. Why write a temporary bitmap? Because when I tested, I found that if I use this image to directly put bitmap. Createscaledbitmap (image, XX, false); Even if I use image. Getwidth() and image. Getheight() before this line of code, the returned values have become the scaled size instead of the original size, which makes me feel very strange. Is getWidth and getHeight of bitmap asynchronous? I hope people who know can help me answer.

Finally, override the OnDraw method

Code of getcirclebitmap method and sub thread:

When the OnDraw method is executed for the first time, a bitmap rotated by 0.8 degrees is obtained. Then, after 16 milliseconds, the child thread is started to refresh, and OnDraw is executed again to obtain a bitmap rotated by 0.8 degrees again, and so on. If you want to rotate faster, increase the angle of each rotation, but not too large, otherwise the effect is very bad. Card by card. This completes the custom view. It's very simple, but I've been tossing around for a long time. The main reason is that I'm not careful enough when measuring. The implementation methods are completed by ourselves. If there is a better implementation method, please let us know.

Finally, two methods are exposed to the outside

Then you can try it in the layout:

Get the control in the activity and override the click event method of the two buttons:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>