I am implementing the listview with one image and framelayout(conatining button and linearLayout) , I want to animate the linearLayout on click of image. Is it possible? I have written following code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/icon" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<LinearLayout
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/a"
android:orientation="vertical" >
<TextView
android:id="#+id/from_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView
android:id="#+id/from_user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
</FrameLayout>
Please help me.
You can call animate method in onClickListener of imageview like..
ivmg.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
startLayoutAnimation(myLayout);
return false;
}
});
And define method as...
public void startLayoutAnimation(LinearLayout mLayout)
{
System.out.println("Inside startAnimation()");
Animation scaleAnim = new ScaleAnimation(0, 2, 0, 2);
scaleAnim.setDuration(5000);
scaleAnim.setRepeatCount(1);
scaleAnim.setInterpolator(new AccelerateInterpolator());
scaleAnim.setRepeatMode(Animation.REVERSE);
/* Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(1);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);*/
Animation rotateAnim = new RotateAnimation(0, 360, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(1);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(rotateAnim);
mLayout.startAnimation(animationSet);
}
}
I haven't tried animating layout yet. So you will have to check whether its working or not. Hope it helps
Related
So I have been trying to animate a smaller relative layout down/up from another relative layout.
I used this in hopes of trying to solve my problem. I was thinking of doing something like the snack bar, but I still would want to add an image to it, which may be a problem completely on its own. Ideas?
Here is my xml:
<RelativeLayout 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:id="#+id/activity_log_in"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.esole.lithouse.SignUp"
android:background="#E6E6E6">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:id="#+id/logInBar"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
app:title="Log In" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="83dp"
android:id="#+id/editTextUsernameOrEmail"
android:imeOptions="actionNext"
android:layout_below="#+id/logInBar"
android:layout_centerHorizontal="true"
android:hint="username or email " />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:imeOptions="actionDone"
android:layout_centerVertical="true"
android:id="#+id/passwordLogIn"
android:hint="password "
android:layout_alignEnd="#+id/editTextUsernameOrEmail" />
<Button
android:text="Log In"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/LogIn"
android:layout_above="#+id/statusIndicator"
android:layout_centerHorizontal="true"
android:layout_marginBottom="67dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#006D6D"
android:id="#+id/statusIndicator"
android:animateLayoutChanges="true"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/indicatorStatusLabel"
android:layout_alignParentEnd="true"
android:layout_toEndOf="#+id/imageView4"
android:layout_alignTop="#+id/imageView4"
android:layout_alignBottom="#+id/imageView4"
android:textAlignment="center"
android:textSize="24sp"
android:text="test "
android:typeface="normal"
android:lineSpacingExtra="24sp"
android:layout_marginRight="67dp" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:srcCompat="#drawable/refresh"
android:id="#+id/imageView4"
android:layout_marginStart="16dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Here is some of the code I have tried to use:
slideInAndOut = (RelativeLayout) findViewById(R.id.statusIndicator);
TranslateAnimation slide = new TranslateAnimation(0, 0, 0, -slideInAndOut.getHeight());
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
slide = new TranslateAnimation(0, 0, slideInAndOut.getHeight(), 0);
slide.setDuration(1000);
slide.setFillAfter(true);
First you should use android:src instead of app:srcCompat in your imageView
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) are the paras he is using!
Second this will work
slideInAndOut = (RelativeLayout) findViewById(R.id.statusIndicator);
TranslateAnimation slide = new TranslateAnimation(0, 0, 0, -slideInAndOut.getHeight());
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
slide = new TranslateAnimation(0, 0, 100, 0); // seems you have a problem with 3rd param
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
----- Wait you will see a code repetition there. Double check!
For only up this is enough!
TranslateAnimation slide = new TranslateAnimation(0, 0, 100, 0);
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
To add more value I modified your code a bit.
(now this view will come up and again will go down if you only need one TranslateAnimation you dont need both!)
// goes up
TranslateAnimation slide = new TranslateAnimation(0, 0, 100,0 );
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// goes down
TranslateAnimation slide = new TranslateAnimation(0, 0,0,100);
slide.setDuration(1000);
slide.setFillAfter(true);
slideInAndOut.startAnimation(slide);
}
}, 3000);
I am currently creating android app. I have a login activity in which on top there is LinearLayout with ImageViewand below it there is GirdLayout where there are few EditTexts. I would like to move my LinearLayout to given height when any of the EditTexts has focus. I was trying the solution from here:
Android translate animation - permanently move View to new position using AnimationListener and put AnimationListener inside of onFocusChange, but in my case it appears to do nothing. Could You help me?
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="6"
android:background="#74B897"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageLayout"
>
<ImageView
android:layout_width="247dp"
android:layout_height="290dp"
android:id="#+id/logo_imageView"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_row="1"
android:src="#drawable/white_logo" />
</LinearLayout>
<GridLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/layout_shape"
android:layout_weight="2"
android:layout_row="7"
android:layout_column="1"
android:weightSum="5"
android:layout_below="#+id/logo_imageView"
android:layout_marginLeft = "15dp"
android:layout_marginRight = "15dp"
android:layout_marginBottom = "30dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fname_editText"
android:hint="First Name"
android:layout_rowWeight="0"
android:layout_marginTop = "15dp"
android:layout_row="1"
android:layout_column="0"
android:textColor="#FFFFFF"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sname_editText"
android:layout_below="#+id/fname_editText"
android:hint="Second Name"
android:layout_rowWeight="0"
android:layout_row="2"
android:layout_column="0"
android:textColor="#FFFFFF"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password_editText"
android:inputType="textPassword"
android:ems="10"
android:hint="Password"
android:layout_rowWeight="0"
android:layout_row="3"
android:layout_column="0"
android:textColor="#FFFFFF"/>
<Button
android:layout_width="141dp"
android:text="Log In"
android:id="#+id/log_button"
android:background="#drawable/button_shape"
android:layout_marginTop="10dp"
android:layout_marginBottom="40dp"
android:layout_below="#+id/id_editText"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:layout_rowWeight="1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_row="5"
android:layout_column="0"
android:layout_height="29dp"
android:textColor="#636363" />
</GridLayout>
</LinearLayout>
Activity:
public class LoginActivity extends Activity implements View.OnFocusChangeListener{
EditText sname;
EditText password;
TextView info;
ImageView img;
EditText fname;
LinearLayout imageLayout;
int oldX, oldY, newX, newY;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
fname = (EditText) findViewById(R.id.fname_editText);
sname = (EditText)findViewById(R.id.sname_editText);
password = (EditText)findViewById(R.id.password_editText);
img= (ImageView) findViewById(R.id.logo_imageView);
imageLayout = (LinearLayout) findViewById(R.id.imageLayout);
oldX = imageLayout.getWidth();
oldY = imageLayout.getHeight();
img.setImageResource(R.drawable.white_logo);
Button log_Button = (Button) findViewById(R.id.log_button);
fname.setFocusable(true);
sname.setFocusable(true);
password.setFocusable(true);
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId() == R.id.fname_editText || v.getId() == R.id.sname_editText || v.getId() == R.id.password_editText)
{
final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 0.F, -300);
final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 300, 0.F);
if(hasFocus){
//first option
moveDownAnim.start();
/*
second option
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -300);
anim.setFillAfter(false);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageLayout.clearAnimation();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageLayout.getLayoutParams();
params.topMargin += -300;
imageLayout.setLayoutParams(params);
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, -300);
animation.setDuration(1);
imageLayout.startAnimation(animation);
}
});
imageLayout.startAnimation(anim);*/
}
}
}
}
I don't see you setting the OnFocusChangeListener for the EditTexts in your code.
fname.setOnFocusChangeListener(this);
sname.setOnFocusChangeListener(this);
I have made a app in which on button click i have slided a linear layout.The code to slide the linear layout is working fine but the problem is that it lags when sliding.Also the height which i have set to slide the layout doesnt look proper on all screen sizes(Specially on Nexus 5).Please do help me out
XMl
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<ImageView
android:id="#+id/iv_upload_add"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/add" />
<LinearLayout
android:id="#+id/ll_upload_options"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/choosefile_bg"
android:gravity="center"
android:orientation="vertical">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="#+id/tv_upload_gallery"
style="#style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="5dp"
android:text="GALLERY"
android:textSize="20sp"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#drawable/line" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="#+id/tv_upload_camera"
style="#style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="5dp"
android:text="CAMERA"
android:textSize="20sp"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#drawable/line" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="#+id/tv_upload_file"
style="#style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="5dp"
android:text="FILE"
android:textSize="20sp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/all_left_mar"
android:layout_marginRight="#dimen/all_right_mar"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.EditTextFont
style="#style/edit_text"
android:drawableBottom="#drawable/line_black"
android:hint="File Title"
android:textColor="#000"
android:textColorHint="#000" />
<AutoCompleteTextView
style="#style/edit_text"
android:layout_marginTop="10dp"
android:completionThreshold="1"
android:drawableBottom="#drawable/line_black"
android:hint="Type"
android:textColor="#000"
android:textColorHint="#000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Switch
android:id="#+id/switch1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:textOff="No"
android:textOn="Yes" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
style="#style/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="#string/Protect"
android:textColor="#000" />
</LinearLayout>
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.EditTextFont
style="#style/edit_text"
android:layout_marginTop="10dp"
android:drawableBottom="#drawable/line_black"
android:hint="Password"
android:inputType="numberPassword"
android:textColor="#000"
android:textColorHint="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="#dimen/all_left_mar"
android:layout_marginRight="#dimen/all_right_mar"
android:layout_weight="1">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.ButtonFont
style="#style/button"
android:text="UPLOAD" />
</LinearLayout>
</LinearLayout>
Code
if (llUploadOptions.getMeasuredHeight() != 0) {
// tvGallery.setVisibility(View.GONE);
// tvCamera.setVisibility(View.GONE);
// tvFile.setVisibility(View.GONE);
ValueAnimator anim = ValueAnimator.ofInt(llUploadOptions.getMeasuredHeight(),200);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llUploadOptions.getLayoutParams();
layoutParams.height = val;
llUploadOptions.setLayoutParams(layoutParams);
}
});
anim.setDuration(700);
anim.start();
} else {
ValueAnimator anim = ValueAnimator.ofInt(llUploadOptions.getMeasuredHeight(),250);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llUploadOptions.getLayoutParams();
layoutParams.height = val;
llUploadOptions.setLayoutParams(layoutParams);
}
});
anim.setDuration(700);
anim.start();
}
The problem is, that you are trying to animate the height of your Layout. That means, that with every animation step you call setLayoutParams() which forces your whole layout (incl. children) to be relayouted by going through the complete view hierarchy and this leads to laggy animation. Layouting things is an expensive operation!
There are some "workarounds" (I'm not sure what you want to do):
Since Android 4.0 you simply can use animate layout changes in your xml if you simply want to show / hide a element with animations in your layout
For more complex things you may consider to use Transitions framework introduced in Kitkat (Api 19) https://www.youtube.com/watch?v=S3H7nJ4QaD8. Backports: https://github.com/guerwan/TransitionsBackport or https://github.com/andkulikov/transitions-everywhere
"Fake" the animation by animating views by hand with translation, alpha etc. and set the LayoutParameters only once after the animation has been finished
I can't remember how the api is called, but you can subclass from ViewGroup and use it as your root layout. You have to intercept layout changes while animating the height somehow to get a smooth animation (this is how animateLayoutChanges = true works). However thats an advanced topic an I would recommend to go one of the other ways
you are using Animators (not Animations), which might be laggy, especially with moving widgets. try TranslateAnimation
TranslateAnimation animation = new TranslateAnimation(0.0f, 200.0f,
0.0f, 0.0f);
// these are delta's, check doc for this constructor
// TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(700);
llUploadOptions.startAnimation(animation);
if you want to leave your animated view with final coordinates use
animation.setFillAfter(true);
but it might not be clickable or other issues might occur. for these you might use
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim)
{
};
public void onAnimationRepeat(Animation anim)
{
};
public void onAnimationEnd(Animation anim)
{
//set fixed position in here, using LayoutParams or setTop/setRight etc. methods of View (API 11)
};
});
edit:
so instead of ValueAnimator you might do smth like this
int desiredHeightInPx = getResources().getDimensionPixelSize(R.dimen.expandedHeight);
//note those are pixels, not dp. you might set this as =200 or =250 like you have
ScaleAnimation animation = new ScaleAnimation(0, 0, 0, desiredHeightInPx/llUploadOptions.getMeasuredHeight(), Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(1000); //ms
llUploadOptions.startAnimation(animation);
next steps as above, fillAfter or set AnimationListener and inside onAnimationEnd set final LayoutParams.
be aware that ScaleAnimation is scaling, it might be more appropiate to use RelativeLayout instead of Linear (which rescales its childers)
I'm using Slide Up/Down Animation for my four TextView. Following is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shadow"
android:orientation="vertical"
android:weightSum="2.0" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<TextView
android:id="#+id/textViewblue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginLeft="25dp"
android:background="#drawable/blue"
android:gravity="center"
android:text="blue" />
<TextView
android:id="#+id/textVieworange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginRight="25dp"
android:background="#drawable/orange"
android:gravity="center"
android:text="orange" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">
<TextView
android:id="#+id/textViewpink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:background="#drawable/pink"
android:gravity="center"
android:text="pink" />
<TextView
android:id="#+id/textViewgreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:background="#drawable/green"
android:gravity="center"
android:text="green" />
</RelativeLayout>
</LinearLayout>
and Following is my Class
public class HomeScreenActivity extends Activity implements OnClickListener {
TextView textViewblue, textVieworange, textViewpink,
textViewgreen;
Animation mAnimation_up, mAnimation_down;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mAnimation_up = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 1.6f,
TranslateAnimation.RELATIVE_TO_SELF, 0f);
mAnimation_up.setDuration(1000);
mAnimation_up.setRepeatCount(-1);
mAnimation_up.setRepeatMode(Animation.REVERSE);
mAnimation_up.setInterpolator(new LinearInterpolator());
mAnimation_down = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 0f,
TranslateAnimation.RELATIVE_TO_SELF, 1.6f);
mAnimation_down.setDuration(1000);
mAnimation_down.setRepeatCount(-1);
mAnimation_down.setRepeatMode(Animation.REVERSE);
mAnimation_down.setInterpolator(new LinearInterpolator());
initialize();
}
private void initialize() {
textViewblue = (TextView) findViewById(R.id.textViewblue);
textVieworange = (TextView) findViewById(R.id.textVieworange);
textViewpink = (TextView) findViewById(R.id.textViewpink);
textViewgreen = (TextView) findViewById(R.id.textViewgreen);
textViewblue .setOnClickListener(this);
textVieworange .setOnClickListener(this);
textViewpink .setOnClickListener(this);
textViewgreen .setOnClickListener(this);
textViewblue .setAnimation(mAnimation_down);
textViewpink .setAnimation(mAnimation_down);
textVieworange .setAnimation(mAnimation_up);
textViewgreen .setAnimation(mAnimation_up);
}
#Override
public void onClick(View v) {
Intent intentmenu;
switch (v.getId()) {
case R.id.textViewblue:
Toast.makeText(this, "blue", Toast.LENGTH_LONG).show();
break;
case R.id.textVieworange:
Toast.makeText(this, "orange", Toast.LENGTH_LONG).show();
break;
case R.id.textViewpink:
Toast.makeText(this, "pink", Toast.LENGTH_LONG).show();
break;
case R.id.textViewgreen:
Toast.makeText(this, "reeng", Toast.LENGTH_LONG).show();
break;
}
}
}
The problem is that when any TextViewslide for int position means when animation starts the onClickListener works only at its XML position not for its updated position.
Please suggest me the solution for that, how can I detect the updated position for to detect onClickListener on TextView.
Thanks.
Maybe that's a bit complicated to make it. As you're using View animation system, the behavior is under expectation. Although the Views are translated to other places, they're just being
drawn there. From the system's perspective, they're in fact always at the original place where you define in xml file. If you want the click event at the new place, you should use the new property animation system.
The onClickListener is not working because views are shifted from their original place due to animation. To solve this create a same layout below your animation and initially make its Gravity as GONE and when your animation finishes up make its Gravity as Visible and your animation layout visibility as Gone.
also be careful that all the id's of animation and original layout would not be same.
I noticed a very strange situation when migrated my android app to 4.4 Suddenly the rotate animation in my HUD component started to blink and not working. I am trying to rotate object like this:
rotateAnimation = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setRepeatCount(Animation.INFINITE);
rotateAnimation.setRepeatMode(Animation.RESTART);
and:
public static void rotate(final View view) {
if(view.getAnimation() != null) {
view.getAnimation().cancel();
view.clearAnimation();
}
view.setAnimation(rotateAnimation);
view.getAnimation().start();
}
The code above works just fine when used on any other not nested view object, but when used for ImageView included with the include tag it simply does not work. Whats strange, other animations on the same object called within the same context works. Is this a 4.4 bug? I Include the view as follows:
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/view_trip_hud"
android:layout_gravity="bottom"/>
And the HUD component itself looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:alpha="0.6"
android:background="#drawable/background_texture_dark"
tools:context=".app.ui.fragment.TripSummaryFragment"
android:gravity="center_vertical|left"
android:id="#+id/relativeLayoutHUDContainer">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="#dimen/ui_default_element_margin">
<ImageView
android:id="#+id/imageViewTrackingStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/status_preparing"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textViewDriveQuality"
style="#style/HUDFont"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/track_status_inactive"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</LinearLayout>
<view
android:id="#+id/linearLayoutHUDCurrentSpeed"
android:layout_width="wrap_content"
android:layout_height="40dp"
class="pl.com.infinitysoftware.carassistant.util.ui.HUDBlockLayout"
style="#style/HUDBlock"
android:layout_alignParentRight="true"
android:layout_marginLeft="#dimen/ui_min_element_margin">
<TextView
android:id="#+id/textViewCurrentSpeed"
style="#style/HUDFont"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="-,-Km/h"
android:clickable="false"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"/>
</view>
<view
class="pl.com.infinitysoftware.carassistant.util.ui.HUDBlockLayout"
style="#style/HUDBlock"
android:id="#+id/linearLayoutHUDCurrentDistance"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/linearLayoutHUDCurrentSpeed"
android:layout_marginLeft="#dimen/ui_min_element_margin">
<TextView
style="#style/HUDFont"
android:id="#+id/textViewCurrentDistance"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="25 Km"
android:clickable="false"
android:layout_gravity="center_vertical"
android:gravity="center_vertical" />
</view>
</RelativeLayout>
I had a similar issue:
Try to set the view you are applying the animation like this (during the time you animate it):
view.setLayerType(View.LAYER_TYPE_HARDWARE,null);
//And then apply the animation
view.startAnimation(myAnimation);
DonĀ“t forget to put this in your manifest:
android:hardwareAccelerated="true"
i find the solution for this problem. Object Animator worked for me very well.
ObjectAnimator floatingButtonAnimator = ObjectAnimator.ofFloat(floatingActionButton,
ANIMATION_NAME, ANIMATION_STARTING_DEGREE, ANIMATION_ENDING_DEGREE);
floatingButtonAnimator.setDuration(ANIMATION_DURATION).addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
exchangeValues();
}
});
floatingButtonAnimator.start();