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 the virtual effect of ground glass by android

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "how to achieve the virtual effect of ground glass in android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Ground glass effect:

StackBlur

First of all, in order to achieve the ground glass effect, this paper uses the StackBlur fuzzy algorithm, this algorithm is widely used, can get a very good ground glass effect. In this case, we use its Java implementation code FastBlur.java.

Public static Bitmap doBlur (Bitmap sentBitmap, int radius, boolean canReuseInBitmap)

As you can see, the method is very simple: pass in the bitmap to be virtualized, the virtual program (usually 8), and whether or not to reuse flag.

Then, if we want to falsify the image above, we can pass it into bitmap, which seems to be very simple, but this is not the case.

OOM

If you pass in a large image directly, it is easy to cause OOM memory overflow.

03-11 21 16727/com.wingjay.jayandroid I/art: 16727/com.wingjay.jayandroid I/art: Clamp target GC heap from 109MB to 96MB 03-11 21 16727/com.wingjay.jayandroid I/art 02lav 02purl 02.030 16727-16727/com.wingjay.jayandroid I/art: Clamp target GC heap from 109MB to 96MB 03-11 21 16727/com.wingjay.jayandroid I/art 02lav 02VR 02.031 16727-16727/com.wingjay.jayandroid I/art: Forcing collection of SoftReferences For 30MB allocation 03-11 21 16727/com.wingjay.jayandroid E/art 0216727/com.wingjay.jayandroid E/art 02for 30MB allocation 03-11 21 byte allocation with 02for 30MB allocation 02.035 16727-16727/com.wingjay.jayandroid E/art: 02.036 16727-16727/com.wingjay.jayandroid E/art: Throwing OutOfMemoryError "Failed to allocate a 32175012 16727-16727/com.wingjay.jayandroid D/AndroidRuntime: Shutting down VM" 03-11 21 21 Fraser 02V 02.036 16727

This is the log information I got directly from the original image. You can see that when the virtualization starts, the virtual machine begins to continuously reclaim the memory, including the memory recycling of all soft references. However, it still causes a memory overflow.

That means I can only virtualize the small image in order to prevent memory overflow. But I don't want to change another picture, so we should zoom this picture.

ReScale

Public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter) {}

We can use this function to scale the bitmap. The first three parameters are obvious, in which the width and height can be selected as 1 filter 10 of the original image size; the fourth filter refers to the effect of scaling, and if filter is true, you will get a bitmap with smooth edges, otherwise, you will get bitmap of edge sawtooth and pixelrelated. Here we want to falsify the scaled image, so it doesn't matter the edge effect, filter=false.

So, we're going to use

Int scaleRatio = 10; int blurRadius = 8; Bitmap scaledBitmap = Bitmap.createScaledBitmap (originBitmap, originBitmap.getWidth () / scaleRatio, originBitmap.getHeight / scaleRatio, false); Bitmap blurBitmap = FastBlur.doBlur (scaledBitmap, blurRadius, true); imageView.setScaleType (ImageView.ScaleType.CENTER_CROP); imageView.setImageBitmap (blurBitmap)

You can get the following results:

As can be seen from the picture, first of all, we can make sure that the idea is right; then, we can see that the effect of ground glass is not particularly obvious. To get a virtual effect like iOS, we have two ways:

Increasing the scaleRatio scaling ratio and using the same smaller bitmap to de-virtual can get a better blur effect and help to reduce the memory footprint.

Increasing blurRadius can get a higher degree of virtual, but it will cause CPU to be more intensive.

Here I experiment by increasing the zoom ratio.

ScaleRatio = 20

ScaleRatio = 35

ScaleRatio = 50

ScaleRatio = 100

By comparing the picture above, we can find out the virtual effect that is most suitable for us.

Performance analysis

So, to achieve this effect, is there a risk of damaging the user experience? Next, we analyze it from the point of view of consuming time and occupying memory.

Time Consuming

In order to analyze the time consumed by falsifying a picture, this paper obtains the average time by simultaneously falsifying 100. In order to get a certain understanding of the influence of virtual time and different scaling ratio on time consumption.

Long start = System.currentTimeMillis (); Bitmap scaledBitmap, blurBitmap; int scaleRatio = 10; int loopCount = 100 for (int iTuno; I

ScaleRatio = 10: time-consuming 887ms, average time-consuming 8.87ms

ScaleRatio = 20: time-consuming 224ms, average time-consuming 2.24ms

ScaleRatio = 35: time-consuming 99ms, average time-consuming 0.99ms

ScaleRatio = 50: time-consuming 55ms, average time-consuming 0.55ms

ScaleRatio = 100: time-consuming 29ms, average time-consuming 0.29ms

In order to make it easier for readers to understand the effect, I fitted the following curve through several sets of data:

From the simulation chart, we can see that the time decreases with the increase of the zoom ratio, and when the zoom ratio reaches more than 30, the time consumed is less than 1ms. Therefore, I think it should not cause delay to destroy the user experience at all.

Memory Consuming

Since there is no problem with time, the main problem: memory footprint, so we need to look at the memory used to generate a virtual image.

In order to test the change in memory consumed by virtualizing an image, we change the number of times we falsify, that is, modify the loopCount above and observe the change to memory. Where scaleRatio = 10 to achieve a relatively large memory consumption.

LoopCount = 1

LoopCount = 10

LoopCount = 20

LoopCount = 50

LoopCount = 100

LoopCount = 300

From the memory consumption chart above, you can see that virtualization does take up a certain amount of memory, and if a large number of virtualization occurs at the same time, it will cause memory jitter because the UI thread suddenly loads a lot of bitmap.

This is the end of the content of "how to achieve the virtual effect of ground glass in android". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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