Create stopwatch timerclock countdown timer with image flip every second android - android

I was looking over internet a lot, but only found examples how to text is changed every second or show even miliseconds.
I was using those examples to change picture when second was change, but timer was not accurate anymore. On image change the app respond with random delay for about 20 milisec or 200 milisec.
I was using class Runnable with postDelay and AsynTask where onPostExecute refresh UI, but both working exactly the same with lag.
What is the best practice to change picture every second?
And in the future I have an idea to start animation of 5 images every second (more fancy clock timer).
I would post my code, but now is really a mess of all examples I found till now.

well you have not shared the code so I have to guess only,
I would suggest to have the images as bitmap or drawable initialized already, may be in a hashtable.
After that, start your timer and place the code to replace the image at right place.
Point is you should not spend time other than replacing image in you thread.
Edited:
If you want to do it via code :
before starting your animation create an array of Bitmaps not drawable ids (or the object of Drawble class).
Why because you need to save time spent by system in creating the bitmaps from the ids you provide in array and then set to you image view.
Also, you can use AnimateDrawbles for this which is very easy optimized and reliable.
here is a sample.
Just tune the duration variable in the xml according to your need.

Related

Android OpenGl: How change single TextureView bitmap?

I have a list of photos in my activity.
I should render those concurrently. so I find the best way to have 3 TextureView at first and change the position of them on scrolling the list. If I create EGLThread each time, It runs correctly, but it has a little delay to update the texture. so I need to have EGLThreads permanently, but I don't know to change the bitmap of each EGLThread. I try so many codes but I couldn't find the solution.
I solved.
should run code in postRunnable in EGLThread

Preparing LruCache in Appwidget

i have an appwidget which starts an activity as user clicks it.
In my Activity i have a gridview containing relatively small Drawables(Images, because drawable could be mor than just Images) but user can size them.
I noticed that it takes too Long to size them at runtime when Scrolling trough the gridview.
I want to prepare an lruCache only one time in my appWidget's onUpdate which is called at the very beginning when the user places the appwidget on the Screen.
The problem
When i define an lruCache in my appWidget with
private final int lruCacheSize = (int) (Runtime.getRuntime().maxMemory()/1024);
private LruCache<String, BitmapDrawable> myLruCache;
...
myLruCache = new LruCache<String, BitmapDrawable>(lruCacheSize) {
#Override
protected int sizeOf(String key, BitmapDrawable value) {
// TODO Auto-generated method stub
return super.sizeOf(key, value);
}
};
Will it only exist as Long as the process exist of the appWidget? Or does the Cache-File from lruCache stays in my cacheDir of my app? Or will it be deleted after the process of the appWidget is finished? If it does exist over the process-lifetime of my appWidget, how can i Access it from my Activity?
I don't want to create everytime the user clicks on the appWidget a LruCache and fill it up with all the relatively small Images the gridview will Need later. I want to do it once, or if user clears the Cache which will be checked every hour(to save battery).
The Question
How can i achieve that? Or is there a much better/ simpler way.
Time is not really (if it happens once at the very beginning) the Problem, i notify the user that the Cache is being prepared when placing the appWidget on the Screen.
Any help is appreciated.
Update regarding CommonsWare answere
Use Traceview to determine specifically why your implementation is slow.
The app Scrolling is slow because i provide three sizes (small, medium, large) and i scale them at while Scrolling trough my gridview.(Because it would take several seconds to scale them once at activity Startup, so i don't want that).
See here what i do in my imageview which will later Show the drawable(which is an appIcon):
android:scaleType="fitXY"
android:adjustViewBounds="true"
This causes the lag because i do this for everyimage depending if selected scale size is small medium or large.
There is no "Cache-File" in your code.
So then i i'm misunderstanding something. Doesn't the lruCache create an Cache-File in the Cache-Directory of my application? If not how does in works?
First, you cannot force the user to install the app widget. Work on solving the actual performance problem, rather than trying to build some optimization that will not help all users.
My "app" is only an appwidget. There is not appIcon like at FaceBook. It is only an appWidget when he doenloads my app. Which starts an activity when you click on the button.
Second, your process can readily be terminated milliseconds after onUpdate() completes. Do not fill a cache, only to have it never be used.
I want to use the Cache which i want to fill with the Drawables in the onUpdate of the appWidget, and then i want to use These Drawables from the Cache in my activity. So i don't understand why i never would use the Cache? Maybe i'm misunderstanding something.
Picasso would give you better performance from the user's standpoint.
Does it fit my Needs after the update right now?
---------------------------------------------------------------------------------------------
Update 2
Since the scaling should be done by the GPU and take microseconds, I have difficulty believing that is your problem. What specifically did Traceview show you that made you think that scaling has something to do with this?
I noticed that the Scrolling is very fluid at medium size because that's nearly the origin size of the appIcon from the PackageManager. If scroll trough large, where only 2 or 3 appIcons are displayed per row (at medium there are 5 or 6 displayed but it is much more fluid)m it lags. (With the same logic behind it). So the only Logical answere can be scaling in the XML at the ImageView. As i commented that XML-Scaling out and scaled the appIcons to the size Large and put them directly scalled to to my Adapter for GridView it runs really smooth( Only one or to really small lags at the beginning because convertView is null??)
onUpdate() will be called much more frequently than the user will actually use your app widget.
That's right after every onclick or at the specified time. But i check if the Cache has been cleared or not, if not don't Change anything, if so load Drawables to Cache.
I noticed that it takes too Long to size them at runtime when Scrolling trough the gridview.
Use Traceview to determine specifically why your implementation is slow.
Will it only exist as Long as the process exist of the appWidget?
It will only exist for the lifetime of the process of your app.
Or does the Cache-File from lruCache stays in my cacheDir of my app?
There is no "Cache-File" in your code.
I don't want to create everytime the user clicks on the appWidget a LruCache and fill it up with all the relatively small Images the gridview will Need later. I want to do it once, or if user clears the Cache which will be checked every hour(to save battery).
First, you cannot force the user to install the app widget. Work on solving the actual performance problem, rather than trying to build some optimization that will not help all users.
Second, your process can readily be terminated milliseconds after onUpdate() completes. Do not fill a cache, only to have it never be used.
Or is there a much better/ simpler way.
Use Traceview to determine exactly where your problem lies. Then, solve that problem. For example, the problem could be that you are loading these images on the main application thread, and using a library like Picasso would give you better performance from the user's standpoint.
The app Scrolling is slow because i provide three sizes (small, medium, large) and i scale them at while Scrolling trough my gridview
Since the scaling should be done by the GPU and take microseconds, I have difficulty believing that is your problem. What specifically did Traceview show you that made you think that scaling has something to do with this?
Doesn't the lruCache create an Cache-File in the Cache-Directory of my application?
No.
If not how does in works?
It is an in-memory cache.
So i don't understand why i never would use the Cache?
onUpdate() will be called much more frequently than the user will actually use your app widget.

Thread assistance

I need some help,
I am creating an app, and want it to run faster I mean when the app is started first it shows a blank white screen for o 1-2 seconds and then loads images. I have a layout background image, and 4 imageviews which are clickable and take you to the next activity. I read somewhere i should use threads to load images and it will load them on a separate thread faster, but i have some problem using it.
So here are the problems and android studio explanations:
Thread thread=new Thread(
public void run(){
ImageView tipka=(ImageView)findViewById(R.id.tipkaproba);
tipka.setImageResource(R.drawable.instructions);
LinearLayout asd=(LinearLayout)findViewById(R.id.layoutproba);
asd.setBackgroundResource(R.drawable.backfround123);
}
).start();
Now the android studio says: 
After
"Thread(" )expected
Before
"public void run(){"
; expected
On ").start();"
Invalid method declaration; return type required, Missing method body, or declare abstract.
Now i would like to know:
Does this speed up loading images, ( if not how to do it then)
How to fix my errors.
Thanks anyway !
Do all images fit in the screen?! If users need to scroll to view other images why load all of them at once? Use a grid view with adapter to load the image. In this way when the app start only images on the screen will be loaded and then when the user scrolls other images show up!
another thing you can do is to have 2 version of each image. one low quality with small size that loads first and one for high quality image. that will load latter. You can also calculate the base color for each image (use open source code or do it manually). then set the background image of iamgeview to this color. So when the image finally loads on the screen. The difference is not as dramatic as it was before.
If you want to use thread try AsyncTask first. Using AsyncTask is simpler than defining thread yourself.
Try to decrease the size of your images! If you be able to do this. it works better than any other trick! It's mobile, you don't have to show supper high quality images. users don't even notice most of the time
You probably took care of this but it worth mentioning that you need to provide different images for different screen densities and it's critical for performance as well as quality.

android: most efficient way to repeat an image

I am doing a project where I want 100 of the same image randomly scattered throughout the screen. In the future, I want an image to disappear when the image is tapped by the user.
I am mainly focusing right now on the most efficient way to display a repeated image. It seems like I could set it up so that the fact that the images are the same makes it more efficient; I'm just not sure how. I do not want to proceed further until I know I have a sound base. I'm using .png files.
I've looked around without a definite answer.
Also, would if be easier to draw my object with two circles (which is what my image is), rather than using a bitmap?
Any clues???
Assuming you're talking about drawing the bitmap on a Canvas object, the method should be pretty straightforward. You load the image into a Bitmap object and keep it as a member of the owning class, and draw it 100 times using canvas.drawBitmap(...) functions.
The other way of doing it is having 100 ImageViews with the same image, but I won't even write the details because this would be truly inefficient!
You can use a listView: http://developer.android.com/guide/topics/ui/layout/listview.html
It shows a list of items, using an adapter to inflate elements. It is pretty simple to use.

Dynamically change the image inside an imageview

I need to show a certain number of images within a imageview dynamically, every few seconds, I need show in that imageview around 40 images, without onclick events or others, just go changing the image, preferably obtain them from a website, how can I start with this?
really would appreciate your help.
Use a Handler to change the image each time delay you specify. Post a Runnable that changes the image with whatever time delay you wish.
Often people use an async-task for loading things online. I personally have never needed to, but you may need to load the images in an async task because it can slow up the UI and prompt a force-quit. Check on that though.
Bitmap bmp = MediaService.getDisplayImage("image_url", getApplicationContext());
((ImageView)findViewById(R.id.yourimageviewid)).setImageBitmap(bmp);
in this way you can retrieve image from network and set into your ImageView... rest all shuffling images etc detail you can manage with using timer..

Categories

Resources