Dynamic ImageView in android - android

I'm a newbie in android so please don't judge me hard.
I'm trying to create a screen with two areas, an image area and a text area.
In the image area several images should be changed within a periodical time, 3-4 seconds. (Please see the image: )
Can you please give me an example how can I accomplish this?
Thank you.

You can use CountDownTimer class for this.
Or you can use a Gallery with Thread and Handler to achieve this.
Here is a link to the project called AutoSlideGallery,
https://github.com/nixit28/AutoSlideGallery
The core logic is here,
(new Thread() {
public void run() {
myslideshow();
handler.postDelayed(this, 2000); // execute every two second.
}
}
).start();
And the method which performs the action,
private void myslideshow() {
PicPosition = gallery.getSelectedItemPosition() + 1;
if (PicPosition >= pics.length)
gallery.setSelection(0); // stop
else
gallery.setSelection(PicPosition);// move to the next gallery
// element.
}

Related

When I load image and text, block click tab several seconds, why?

I do this in the create method on the activity for the second tab
public void infoTabs(){
//tab1: it works well
.......
// tab 2:
image.setImageResource(panel.getPhoto());
String infoCasilla = panel.getInfoText();
informacionCasilla.setText(infoCasilla);
}
Then when I press the second tab, it takes 3-4 seconds to load the image and text and is locked up showing this content tab.
Can you think of any idea as to prevent it from being blocked those seconds?
thank you very much.
If your photo large , it could take a lot of time (i.e. 3-4 sec) to load image into imageButton(or ImageView,whatever you using). You should load it in worker(background) thread, not in ui thread (like you do it for now). So use Separate Thread like this:
new Thread(new Runnable() {
public void run() {
image.post(new Runnable(){
public void run() {
image.setImageResource(panel.getPhoto());
}
}
}
}).start();
Or use Picasso or Fresco libs, that load image in separate thread for you.

Android- executing commands while countdown timer is running

I am trying to write an app that shows the user a string on textview, and replace the text due to user input. This should happen on time intervals of 60 seconds.
I tried to use a countdown timer, which sets a boolean var to false on it's onFinish method. While this boolean is true, I am trying to change the strings in the textview (further work will be to change the text due to the user actions, but I'm still trying to get this work for simple actions).
For now, the app seems to be stucked and nothing happens, but if I am removing the while loop, there is a single text on the screen (as it should be).
Is there a problem using a while loop for that purpose? Is there a different way to work along with the timer?
(p.s I know there are some problems in the code, but this is just for isolating the problem. Thanks!)
EDIT:
I will try to clarify my intentions:
I want to give the user tasks, which he should perform in a given time. I want to display the time remaining on the screen, along with the task to perform. When the user finishes the task, if there is still time on the clock, he will press a button and another task will appear. The goal is to finish as many tasks as possible in the given time.
My code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_game);
Bundle b = getIntent().getExtras();
game = b.getParcelable("game_record");
figuresList = new ArrayList<String>(game.getFiguresList());
clockView = (TextView) findViewById(R.id.clockView);
figureText = (TextView) findViewById(R.id.figureText);
timer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
clockView.setText(Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
clockView.setText("done!");
isTurn = false;
}
};
playRound();
}
private void playRound() {
figuresIterator = figuresListUsage.iterator();
isTurn = true;
String nextFigure = figuresIterator.next();
timer.start();
while (isTurn == true) {
figureText.setText(nextFigure);
nextFigure = figuresIterator.next();
}
}
It's a little difficult to understand your games logic but the main problem i see with your code is that you are entering the loop in a lifecycle event handler. Take a look at this lifecycle description. You are stopping it in onCreate and there is still work to be done before the activity will finish its lifecycle handling.
I suggest you try making play round event bound or use a diffrent thread for it. there are alot of threading APIs for android and as i dont know the nature of your game rounds i cant recommend any.

Android Development- Change gridview images continously after 2 sec- How can I use handler to achieve this task?

I have a question regarding grid views in android.
I have a grid of 5x5 images. And I want to change these images continously. Loads new set of images from the array every time.
I have a random generator function which changes the value of the mThumbIds array after it loads every time.
But I am unable to apply the new images before it needs some event to render the new set of images. Here I do not have any event. I want them to change continuously.
Can you please me.
Unable to find any solution for this.
Did you try using an AsyncTask to change your images ? The AsyncTask could change one image, then launch a new AsyncTask.
An other option is to use a Handler. That may be your best option actually. http://mobileorchard.com/android-app-developmentthreading-part-1-handlers/
Do you mean a timer to trigger the image changing function every 2 seconds?. If so try a Handler. Something like
private Handler mHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {
// init grid and set first batch of images
}
protected void onResume() {
mHandler.postDelayed(mUpdateImagesTask , 2000);
}
private Runnable mUpdateImagesTask = new Runnable() {
public void run() {
// Code to change the images
// call me in 2 seconds...
mHandler.postDelayed(this, 2000); // 2 seconds
}
};
If you want to stop the loop just put some conditions to call Handler.postDelayer() inside mUpdateImagesTask or call mHandler.removeCallbacks(mUpdateImagesTask) from outside the Runnable (at a button click, i.e).

Delayed image capturing

I know that is easy to fire an intent for image capturing and then after the user press on the capture button to take the image location of the image...
But I want to do something like this:
-user press a button
-camera app is started
-after 10seconds the image is taken automatically (no user interaction needed)
i think for your custom use case, you could write your own camera activity and then capture at 10 seconds
Edit: you can use this tutorial http://code.google.com/p/openmobster/wiki/CameraTutorial
Also the camera preview is present in apidemos application. It is present at Graphics->CameraPreview.
hmmm, maybe have a handler with a counter updating a TextView every second (So the app isnt seen as frozen) and then once the counter has reached ten take the photo
int counter2 = 0;
private Runnable mUpdateTimeTask2 = new Runnable() {
public void run() {
if (counter2 == 10){
takepicture();
}
counter2++;
mHandler.postDelayed(this, 1000);
}
};

Shorter delay in android slideshow

So im making a slideshow in android that i want to move very fast to look almost as though an animation. Bellow is the code that i am using but i want it go move faster(smaller delay) but i cant make the postDelay any smaller. How would i do this? If its not possible what would a better way to do this be?
private Runnable runnable = new Runnable() {
public void run() {
myslideshow();
handler.postDelayed(this, 1);
}
};
private void myslideshow()
{
if (position < imageIDs.length){
iv.setImageResource(imageIDs[position]);
position++;
}
else{
iv.setImageResource(R.drawable.logo);
}
}
Could the Android Animation API help instead?
You can use a gallery. On click of some button or anything you can run a for loop to
display next element in the gallery till the last element (image in your case) is displayed

Categories

Resources