I have a LinearLayout, I am dynamically adding TextView, ImageView to that LinearLayout, now I have added OnClickListener to these TextViews and ImageViews.
I have an arraylist which has 10 items in it, using a for loop I am getting each value and displaying, since it's quiz app I have next and previous buttons, when I click on that I am displaying items one by one from the arraylist.Now I need slide In and slide Out animation to apply to this Linear Layout.
I have seen this :
Link 1
Link 2
and also I have tried this
slideIn = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, 1.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f);
slideIn.setDuration(500);
slideOut = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, -1.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f);
slideOut.setDuration(500);
But not working please help on this.
EDIT:
Problem: I have applied this to the linear layout but what happens is that when i click on next button current question will slide left and a blank screen then it will slide in and display next question, but I want as soon as the current question slides out it should be followed by next question.
If your layouts are having identical content, create two layouts inside a view flipper.
Load first view with data and show it.
When user clicks next or previous, load next view with data and keep a flag to show that 2nd view is now visible and show it with animation.
Now onwards load the appropriate views with data based on the flag values and call shownext().
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainFlipper = (ViewFlipper) findViewById(R.id.flipper);
firstLayout = (LinearLayout) findViewById(R.id.layout1);
secondLayout = (LinearLayout) findViewById(R.id.layout2);
findViewById(R.id.btnPrevious).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
showPrevious();
}
});
findViewById(R.id.btnNext).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
showNext();
}
});
}
private void showNext() {
mainFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
mainFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_right));
flip();
}
private void showPrevious() {
mainFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
mainFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));
flip();
}
private void flip() {
if(isFirstVisible) {
isFirstVisible = false;
secondLayout.removeAllViews();
secondLayout.addView(getTextView("Second"));
} else {
isFirstVisible = true;
firstLayout.removeAllViews();
firstLayout.addView(getTextView("First"));
}
mainFlipper.showNext();
}
private TextView getTextView(String txt) {
TextView txtView = new TextView(this);
txtView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
txtView.setText(txt);
return txtView;
}
Use this xml in res/anim/
leftright.xml
This is for left to right animation:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
use this in your java code
Handler handler = new Handler();
Runnable runnable = new Runnable(){
{
public void run()
{
item[i].setInAnimation(AnimationUtils.loadAnimation(this,R.anim.leftright.xml));
i=i+1;
handler.postDelayed(this,5000);
}
};handler.postDelayed(runnable,5000);
Related
I have the following code, after creating ~400 ImageView instance (by calling updateUI(NewImage) ~400 times) the newly created ImageView animation would be laggy, the more time elapsed the more laggy they become. I think after anImageView animation is done, i must destroy them to avoid being laggy. any idea?
public void updateUI(final Bitmap bitmap)
{
runOnUiThread(new Runnable() {
#Override
public void run() {
final ImageView tmp = new ImageView(getApplicationContext());
tmp.setImageBitmap(bitmap);
tmp.setY(AT_THE_BOTTON_OF_ACTIVITY);
rv.addView(tmp); add the ImageView to the Relalayout
tmp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//disappear the imageView
ImageView t=imgs.get(id);
ObjectAnimator opacity = ObjectAnimator.ofFloat(tmp(The ImageView), "alpha", 0.0f);
opacity.setDuration(200);
opacity.start();
}
});
ObjectAnimator anim2 = ObjectAnimator.ofFloat(tmp, "y", 0);
anim2.setDuration(5000);
anim2.setInterpolator(new LinearInterpolator());
anim2.start();
}
});
}
I'm stuck and need help.
How can I add animations like on the picture. I want the color of the background and text to be smoothly automatically changed 1 sec after the app is launched. And to be like a cycle. Meanwhile an icon is impulsing.
You can use Property Animation for changing color
int colorFrom = getResources().getColor(R.color.red);
int colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(500); // milliseconds
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
textView.setBackgroundColor((int) animator.getAnimatedValue());
}
});
colorAnimation.start();
Use alpha animations to fade in and out the textchanging So on repeat is when it is fading back in so you can update the text at that time
AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setDuration(500);
anim.setRepeatCount(1);
anim.setRepeatMode(Animation.REVERSE);
txtLabel.startAnimation(anim);
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
txtLabel.setText("my next Text"); //for fading back in
}
});
You can use the same animation for alpha on a bitmap or imageView to pulse in and out as well.
AlphaAnimation animPulse = new AlphaAnimation(1.0f, 0.0f);
animPulse.setDuration(500);
animPulse.setRepeatCount(Animation.INFINITE);
animPulse.setRepeatMode(Animation.REVERSE);
imgPulse.startAnimation(animPulse);
Then of course just put in your
public void onCreate(){
Handler mMainHandler = new Handler(Looper.prepareMainLooper());
mMainHandler.postDelay(new Runnable(){
doAnimations();
}, 1000);
}
Then simply lump all your animations into methods and call them from doAnimations. Good luck.
Here you go..
I did the code to change color of background layout and text. Currently I did for random colors, if you need pre-defined colors, please let me know. Below is my code
activity_main.xml
`
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.golevr.background_animation.MainActivity"
android:id="#+id/constraint">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
`
MainActivity.java
`
public class MainActivity extends AppCompatActivity {
// Move your declarations here if you intend on using them after the onCreate() method
ConstraintLayout layout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inflate your objects
layout = (ConstraintLayout) findViewById(R.id.constraint);
textView = (TextView) findViewById(R.id.textView);
// We change the color to RED for the first time as the program loads
layout.setBackgroundColor(Color.RED);
// We change the color to GREEN for the first time as the program loads
textView.setTextColor(Color.GREEN);
// Create the timer object which will run the desired operation on a schedule or at a given time
Timer timer = new Timer();
// Create a task which the timer will execute. This should be an implementation of the TimerTask interface.
// I have created an inner class below which fits the bill.
MyTimer myTimer = new MyTimer();
// We schedule the timer task to run after 1000 ms and continue to run every 1000 ms.
timer.schedule(myTimer,1000,1000);
}
// An inner class which is an implementation of the TImerTask interface to be used by the Timer.
class MyTimer extends TimerTask{
#Override
public void run() {
// This runs in a background thread.
// We cannot call the UI from this thread, so we must call the main UI thread and pass a runnable
runOnUiThread(new Runnable() {
#Override
public void run() {
Random rand = new Random();
// The random generator creates values between [0,256) for use as RGB values used below to create a random color
// We call the RelativeLayout object and we change the color. The first parameter in argb() is the alpha.
layout.setBackgroundColor(Color.argb(255, rand.nextInt(256),rand.nextInt(256),rand.nextInt(256)));
textView.setTextColor(Color.argb(222,rand.nextInt(222),rand.nextInt(222),rand.nextInt(222)));
}
});
}
}
}
`
Is this what you want?
I am trying to get a button to fade in and out continuously and I cant seem to figure it out.
In the OnCreate:
Button playbtn =(Button) findViewById(R.id.playbutton);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.tween);
playbtn.startAnimation(myFadeInAnimation);
tween xml file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
Thanks for your help.
You can do this:
private void fadeIn() {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mButton, "alpha", 0f, 1f);
objectAnimator.setDuration(500L);
objectAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
fadeOut();
}
});
objectAnimator.start();
}
private void fadeOut() {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mButton, "alpha", 1f, 0f);
objectAnimator.setDuration(500L);
objectAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
fadeIn();
}
});
objectAnimator.start();
}
You can improve by consolidating both methods into a single one by keeping track of the state (fading in, fading out). You should also add a function to cancel the animation (you can return the animator from these functions and then call cancel on it).
Edit: to cancel, you can do this - create a member variable that holds your current animator, then simply can cancel on it:
private ObjectAnimator objectAnimator;
private void fadeOut() {
objectAnimator = ObjectAnimator.ofFloat(mButton, "alpha", 1f, 0f);
objectAnimator.setDuration(500L);
objectAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
fadeIn();
}
});
objectAnimator.start();
}
private void cancelAnimator() {
if (objectAnimator != null) {
objectAnimator.cancel();
objectAnimator = null;
}
}
Your code works perfectly for me..
also you can do button fade in and out continuously in java Using
myFadeInAnimation .setRepeatCount(Animation.INFINITE);
Hope this will helps you
This is java code.I am getting image from image gallery.I have one Button and one ImageView. It is rotating only one time.When I again click button it is not rotating image.
public class EditActivity extends ActionBarActivity
{
private Button rotate;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
rotate=(Button)findViewById(R.id.btn_rotate1);
imageView = (ImageView) findViewById(R.id.selectedImage);
String path = getIntent().getExtras().getString("path");
final Bitmap bitmap = BitmapFactory.decodeFile(path);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 510, 500,
false));
rotate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
imageView.setRotation(90);
}
});
}
Change your onClick() method to
#Override
public void onClick(View v)
{
imageView.setRotation(imageView.getRotation() + 90);
}
Notice, what the docs say
Sets the degrees that the view is rotated around the pivot point. Increasing values result in clockwise rotation.
I'd like to update my answer to show how to use RotateAnimation to achieve the same effect in case you're also targeting Android devices running Gingerbread (v10) or below.
private int mCurrRotation = 0; // takes the place of getRotation()
Introduce an instance field to track the rotation degrees as above and use it as:
mCurrRotation %= 360;
float fromRotation = mCurrRotation;
float toRotation = mCurrRotation += 90;
final RotateAnimation rotateAnim = new RotateAnimation(
fromRotation, toRotation, imageview.getWidth()/2, imageView.getHeight()/2);
rotateAnim.setDuration(1000); // Use 0 ms to rotate instantly
rotateAnim.setFillAfter(true); // Must be true or the animation will reset
imageView.startAnimation(rotateAnim);
Usually one can setup such View animations through XML as well. But, since you have to specify absolute degree values in there, successive rotations will repeat themselves instead of building upon the previous one to complete a full circle. Hence, I chose to show how to do it in code above.
So, here's my problem? I have it where it will slide out and then it will stay in that position and then you should be able to click on it again and it will slide back to the original location.
But instead I have to click on where it FIRST was at. Even when it's slided out. I want to make it after the animation I can click it any where, that it slided out.
Here's my code
public void sideBar()
{
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
if(out == 0)
{
mSlideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
mSlideInRight.setFillAfter(true);
sidebar.startAnimation(mSlideInRight);
out= 1;
}
else if(out == 1)
{
mSlideInLeft = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
sidebar.startAnimation(mSlideInLeft);
out=0;
}
}
and this is the code for when you click on it
public void onClick(View v) {
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
ImageView popup = (ImageView)findViewById(R.id.popup);
ImageView popup2 = (ImageView)findViewById(R.id.popup2);
switch(v.getId())
{
case R.id.sidebar:
sideBar();
break;
}
}
Animations only affect how your sidebar ImageView is drawn, not its actual position. After your animation completes, you should update the sidebar's layout parameters to your desired position. You can do this in an AnimationListener if you want the position to be updated as the animation runs or after it is complete, or right after your call to startAnimation if you want the position change to happen as the animation starts.
mSlideInRight.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation anim) {};
#Override
public void onAnimationRepeat(Animation anim) {};
#Override
public void onAnimationEnd(Animation anim) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(...);
// Set properties of layoutParams here
sidebar.setLayoutParams(layoutParams);
};
});
Replace RelativeLayout with whatever layout your ImageView is a child of.