Animation not smooth - android

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)

Related

How to animate scaling of a relative layout(Which contains many layouts and ImageViews inside)

I tried with scaling but while scaling layouts inside are shrinking,
Can anyone explain how to animate a layout without shrinking
need to scale letterLayout from a little bit above the parent bottom to top
Animation anim = new ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(false); // Needed to keep the result of the animation
anim.setDuration(1000);
v.startAnimation(anim);
Layout code
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sample.LaunchScreenActivity">
<ImageView
android:layout_marginTop="195dp"
android:layout_marginLeft="26dp"
android:src="#drawable/ic_envelope_back"
android:layout_width="308dp"
android:layout_height="327dp" />
<RelativeLayout
android:id="#+id/letterLayout"
android:layout_width="284dp"
android:layout_height="451dp"
android:layout_marginLeft="38dp"
android:layout_marginTop="19dp">
<RelativeLayout
android:id="#+id/logo_layout"
android:layout_width="284dp"
android:layout_height="236dp"
android:background="#ff33">
<ImageView
android:layout_centerInParent="true"
android:src="#drawable/ic_logo_launch_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:background="#ff3344"
android:layout_below="#id/logo_layout"
android:layout_width="284dp"
android:layout_height="215dp">
<Button
android:layout_width="160dp"
android:layout_height="41dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:background="#FC5D2D"
android:text="brinblox_login"
android:textColor="#ffff"/>
</RelativeLayout>
</RelativeLayout>
<FrameLayout
android:layout_marginTop="316dp"
android:layout_width="308dp"
android:layout_height="206dp"
android:layout_marginLeft="26dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_envelope_front"/>
</FrameLayout>
</RelativeLayout>
I would suggest you to use a Value animator like this and change the margins of the RelativeLayout:
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
ValueAnimator va = ValueAnimator.ofInt(initialMargin, finalMargin);
int mDuration = 3000; //in millis
va.setDuration(mDuration);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
params.setMargins((int)animation.getAnimatedValue(),(int)animation.getAnimatedValue(),(int)animation.getAnimatedValue(),(int)animation.getAnimatedValue());
relativeLayout.setLayoutParams(params);
}
}
);
va.start();
Hope this helps.

After Slide Up/Down Animation Textview onClickListener not working

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.

Android 4.4 rotate animation blinks when animating nested view

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

How to animate the textview (very very long text )scroll automatically horizontally

I am not interested in Marquee because, in Marquee you can not control the speed of marquee.
I have tried to animate the textview but Parent view clips the text at the end even though all parent layout and view groups encompassing textviews are set with two flags clipchildren= false, clipToPadding=false.
Am I missing something or is there a better work around ?
The xml looks like
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="66dp"
android:maxLines="1"
android:singleLine="true"
android:textColor="#585858"
android:textSize="32sp" >
</TextView>
and code snippet look like
TextView textView2 = (TextView)findViewById( R.id.textview1 );
textView2.startAnimation((Animation)AnimationUtils.loadAnimation(this, R.anim.translate));
I think you can use translate animation. Something like this
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="100"
android:interpolator="#android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXDelta="-100" />
And add to your textview like this
textview.startAnimation((Animation)AnimationUtils.loadAnimation(Context,R.anim.scroll_animation));
Hope it can help you.
I am sure this will definitely solve the problem of the large audience out there.
Q: Auto-scroll a single line long text message(either using hard_coding or from string.xml) horizontally & infinitely at a reasonable speed but using marquee(try it once at least). No clipping
Step 1:
In activity_main.xml file:
<TextView
android:text="either hard coding or from string.xml"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:background="#color/colorPrimary"
android:textSize="18sp"
android:marqueeRepeatLimit="marquee_forever"
android:textColor="#android:color/background_light" />
Step 2: In main_activity java file
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView2);
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setSelected(true);
textView.setSingleLine(true);
textView.setText("Oxfam says 8 men as rich as half the world. | Govt may set threshold for probe into deposits. | At least 32 dead after Turkish plane hits village.");}}
//one can remove the last line line if he has already feed the long input
Just add this to your textview
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textSize="30dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Your_Text" />
Here was my SOLUTION
To make the long text inside textview not be cut by parent view or by screen, I have done two things.
First, let textview inside a scroolview like below code
<ScrollView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerHorizontal="true">
<TextView
android:id="#+id/marquee_text"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:maxLines="1"
android:textColor="#android:color/black"
android:textSize="30sp"/>
</ScrollView>
Then, I measure my text size then refine the textview param by doing this.
marqueeText.setText("my long text");
Paint textPaint = marqueeText.getPaint();
String text = marqueeText.getText().toString();//get text
int width = Math.round(textPaint.measureText(text));//measure the text size
ViewGroup.LayoutParams params = marqueeText.getLayoutParams();
params.width = width;
marqueeText.setLayoutParams(params); //refine
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
//this is optional. do not scroll if text is shorter than screen width
//remove this won't effect the scroll
if (width <= screenWidth) {
//All text can fit in screen.
return;
}
//set the animation
TranslateAnimation slide = new TranslateAnimation(0, -width, 0, 0);
slide.setDuration(20000);
slide.setRepeatCount(Animation.INFINITE);
slide.setRepeatMode(Animation.RESTART);
slide.setInterpolator(new LinearInterpolator());
marqueeText.startAnimation(slide);
I hope this solution which took me half a day to research can help others who might meet the same problem like me.
Can try out this. This is a solution using TranslateAnimation for creating an auto scrolling text (horizontal scroll, from Right to Left) (Tested on Android 8)
Class: AnimationAutoTextScroller.java
/**
* A Class for automatically scrolling text horizontally from Right to Left
* using TranslateAnimation so that the scrolling speed can be controlled -Suresh Kodoor
*/
public class AnimationAutoTextScroller {
Animation animator;
TextView scrollingTextView;
int duration = 50000; // default value
public AnimationAutoTextScroller(TextView tv, float screenwidth) {
this.scrollingTextView = tv;
this.animator = new TranslateAnimation(
Animation.ABSOLUTE, screenwidth,
Animation.RELATIVE_TO_SELF, -1f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f
);
this.animator.setInterpolator(new LinearInterpolator());
this.animator.setDuration(this.duration);
this.animator.setFillAfter(true);
this.animator.setRepeatMode(Animation.RESTART);
this.animator.setRepeatCount(Animation.INFINITE);
// setAnimationListener();
}
public void setDuration(int duration) {
this.duration = duration;
}
public void setScrollingText(String text) {
this.scrollingTextView.setText(text);
}
public void start() {
this.scrollingTextView.setSelected(true);
this.scrollingTextView.startAnimation(this.animator);
}
public void setAnimationListener() {
animator.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
// This callback function can be used to perform any task at the end of the Animation
}
public void onAnimationRepeat(Animation animation) {
}
});
}
}
Layout XML: (keep the TextView under a HorizontalScrollView)
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="#+id/hguide3"
app:layout_constraintEnd_toStartOf="#+id/vguide2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/vguide1"
app:layout_constraintTop_toBottomOf="#+id/hguide2">
<TextView
android:id="#+id/translateanimatortextviewscroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginEnd="0dp"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:text=""
android:singleLine="true"
android:focusable="true"
android:scrollHorizontally="true"
android:background="#000000ff"
android:textColor="#ff0000"
android:textSize="55dp"
android:textStyle="bold"
android:typeface="sans" />
</HorizontalScrollView>
Activity:
TextView scrollertextview = findViewById(R.id.translateanimatortextviewscroller);
textscroller = new AnimationAutoTextScroller(scrollertextview, screenwidth);
textscroller.setScrollingText(scrollertext);
textscroller.setDuration(60000);
textscroller.start();
Add this Animation file:
<translate
android:duration="7000"
android:fromYDelta="0%p"
android:interpolator="#android:anim/accelerate_interpolator"
android:repeatCount="10"
android:repeatMode="restart"
android:toYDelta="-100%p" />
/*Put your text view inside scroll*/
<ScrollView
android:layout_width="#dimen/dp_size_220"
android:layout_height="#dimen/dp_size_16"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="#+id/iv_myra_notification"
app:layout_constraintRight_toLeftOf="#+id/iv_one_way"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/marquee_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:text="#{itemData.notification.message}"
android:textColor="#android:color/black"
android:textSize="12sp"
tools:text="bfnfkjnvlen jknjkgnojeng"/>
</ScrollView>
try this code it will help you out Shri n HERO
<?xml version="1.0" encoding="utf-8"?>
<translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="1500"
android:interpolator="#android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXDelta="-1250" />
</translate>
This is what worked for me.
Place your textview inside a scroll view and then perform TranslateAnimation on the scrollview's child, my case its the LinearLayout.
I am actually adding multiple views dynamically inside this linear layout.
<ScrollView android:id="#+id/textScrollView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/textLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
TranslateAnimation slide = new TranslateAnimation(0, 0, height, -textLayout.getHeight());
slide.setDuration(movementSpeed);
slide.setRepeatCount(-1);
slide.setRepeatMode(Animation.RESTART);
slide.setInterpolator(new LinearInterpolator());
textLayout.startAnimation(slide);
height --> The point start scrolling up (in my case device height (device bottom))
movementSpeed --> scrolling speed
Use this simple way with ellipsize and marquee options using #rajath answer
<TextView
android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Its really frustrating,...
But the answer is simple,
Use Edittext instead of TextView, and wrap it in horizontalscrollview
At the same time, setfocusable: false
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:scrollbars="none">
<EditText
android:id="#+id/post_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:focusable="false"
android:outlineAmbientShadowColor="#color/colorPrimaryDark"
android:shadowColor="#000000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="20dp" />
</HorizontalScrollView>
Thanks to Hein, add the animation code
final EditText textView = view.findViewById(R.id.post_message);
textView.startAnimation((Animation) AnimationUtils.loadAnimation(context,R.anim.horizontal_animation));
String message="LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLong Text.";
textView.setText(message);
<translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5000"
android:fromXDelta="1500"
android:interpolator="#android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXDelta="-1250" />

Animating the linear layout

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

Categories

Resources