ViewHelper in NinaOldAndroid does not work outside of onCreate() method - android

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

Related

Controlling the frame animation speed with seek bar in android

I have the following code that changes the opacity of the frame animation with the help of a seekbar, now i'm trying to control the duration of frame animation with the help of this seek bar, can anybody help me in telling that if it is possible?
The code:
ImageView myAnimation = (ImageView)findViewById(R.id.myanimation);
//Create a new AnimationDrawable
final AnimationDrawable myAnimationDrawable
= createAnimationDrawable();
//apply the new AnimationDrawable
myAnimation.setImageDrawable(myAnimationDrawable);
SeekBar setAnimationAlpha = (SeekBar)findViewById(R.id.setalpha);
Button startAnimation = (Button)findViewById(R.id.startanimation);
Button stopAnimation = (Button)findViewById(R.id.stopanimation);
startAnimation.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(!myAnimationDrawable.isRunning()){
myAnimationDrawable.start();
}
}});
stopAnimation.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(myAnimationDrawable.isRunning()){
myAnimationDrawable.stop();
}
}});
setAnimationAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
myAnimationDrawable.setAlpha(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}});
}
private AnimationDrawable createAnimationDrawable(){
AnimationDrawable newAnim = new AnimationDrawable();
newAnim.addFrame(getResources().getDrawable(R.drawable.android_1), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_2), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_3), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_4), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_5), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_6), 90);
newAnim.addFrame(getResources().getDrawable(R.drawable.android_7), 90);
newAnim.setOneShot(false);
return newAnim;
}
}

RelativeLayout child top is coming as zero

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);

Android animation when scroll

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();
}

Hide view with animation and set visibility to gone without flash the screen

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);

Android Create Object Dynamically

I want to create one button/layout object dynamically from the object that exist on the current layout and want to handle both differently..
Here is my code that I implemented..
declared globally
Button btnStart, btnButton1, btnButton2, btnButton3;
and in OnCreate()
btnStart = (Button) findViewById(R.id.btnStart);
btnButton1 = (Button) findViewById(R.id.Button1);
btnButton2 = (Button) findViewById(R.id.Button2);
btnButton3 = btnButton1; // Problem comes here.
I want to create btnButton3 dynamically same as btnButton1. On clicking the
btnStart I am running animations for btnButton1, btnButton2 and btnButton3.
Now the issue is btnButton2 running animation fine.. but btnButton1 is not animating
and instead btnButton3 is animating.
Here's my complete code..
public class TweenAnimationActivity extends Activity {
/** Called when the activity is first created. */
Button btnStart, btnButton1, btnButton2, btnButton3;
FrameLayout mainLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout = (FrameLayout) findViewById(R.id.flMain);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Log.e("Dimension", "Width : " + width + " Height : " + height);
btnStart = (Button) findViewById(R.id.btnStart);
btnButton1 = (Button) findViewById(R.id.Button1);
btnButton2 = (Button) findViewById(R.id.Button2);
btnButton3 = btnButton1;
// btnButton3.setLayoutParams(relativeParams);
final Animation animation = new TranslateAnimation(0, 0, 0,
height - 220);
animation.setDuration(5000);
animation.setFillAfter(true);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btnButton1.setText("Moving");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
btnButton1.setText("Moved");
}
});
final Animation animation1 = new TranslateAnimation(0, 0, 0,
-(height - 220));
// animation1.setStartOffset(1000);
animation1.setDuration(5000);
animation1.setFillAfter(true);
animation1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btnButton2.setText("Moving");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
btnButton2.setText("Moved");
}
});
// final Animation animation2 = new TranslateAnimation(0, 0, -(width - 220),
// height - 220);
final Animation animation2 = new TranslateAnimation(0, 0, (width - 220),
height - 220);
animation2.setDuration(5000);
animation2.setFillAfter(true);
animation2.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
btnButton3.setText("MovingCopied");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
btnButton3.setText("MovedCopied");
}
});
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnButton1.startAnimation(animation);
btnButton2.startAnimation(animation1);
btnButton3.startAnimation(animation2);
}
});
}
}
any here's my layout..
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="#+id/Button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="top|right"
android:text="Button1" />
<Button
android:id="#+id/Button2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom|right"
android:text="Button2" />
</FrameLayout>
In your code, btnButton3 = btnButton1, you have not created button3 object just created a reference of button1.
try
btnButton3 = new Button(this)
btnButton3.setLayoutParams(btnButton1.getLayoutParams());
mainLayout.addView(btnButton3);
and for better tutorial you can look here.

Categories

Resources