Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Android track Animation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to achieve Android trajectory animation, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The details are as follows

II. Description of requirements

In the middle of the year, his company gave him a picture with only one curve, asking him to dynamically show the whole curve according to the trajectory of the curve, but the gay friend was busy buying off me at the price of a steamed bread, so he wrote the code for him. Let's take a look at the implementation effect first.

On the original image (the trajectory curve of the original image is white, the other parts are transparent, here I set the background to black for easy observation)

Third, analysis and implementation

To be reasonable, I was a little confused when I first got this picture. An irregular curve obviously cannot describe the specific location of the point through a simple equation. I even want to say that it would be nice to design an animation directly.

After complaining, the demand still has to be realized, and there are many common ways to achieve this requirement, such as keeping the upper and lower layers of the picture all the time, and the upper layer constantly clearing the vertical pixels of the original image from left to right. then draw the designed picture from left to friend. However, the scalability of this method is too poor, the bottom picture changes (sliding, discoloration) the upper layer also needs to cooperate.

The method of use here is to map the position of the colored pixels in the picture to the View to be drawn proportionally.

For example, if the size of the original image is 200cm 100, the size of the View to be displayed is 450mm 300, and a point is located at x100Magi y20 in the original picture, then its position in view should be 225pm 60. That is to say, the position of the point on the way is compared to the corresponding size of the original picture and then multiplied by the size of View.

The track can be obtained by getting all the pixel colors, because what is transparent must not be the track, so you can judge the alpha value, and because the track is a line, when we use Path points, in order to minimize unnecessary connections, I use the average method to reduce the width of the curve to 1.

Since the position of the point is calculated in proportion to the size, no matter how strange the size of the picture to be displayed, it can be perfectly adapted.

IV. Implementation code

Public class TestView extends View implements ViewTreeObserver.OnGlobalLayoutListener {private Paint paint = new Paint (); private Bitmap bitmap; private int [] local; boolean enter = false; int count; ViewTreeObserver observer; Canvas canvas; float lastX = 0; float lastY = 0; public TestView (Context context) {super (context);} public TestView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); observer = this.getViewTreeObserver (); observer.addOnGlobalLayoutListener (this); getImageFromAssetsFile ("target.png") / / read pictures} public TestView (Context context, AttributeSet attrs) {super (context, attrs); observer = this.getViewTreeObserver (); observer.addOnGlobalLayoutListener (this); getImageFromAssetsFile ("target.png");} private void getImageFromAssetsFile (String fileName) {AssetManager am = getResources (). GetAssets (); try {InputStream is = am.open (fileName); bitmap = BitmapFactory.decodeStream (is); is.close ();} catch (IOException) {e.printStackTrace () }} private void filterColor () {int w = bitmap.getWidth (); int h = bitmap.getHeight (); int [] pixels = new int [Wendh]; local = new int [w]; / / the position of the save point on the Y axis of the original drawing in terms of width, bitmap.getPixels (pixels,0,bitmap.getWidth (), 0th0membitmap.getWidth (), bitmap.getHeight ()); for (int I = 0; I < wt; iSense +) {int lenght = 0; int total = 0; for (int j = 0bot j)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report