Android Imageview auto scroll with an animation - android

I have a image(size is 600 * 400) and my imageview size is (300 * 400).
I want load this image and auto scroll from left to right (in 5 seconds) with an animation.
I try to use "scrollto" and timer but It's not very good.
How to smooth scroll this imageview with an animation ?
Thanks very much.

did you try to use Handler?
Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
public void run() {
"Your code that be executed after 5 seconds"
}
}, 5000);

Related

Animate change of view background not happening smoothly

I am trying to animate the back ground of my activity
What i have done is
ImageView image = (ImageView) findViewById(R.id.toggle_image);
final TransitionDrawable td = new TransitionDrawable( new Drawable[] {
getResources().getDrawable(R.drawable.welcomeimagetoggle1),
getResources().getDrawable(R.drawable.welcomeimagetoggle2)
});
image .setImageDrawable(td);
td.startTransition(3000);
td.reverseTransition(3000);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
td.startTransition(3000);
td.reverseTransition(3000);
handler.postDelayed(this, 3000);
}
}, 3000);
It's working properly but the problem is while the timer ends the change happening is to smooth , there is a sudden movement. How can i avoid that ?
I think you should use this library (DexMovingImageView) instead.
It offers some cool transitions (check their demo). In your case, use the DexCrossFadeImageView with multiple images.
Don't forget to make the window background of your activity transparent to avoid overdraw.
UPDATE : About the padding, check the scaletype. As for animation, here's KenBurnsView (ImageView animated by Ken Burns effect), Motion (ImageView with a parallax effect that reacts to the device's tilt) and finally LoyalNativeSlider (An image slider with many cool animations). Hope this helps!

Automatic view pager in android

I have a problem with viewPager. I want to swipe pages automaticcly. I use handler to do that.
private Handler h = new Handler();
private Runnable animateViewPager = new Runnable() {
public void run() {
if (!pagerMoved) {
if (ticker == 3) {
h.removeCallbacks(animateViewPager);
h.postDelayed(startLogin, ANIM_VIEWPAGER_DELAY);
}else{
h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
}
viewPager.setCurrentItem(ticker % adapter.getCount(), true);
ticker++;
}
}
};
but when I change page this is too fast. I don't see animation like when you swipe by finger. I want to use fakeDrag to simulate swipe by I don't have idea how can I do that to take 2 seconds for example. Any ideas how can I simulate smooth slide page?
swipe pages automaticcly with slideshow using fade-in/out animation to switch between views
Ex: Using ViewPager for Screen Slides
for slowing down the swipe speed you need make custom ViewPager/Scroller.

How to wait after UI thread in Android?

In my Activity, I have a method that makes the font size bigger or smaller, depending on the width of a TextView. I do a setText on the TextView to enter a new value (which will most likely increase or decrease the width of the ViewText) and then go right into my method. My problem is that when I enter my method, the size of the TextView has not changed yet, so I'm guessing the UI thread is taking a bit more time to accomplish the resize of the TextView and my method does not work because of that. So how do I wait after the UI thread so that I can correctly execute the code of my method?
This is the code of my main activity. The method texteAffichage is just setting what is in my TextView and gestionPolice is resizing the font if it needs to. When I do a sysout in gestionPolice, I never get the current width of the TextView, because it seems like it did not have time to do that.
affichage.setText(texteAffichage((Button) v));
gestionPolice();
You could delay your code using a Handler:
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// DO DELAYED STUFF
}
}, your_variable_amount_of_time); // e.g. 3000 milliseconds
Side note: I do not think that your problem occurs because the setText(...) method takes too much time. Post your code so that others can have a look at it.
My suggestion would be something like:
tv.setText("someText");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
int width = tv.getWidth();
// your code reacting to the change in width
}
}, 50);
50 miliseconds should be enough for the change in UI to complete but fast enough for the user not to notice.
As Simon correctly points out this is not necessarily the best solution. To give you an example of how you might use GlobalLayoutListener:
final ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
width = tv.getWidth();
// your code reacting to the change in width
}
});

how to increase or decrease Height and width of scrollview at runtime in android?

I have a scrollview which height and width is defined in xml file, but I want to increase or decrease the height and width of scrollview dynamically at runtime.
Actually I want to increase the height and the width of the scrollview 10 px per second for 10 seconds. But scrollView.getLayoutParams().height = GivenHeight using this code I can increase it run time only once. Can we increase it more than one.
Thanks in advance.
scrollView.getLayoutParams().height = yourNewHeight; // in pixels
scrollView.getLayoutParams().width = yourNewWidth; // in pixels
EDIT to your new question:
If you want to increase this as time goes, you can easily achieve this by using a TimerTask and running it on a UI Thread, since we are touching it views.
I'll give you the code, since it's really simple.
int secondCounter = 0;
int delay = 0; // delay for 0 sec.
int period = 1000; // repeat every sec.
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// do work inside ui thread
runOnUiThread(new Runnable() {
public void run() {
// do your work right here
secondCounter++;
yourNewHeight += 10;
yourNewWidth += 10;
scrollView.getLayoutParams().height = yourNewHeight; // in pixels
scrollView.getLayoutParams().width = yourNewWidth; // in pixels
//stop the timer when 10 seconds has passed
if(secondCounter == 10){
timer.cancel();
}
}
});
}
}, delay, period);
EDIT:
To force view to refresh..
ViewGroup vg = findViewById (R.id.rootLayout);
vg.invalidate();
You will need to call invalidate inside the run method, after the new height and width has been set.
scrollView.getLayoutParams().height = assignHeight;
scrollView.getLayoutParams().width = assignWidth;

animate a image means it become big and small in one place android

hi i want to give animation using only one image in drawable folder means in imageview tag of xml file i increase width and height of same image now i want to animate it in one place
i had the code which uses series of images seved in drawale folder and animate it but i have only 1 image ..pls give any suitable example which uses only 1 image and animate that image using imageview height and width increasing..thanks in adv
below is my code..
final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
splashImageView.setBackgroundResource(R.drawable.flag);
final AnimationDrawable frameAnimation = (AnimationDrawable)splashImageView.getBackground();
splashImageView.post(new Runnable(){
#Override
public void run() {
frameAnimation.start();
}
});
final SplashScreen sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
}
};
mSplashThread.start();
}
Try this:
ScaleAnimation scaler = new ScaleAnimation((float) 0.7, (float) 1.0, (float) 0.7, (float) 1.0);
scaler.setDuration(40);
imageView1.startAnimation(scaler);
"become big and small in one place" usually is called scaling.
I didn't really read it (just googled "android scaling images"), but this forum post might help you out. It seems to have a code tutorial:
http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

Categories

Resources