I want do a animate scroll like Jquery´s (in this web page works when you click in top menu´s options) in android. is it possible? I have seen that exists scrollTo but is possible to do with animation and not like jumps. Thank you very much.
Using ObjectAnimator, This is a sample for scrolling to top :
public void scroolToTop() {
int x = 0;
int y = 0;
ObjectAnimator xTranslate = ObjectAnimator.ofInt(mScrollView, "scrollX", x);
ObjectAnimator yTranslate = ObjectAnimator.ofInt(mScrollView, "scrollY", y);
AnimatorSet animators = new AnimatorSet();
animators.setDuration(1000L);
animators.playTogether(xTranslate, yTranslate);
animators.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
animators.start();
}
Related
I have a ListView whose items need to be collapsed with an animation and then deleted. I use Animation for collapsing items, and after it's done collapsing I delete the item from the ListView (by deleting it from data list and calling notifyDataSetChanged). To detect if animation is done I check if interpolatedTime == 1.0 in applyTransformation method. The problem is that applyTransformation is called twice with `interpolatedTime' == 1, so I can't really rely on that (otherwise I can delete two items instead of just one). Why is this happening? Here's some of my code:
public static void collapseAndDelete(final View v, final ArrayList<AlarmClock> alarmClockArrayList,
final AlarmsArrayAdapter adapter, final int position) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
alarmClockArrayList.remove(position);
adapter.notifyDataSetChanged();
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
v.setAlpha(1.0f - interpolatedTime);
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(400);
v.startAnimation(a);
}
Implement animation listener to catch final callback
r.setAnimationListener(new OAnimationListener(this));
example of the class:
public class OAnimationListener implements AnimationListener{
private MyView vv;
public OAnimationListener(MyView vv) {
// TODO Auto-generated constructor stub
this.vv = vv;
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
if (vv != null)
vv.stopAnim(2); //or any wanted callback
}
}
do not forget to setup this:
r.setRepeatCount(0);
r.setFillAfter(true);
TranslateAnimation is use when you want to move image in different possintion like
left to right
up to down
one XYscalse position to other XYscale position
Syntax
TranslateAnimation animation = new TranslateAnimation(StartinXscale,StartingYscale,EndXscale,EndYscale);
Methods
animation.setDuration(millisecond);//move speed.
animation.setRepeatCount(int value);//how many time you want to move it from starting to ending position.
animation.setRepeatMode(int value);//mode like goto destination and return back to main position.
imageView.startAnimation(animation);//Start animation on imageView
Can we use above code twice time in one activity?
MyCode
private void animationAction() {
float StartX = 500.0f;
float StartY = -300.0f;
float EndX = -300.0f;
float EndY = 500.0f;
int i = 0;
for (i = 0; i <2; i++) {
TranslateAnimation animation = new TranslateAnimation(StartX,StartX + EndX, StartY, StartY + EndY);
animation.setDuration(3000);
animation.setRepeatCount(5);
animation.setRepeatMode(2);
animation.setFillAfter(true);
img_animation.startAnimation(animation);
EndX = 300.0f;
}
In MyCode result was only execute one time
// Animation
private Animation animMove;
private Animation animFadein;
//Method to preform 2 animation in sequence
private void doAnimation() {
// load the animation
animMove = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
// start move up animation on brnetwork text
imageView.startAnimation(animMove);
// set animation listener
animMove.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.background);
relMain.setBackgroundDrawable(drawable);
llInput.startAnimation(animFadein);
llInput.setVisibility(View.VISIBLE);
tvSignUp.startAnimation(animFadein);
tvVideo.startAnimation(animFadein);
tvSignUp.setVisibility(View.VISIBLE);
tvVideo.setVisibility(View.VISIBLE);
}
});
// set animation listener
animFadein.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
}
after MOVE animation i have to perform FEDIN animation
in your case you can replace FEDIN by your Next Animation.
am facing a strange problem, I have generated my layout file using RelativeLayout. however while reading top of one of the child's present in it is being returned as zero,which is causing my whole animation logic go for toss.
My idea is to animate them from bottom of screen, to there initial position. below is my code, any help from you all will be highly appreciated.
My layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingLeft="35dp"
android:paddingRight="35dp" >
<LinearLayout
android:id="#+id/msgs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="35dp"
android:layout_marginTop="25dp"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:text="#string/share"
android:textColor="#android:color/black"
android:textSize="26dp"
android:typeface="monospace" />
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="top"
android:maxLines="3"
android:text="#string/share_message"
android:textColor="#android:color/black"
android:textSize="14dp"
android:typeface="sans" />
</LinearLayout>
<ImageView
android:id="#+id/view_twitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_above="#id/msgs"
android:layout_marginEnd="25dp"
android:scaleType="fitCenter"
android:src="#drawable/circle_twitter" />
<ImageView
android:id="#+id/view_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/msgs"
android:layout_marginEnd="25dp"
android:scaleType="fitCenter"
android:layout_toStartOf="#id/view_twitter"
android:src="#drawable/facebook_circle" />
<ImageView
android:id="#+id/view_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/msgs"
android:layout_marginEnd="25dp"
android:layout_toEndOf="#id/view_twitter"
android:scaleType="fitCenter"
android:src="#drawable/google_circle" />
</RelativeLayout>
My Fragment code, where am animating them
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
public class IntroFragmentThird extends Fragment{
View google;
View twitter;
View facebook;
View messagePanel;
int screenHeight;
int screenWidth;
int offset;
float beginPoint;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
offset = screenHeight + 200; // start from 200px below screen height
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.intro3_layout, container, false);
google = view.findViewById(R.id.view_google);
facebook = view.findViewById(R.id.view_facebook);
twitter = view.findViewById(R.id.view_twitter);
messagePanel = view.findViewById(R.id.msgs);
google.setVisibility(View.GONE); // Initial view visibility will be gone
facebook.setVisibility(View.GONE);
twitter.setVisibility(View.GONE);
beginPoint = twitter.getTop(); // this is coming as ZERO
return view;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("#### Top : " + beginPoint);
playScreenAnimation();
}
public void playScreenAnimation(){
facebook.clearAnimation();
facebook.setVisibility(View.GONE);
facebook.clearAnimation();
facebook.setVisibility(View.GONE);
facebook.clearAnimation();
facebook.setVisibility(View.GONE);
ObjectAnimator anim1 = ObjectAnimator.ofFloat(facebook, "y", offset , beginPoint);
anim1.setInterpolator(new AccelerateDecelerateInterpolator());
anim1.setDuration(1000);
anim1.setStartDelay(1000);
anim1.addListener(new AnimatorListener(){
#Override
public void onAnimationStart(Animator animation) {
facebook.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
});
ObjectAnimator anim2 = ObjectAnimator.ofFloat(twitter, "y", offset , beginPoint);
anim2.setInterpolator(new AccelerateDecelerateInterpolator());
anim2.setDuration(1000);
anim2.addListener(new AnimatorListener(){
#Override
public void onAnimationStart(Animator animation) {
twitter.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
});
ObjectAnimator anim3 = ObjectAnimator.ofFloat(google, "y", offset , beginPoint);
anim3.setInterpolator(new AccelerateDecelerateInterpolator());
anim3.setDuration(1000);
anim3.setStartDelay(1000);
anim3.addListener(new AnimatorListener(){
#Override
public void onAnimationStart(Animator animation) {
google.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
});
AnimatorSet set = new AnimatorSet();
set.playTogether(anim2,anim1,anim3);
set.start();
}
}
Here is screenshot of, initial state of views (those three fb, g+, twitter circles are being animated)
Alright I solved it, instead of using object animator, i ended up using view animation, and animating views considering there relative positions.
facebook.clearAnimation();
facebook.setVisibility(View.INVISIBLE);
google.clearAnimation();
google.setVisibility(View.INVISIBLE);
twitter.clearAnimation();
twitter.setVisibility(View.INVISIBLE);
TranslateAnimation anim1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0
,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0);
anim1.setDuration(1000);
anim1.setInterpolator(new AccelerateDecelerateInterpolator());
anim1.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
facebook.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
TranslateAnimation anim2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0
,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0);
anim2.setDuration(750);
//anim2.setStartOffset(500);
anim2.setInterpolator(new AccelerateDecelerateInterpolator());
anim2.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
twitter.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
TranslateAnimation anim3 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0
,Animation.RELATIVE_TO_SELF,offset,Animation.RELATIVE_TO_SELF,0);
anim3.setDuration(750);
anim3.setStartOffset(500);
anim3.setInterpolator(new AccelerateDecelerateInterpolator());
anim3.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
google.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
});
twitter.startAnimation(anim2);
facebook.startAnimation(anim1);
google.startAnimation(anim3);
i want to hide dicelayout (dicelout was in mainboardlinear) but when animation finished the screen flashed because of View.GONE!
if i set setfillafter to true and clear View.GONE, i do not have the flash problem anymore but my scrollview in mainboardlinear can't be scrolled in this case!
final RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.dicelayout);
Animation animation=new TranslateAnimation(0, 0, 0, -rLayout.getHeight());
animation.setFillAfter(false);
animation.setFillBefore(true);
animation.setDuration(1000);
((LinearLayout)findViewById(R.id.mainboardlinear)).startAnimation(animation);
Thread t=new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(999);
runOnUiThread(new Runnable() {
public void run() {
rLayout.setVisibility(View.GONE);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
using onAnimationEnd from setAnimationListener and setFillEnabled solve my problem.
Animation animation=new TranslateAnimation(0, 0, 0, -rLayout.getHeight());
animation.setFillEnabled(true);
animation.setDuration(1000);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animatiofillAftern) {
// TODO Auto-generated method stub
// mainBoardLinear.removeView(rLayout);
rLayout.setVisibility(View.GONE);
}
});
mainBoardLinear.startAnimation(animation);
I try to set ViewHelper.setPivotY(test, 0); in method onClick() but it doesn't work. The animation still scale to the centerY. Here is my code. I don't know where I am wrong?
public class NewClassTest extends Activity{
LinearLayout test;
FrameLayout content;
Button btn;
boolean toggle = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
test = (LinearLayout) findViewById(R.id.test);
content = (FrameLayout) findViewById(R.id.content);
content.setVisibility(View.INVISIBLE);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// ViewHelper.setPivotY(test, 0); does not work
runAnimation(toggle);
toggle = !toggle;
}
});
ViewHelper.setPivotY(test, 0); // work
}
private void runAnimation(boolean in){
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator anim = null, anim2 = null;
// ViewHelper.setPivotY(test, 0); also does not work
if(in){
anim = ObjectAnimator.ofFloat(test, "scaleY", 1, 1 - (((float)content.getHeight()) / ((float) test.getHeight())));
anim2 = ObjectAnimator.ofFloat(content, "translationY", content.getHeight(), 0);
animSet.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
content.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
}else {
anim = ObjectAnimator.ofFloat(test, "scaleY", 1 - ((float)content.getHeight()) / ((float) test.getHeight()), 1);
anim2 = ObjectAnimator.ofFloat(content, "translationY", 0, content.getHeight());
animSet.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
content.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
}
animSet.setDuration(1000).play(anim2).with(anim);
animSet.start();
}
Thank for your help!.
Happen to me also. test it on HTC One S with android 4.1 and that wasn't worked, but in Samsung Galaxy I with android 2.2 it worked as expected.
Changed it to ViewHelper.setPivotY(test, 0.00000001f); solved it =\
Don't know if it's a bug of HTC, android or nineoldandroids