1. Overview It seems that I saw a very unique way of advertising display when I was browsing Zhihu some time ago. That is, on the list page, a certain item shows part of the advertising picture behind it, and as the list scrolls, all the pictures will gradually be displayed. When I first saw it, I wanted to implement it, but I have always been lazy. Some people also asked me how to implement it in the background of the official account. Today I will explain it to you. Of course, some custom Views are not difficult at present, so the explanation in this article will provide some guidance on implementation ideas. I believe it will not be such a boring article. I hope it will be of some help to you. Well, I can’t find this effect on Zhihu anymore. I tried multiple historical versions but still couldn’t find it. So I can only post the effect picture of the implementation~ The effect diagram is as follows: Choose 1 from 2, which rendering do you like? 2. Ideas Okay, let’s put aside other things and determine the goal of this article: To display a picture in a list:
In other words, we need to change the displayed part of the image when the list scrolls. Two points:
To combine them, we listen to the scrolling dy of the list, pass it to our image control, set the translate, and then draw it. At this point, the idea is very clear, and this thing can definitely be done. Preliminary plan: customize a View, draw the bitmap yourself, expose setDy(dy) to the outside world, and then redraw the canvas based on dy offset. Now that I have a preliminary plan, I am basically not panicking anymore. So what should I think about? Can we use existing controls, such as ImageView? Of course you can. This saves us from declaring a property to accept the image. We write a subclass and still use it by setting src. Let’s inherit ImageView and implement it first. Implementation First, let's write a fake list. Since RV is used more and more, let's use RecyclerView. layout The main layout file, a RecyclerView is enough:
Item layout file:
Very simple, ignore the AdImageViewVersion1 class for now, this will be our specific implementation class. From the layout file, you can see that we only use one item layout file, and then use visible and gone controls to display different forms. Activity
Just set the data, Adapter is used here
You can use any Adapter encapsulation class you like. At this point, a list page is displayed, and every 6 items will be displayed as pictures. No more screenshots, just imagine... Now it is officially starting to be realized. Custom AdImageView
Because we want to draw, we convert the drawable into a bitmap here, and then we display the top by default, so we need a minimum offset, which is the height of the control. We have done all these things in onSizeChanged. And we scale the bitmap according to the current control width, and store the scaled size in mBitmapRectF for easy drawing. Then the next step is drawing. Remember that during the drawing process, we mainly use translate to control the drawing area, so we also need to expose a setDy method to the outside world. So, our code is roughly like this:
When setting Dy, we make a boundary judgment. In the minimum case, we offset -mMinDy to display the bottom of the picture. ***When we use the image height -mMinDy, we show the top part. So we make a minimum and maximum value judgment on the passed value. Then when drawing, it is simple, first translate the dy distance, and then draw. Our custom View part ends here, with very little code~ Combine with RecyclerView The next step is to pass dy to us when RecyclerView scrolls.
Through addOnScrollListener listening, when scrolling, get all visible items and find the item that is displaying the picture. Then call setDy, the value of dy is mLinearLayoutManager.getHeight() - view.getTop(), when the view appears from the top, it is 0, when the view reaches the top, it is the height of the current rv. You can reasonably use the value passed in by setDy to make a movement difference, display the area from top to bottom, etc. That's it~~ To achieve it in one sentence: keep changing dy when scrolling, and then translate to draw it. 4. Think Again Looking at this code, it seems that drawableToBitamp looks very unpleasant and is also the part that consumes more memory. Let's think about it again: Drawable itself can be drawn, why do we need to convert it into a bitmap? It seems to make sense. ImageView itself draws Drawable. What we need to control is that the drawing range of this Drawable should be large enough and cannot be affected by the width and height of the control itself, causing the image to be flattened. There seems to be a method:
That's easy. Just remove the drawable2bitmap code and use the original drawing directly. The only thing we have to do is to set the bounds and do a translate dy. Complete code:
It is achieved with just a short code, which looks much more pleasing to the eye~~ Here is the effect diagram: The effect picture mainly depends on the words, you know! Well, this article summarizes:
The example is relatively simple, have a nice day ~~ |
<<: Spam SMS filtering app based on iOS 11 machine learning: Pandas eat SMS
>>: iOS 11 installation rate has reached 59%, but it is lower than iOS 10 during the same period
After the App Store developer website was updated,...
I heard that yogurt is better than pure milk, is ...
How much does it cost to attract investment for t...
Nowadays, we play with our phones every day, and ...
The social security exemption policy was introduc...
Why did the horse-faced skirt become the "Ne...
For flower shop owners, offline channels can no l...
Regarding the question of how to quickly build a ...
Fan Xiaoshi short video course + e-commerce cours...
Wood type printing Author: Lei Biyu Source: "...
This autumn, I was wrapped tightly in the fragran...
There is no doubt that in the battle for the next...
In rural areas, village cadres are the leaders of ...
Do you know about the Sidewinder missile? In the ...