I would like to know what is the difference between SurfaceView and ImageView and their usage scenarios. Both seem to be the same. Kindly direct me if there are proper links which I had probably missed.
Some advantages and differences of a surface view:
Better rendering mechanism. threads can update the surface's content without using a handler. This helps for better performance in games and too much animation.
So if you need to update GUI rapidly or if the rendering takes too much time and affects user experience then Surfaceview is advisable instead of imageview.
Surfaceviews cannot be transparent, they can only appear behind other elements in the view hierarchy.
Surfaceview has dedicated buffer, too. So it costs more resources than imageview and other views.
Here is a reference links which you could refer to understand better.
Difference between SurfaceView and View?
Hope this clears some doubt.
Basically the difference lies on how both the views are being processed internally.
Surface View, has more rendering options. The view implicitly can render the images or animations using the graphics hardware. It doesn't need any third party support ( or makes less use of ) to make the animation work.
Preferably used when you want too many animations to be used. The View renders them automatically to the screen size. Ex: gaming applications.
Image View, is preferably when you want to display more of static images. The View render the images to any layout size. But when you have any animations on the page, the View takes support of GUI related conversions.
Related
To set the transparency level of a view background we use
mView.getBackground().setAlpha(mViewTransparencyLvl);
How to set the blur level of a view background?
Knowing that the background is a shape drawable not an image. Knowing that the user controls the blur degree. Knowing that this view is over part of another view.
Android doesn't provide this functionality for UI elements as iOS does. It's not part of the UI rendering engine and to obtain these kind of effects you need to use third-party libraries which will essentially take a snapshot of the drawing cache, reduce its size for a faster processing and then put the generated bitmap into a view.
Here are some example libraries:
BlurrView
Blurry
Please bear in mind that this will have a serious performance hit on mid-low end phones.
background
I've made a simple app called "LWP+", which shows a cropped image . When the user chooses a photo, I save it into a file, and then do the cropping when needed. I do this by using 2 libraries. One for allowing the user to choose the cropping rectangle, and one for the actual cropping.
The problem
This is just for a normal cropping, but a nicer feature I'd like to add is a way to scroll through the content of the entire given image, at least horizontally.
On a normal Activity, I'd probably use a ViewPager that has an ImageView for each page.
But here a live wallpaper doesn't really have a layout.
What I've found
There isn't much of information about live wallpapers in general, but when I've searched for scrolling of an image in it, I've found just how to move the canvas.
This can actually be a good idea, of somehow having bitmaps (or partial decoding of the image) around the current position, and translating the canvas as needed. I could also use a GestureDetector class to identify flinging, but this is only a tiny fraction of what's needed to do a nice scrolling of a zoomed image.
It is also very complex, as it requires good memory considerations in mind.
If I could use a normal view, I could have used a ViewPager, or even a third library that shows an image and allows to scroll in it freely , like in the gallery app (such as this one).
The question
Given a View of any kind, such as ViewPager or another, is it possible to host it somehow inside the LiveWallpaper, to let it show content, and to handle touch events ?
Alternatively, is there an efficient way to view content as I've written, yet in a live wallpaper? Meaning viewing a portion of a large image, while allowing to scroll though it (like a ViewPager, but maybe even freely like on a photo viewer library, in all directions) ?
I am freelancing to solve crash of an android app. It happens on startup, and I know it is because the activity_main.xml. It has too many views, too many nesting levels. Apart from that (wich I will try to reduce)
what are other strategies to improve layout performance in Android?
The user interface cannot be changed or simplified.
For example:
- for repeated elements (50 - 100), does using 'include' makes it faster?
- the same for defining properties in styles instead of in every item.
I have been recently working on improving performance of app and renderd a fast smooth UI let me share you my experience :
The vision of performance in terms of UI is:
Lower the latency of screen draws
Create fast, consistent frame rates to avoid jank/lag.
And there are some thumb rules for layouts .
Minimum view hierarchy.
minimum background drawbles
minimizing overdraw of window :
minimizing overdraw of Views
using drawble left instead of image and textview aligned horizontally.
using lists/recyclerview very often when repetitive view
and Many more . let me share some links which are really helpful
https://medium.com/#elifbon/android-application-performance-step-1-rendering-ba820653ad3#.pp7hpnv07
https://www.safaribooksonline.com/library/view/high-performance-android/9781491913994/ch04.html
https://riggaroo.co.za/optimizing-layouts-in-android-reducing-overdraw/
https://www.hackerearth.com/notes/rendering-performance-in-android-overdraw/
Letme know if it's helpful. Cheers
You can try following from https://developer.android.com,
Optimizing Layout
Inspect Your Layout
Revise Your Layout
Use Lint
AS well as
Improving Layout Performance
Optimizing Layout Hierarchies
Re-using Layouts with
Loading Views On Demand
Re-using Layouts with
I have answered a related question before and my advice is still applicable in this topic.
Handling loads:
Lazy Loading - Load only those piece of information that is really needed by now. Example : A Movie app: Load only those latest movies rather than those old movies unless the user told you to do so. The idea here is that decided only on which is necessary to load first and load other else later on. The lesser the load the greater the performance is while without sacrificing a good content.
Caching - If you keep on downloading things from your server chances are it might take too long to load and your splash screen will be visible for longer period of time or some of your UI might freeze specially when you do it in the Main thread. With caching you will need to fetch fewer data from the network since you've downloaded some of them already.
Things to consider in creating views.
Avoid a super deep nested views.
Avoid a deep nested weights.
For image loading use some popular library like Picasso, Glide etc.
does using 'include' makes it faster?
A bit, Reusing layouts is particularly powerful as it allows you create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text. It also means that any elements of your application that are common across multiple layouts can be extracted, managed separately, then included in each layout. So while you can create individual UI components by writing a custom View, you can do it even more easily by re-using a layout file.
Source : Official docs
Addendum
When things are still wrong and the Android framework doesn't provide what you really need; the last approach is to create your own view via extending View or ViewGroup. Creating your own view/layout requires time and much effort but you gain more controls since it is your own view/layout implementation, let say you have the power to change the world.
This question is about performance. My goal is to make an animation which flips view vertically. What is better, to flip the actual View inflated from XML, or make a snapshot of that view and flip bitmap image?
One of the ways to flip is described here:
http://2cupsoftech.wordpress.com/2012/09/18/3d-flip-between-two-view-or-viewgroup-on-android/
But how performant is it?? And isn't better to flip a bitmap?
I would suggest using card flip animation as described here. It works fine. (But it depends on your View as well)
Snapshots are heavy in memory. Besides, you have to create the second snapshot too, if the second Activity isn't loaded yet.
I was wondering if it was possible for a SurfaceView to hold multiple ImageView. Each ImageView should be able to move independently. Imagine a table of cards, where each card is to be moved. The cards (ImageViews) are to be added to the surfaceview dynamically. Any ideas? Tried researching it, but to no avail. Thanks!
A SurfaceView is a View, not a ViewGroup. Therefore, it cannot hold any ImageView at all :)
As Romain's answer hints at, you seem to have a fundamental misunderstanding of what SurfaceView is. Take a look at the docs. It's a View for drawing on, not to hold other Views (be they ImageViews or otherwise). You can draw images (including bitmaps) on it, but that's different from holding ImageViews. If you're looking to have a game with a bunch of cards, you probably want to find a tutorial on drawing with SurfaceViews. This might be somewhere to start.