I have a problem while using the scroller in my application.
I have used the scroller but it is not working properly as it is not able to maintain a consistent speed while automatically scrolling. The speed of the text is getting slower with time as the text moves upward.
Here is the code that I used for this purpose:
length = prompt_text.getLineCount();
Log.d("length",""+length);
prompt_text.setScroller(scroll);
scroll.startScroll(0,0,0,10 * length, 100000 / speedAmount);
Have you tried setting up the Scroller interpolator as a LinearInterpolator?
http://developer.android.com/reference/android/widget/Scroller.html#Scroller(android.content.Context,%20android.view.animation.Interpolator)
Sample:
yourScroller = new Scroller(yourContext, new LinearInterpolator());
Related
I'm creating an app that works like a level for speeds.
If you go fast, my image will go up, and if you go slow, it will go down (Not to the top, the faster the upper and the slower the lower).
Currently, on every onLocationChange I calculate the recommended speed and get the actual value. Then I put move the image like that:
imageView.setY((float) speedvalue);
I have two problems:
setY is in pixels, so it doesn't fit to every screen.
Image moves instantly, I would like it to move like an animation.
How can I solve them?
You can animate the movement of a View like this:
TranslateAnimation animation = new TranslateAnimation(startXCoordinate, finishXCoordinate, startYCoordinate, finishYCoordinate);
animation.setDuration(1000);
view.startAnimation(animation);
Secondly, convert pixels to dps using this code and it will move the same amount on different screens:
int distanceToMove = (int) TypedValue.applyDimension(1, howManyDPsToMove, context.getResources().getDisplayMetrics());
i am trying to implement an animated butterfly which should fly around the screen by clicking a button .How can i implement it efficiently.If i need multiple butterflies(may be 100+) whether it effect device performance?.How can i achieve fly effect on wings.
Is it possible to implement with many parts of a butterfly image put
together and bring this fly effect.
Can i use renderScript
Please provide a sample code.i tried scaled animation but it is not as expected .any help appreciable.
You can achieve a quite good performance with showing a simple gif in your layout. Just create a gif out of the images you have (use some gif maker for that - there are a ton of other options, just search for it). For showing the gif in your application you can use a custom library, like android-gif-drawable. Implementation is similar to an ImageView:
<pl.droidsonroids.gif.GifImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/your_butterfly_anim"
/>
It's a possibility for simple animations.
Edit:
int distance = 100; //the distance to move in pixels
int duration = 500; //the duration of the animation in ms
double direction = Math.random() * 2 * Math.PI;
int translationX = Math.cos(direction) * distance;
int translationY = Math.sin(direction) * distance;
yourImageView.animate().translationX(translationX).translationY(translationY).setDuration(duration).start();
This should give you some first ideas of how you can get it flying randomly.
I'm a beginner in Andengine (Started 1 week ago :D ) and I decided to follow the full game tutorial by matim-dev which you can find here : http://www.matim-dev.com/full-game-tutorial---part-1.html
Once finishing the tutorial, I wanted to change the way how the sprite jump, more faster.
e.g : In this game the sprite takes 1 sec to go up and 1 sec to go down.
So my questions are : How can the sprite take 1 sec to up and down or less or even more or How can I handle the jump speed of a sprite in Andengine.
P.S : sorry for my bad english
Thank you !
I haven't tested this, but I think it could work. You can increase the gravity of the world. This will make the player not jump as high as before. If you want him to jump to the same height but faster, you could increase the gravity and also increase the linear velocity when jumping.
The gravity is set when you create you physicsworld, in the example it is set to -17:
private void createPhysics()
{
physicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, -17), false);
registerUpdateHandler(physicsWorld);
}
And changing the jump linear speed, in this example it is set to 12:
public void jump()
{
body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, 12));
}
I have two RelativeLayout views. The first one has about three children. The second one has only the first one view as its child. I have two animations, fadeIn and translateIn, which must be applied to the views firstView and secondView respectively. The code below is working. They start and end at the same time, but it's lagging. It's not so smooth. Is there any thing I can do to run them simultaneously and smooth?
Here's the code:
private static final int duration = 500;
protected static boolean ANIMATION_FINISHED;
protected static void onShowAnimation() {
int width;
ANIMATION_FINISHED = false;
width = getScreenWidth();
final AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
final TranslateAnimation translateIn = new TranslateAnimation(-width, 0, 1, 1);
fadeIn.setDuration(duration);
translateIn.setDuration(duration);
firstView.startAnimation(translateIn);
secondView.startAnimation(fadeIn);
secondView.postDelayed(new Runnable() {
#Override
public void run() {
ANIMATION_FINISHED = true;
}
}, duration);
}
animations up to gingerbread always been pretty laggy in Android. In Honeycomb and forward they fixed it with the new framework, but then the developers (that's you) must know which framework to use.
to have a nice smooth hardware accelerated animation you should use the android.animation not the android.view.animation
Here you have the complete guide for the property animation framework: https://developer.android.com/guide/topics/graphics/prop-animation.html
and here is the base class for it:
https://developer.android.com/reference/android/animation/package-summary.html
if you need to target devices on gingerbread or below, I suggest you to use the NineOldAndroid library that automatically handles the best possible framework for the device. (Remembering it will still be laggy on 2.3, but at least 3.0+ will be smooth).
ps.: You shouldn't use a postDelayed to know when the animation is over, instead use an Animation Listener
After searching a lot finally i successfully remove lagging during animation.
you need to set,
yourAnimatedView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
before starting animation, and after completion of animation you need to set,
yourAnimatedView.setLayerType(View.LAYER_TYPE_NONE, null);
During animations your views may be redrawn each frame. If you use view layers, instead of having to redraw each frame, views render once into an off-screen buffer which can be reused.
In addition, hardware layers are cached on the GPU, which makes certain operations during animation much faster. Simple transformations (translation, rotation, scaling and alpha) can be rendered quickly with layers. Since many animations are just a combination of these transformations, layers can supercharge animation performance
Goal
I'd like to implement a countdown timer that just scrolls numbers (not graphics) from left to right.
Effect
The effect would look like the number zooms in from the left, slows down towards the middle, and then zooms off to the right.
Notes
Since I'm already using a TimerTask to execute code every second, I could use that to trigger the next number to scroll across the horizontally-scrolling textview.
Could this just be implemented as a textview inside a scrollview ? Looking for a code sample to start off with....
Using Animations would be the simplest solution. You can create your own or try and combine multiple TranslateAnimations and ScaleAnimations.
This would mean putting each number into its own TextView instead of using a scroll view.
Then you could control the acceleration to the middle with an Interpolator. Interpolators are how Android handles easing. You would probably want the AccelerateDecelerateInterpolator for the speeding up / slowing down effect.
You can use an AnimationSet to apply multiple animations to the same View. Figuring out how to put together a good AnimationSet will be the most challenging part of the project. Make sure to pay attention to the "fill" property. In fact after playing around a little, I think a custom animation is simpler than using the ready made ones.
You can fork my GitHub project that implements a very simple version of this. April 17 and before I used multiple pre made Animations. If you look at the most recent version, you'll see the custom animation.
The timing for each Animation takes care of itself after you set the duration for one Animation. A Handler calls the next number after the previous one finishes. I think this is a little neater than having to call a function every X seconds to update everything.
The outline of functionality:
An Activity (CountDownActivity.java) over sees everything.
The Activitiy's layout XML has a button that is used to start the count down.
Once the countdown starts, the button disappears. It reappears when the count down is done.
The Activity contains a Handler (MotionHandler.java). The Handler controls the movement and timing of the numbers.
The Handler uses a AnimationSet to move the numbers
The AnimationSet is a passed in dependency
This is for flexibility. Simply pass in a different AnimationSet to change how the numbers move
The AnimationSet is made of four Animations a custom Animation (see below)
The AnimationSet uses a shared AccelerateDecelerateInterpolator, which seems to work decently. There are other options, including writing your own.
The Handler uses a delayed message to start the next number
The Handler notifies the Activity when the count down is done using a custom listener (MotionHandler >> CountdownListener)
Rotating the device will restart the count down.
Note - previously I was using four ready made Animations in one AnimationSet, I've edited to include just one custom Animation... You can tweak its algorithm to your liking.
This custom animation uses a Cycloid to make the numbers appear larger and smaller.
/**
* A custom animation to move and scale the numbers.
*
*/
public class NumberAnimation extends Animation
{
final public static float MINIMUM = 3;
private int mHorizontal;
private int mScaling;
public NumberAnimation(int horizontalMovement, int scaling)
{
mHorizontal = horizontalMovement;
mScaling = scaling;
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
// Cycloid repeats every 2pi - scale interpolatedTime to that
double time = 2 * Math.PI * interpolatedTime;
// Cycloid function
float currentScale = (float) (mScaling * (1 - Math.cos(time))) + MINIMUM;
Matrix matrix = t.getMatrix();
matrix.preScale(currentScale, currentScale);
matrix.postTranslate(mHorizontal * interpolatedTime, 0);
}
}
Easing will help you control the speed.