java - How to slide an ImageView from left to right smoothly in Android? -
i need make imageview
slide left right of screen smooth animation (i want imageview
visible during transition) tried following code:
display display = getwindowmanager().getdefaultdisplay(); point size = new point(); display.getsize(size); int width = size.x; camion.animate() .translationx(width) .setduration(2000) .setinterpolator(new linearinterpolator()) .setlistener(new animatorlisteneradapter() { @override public void onanimationend(animator animation) { super.onanimationend(animation); //camion.setvisibility(view.gone); } });
the imageview
moves animation laggy , not smooth want to. doing wrong on code?
creating kind of tween animation simple. follow steps,
step 1
create directory anim
inside res
directory , put slide.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" android:fillafter="true"> <translate android:fromxdelta="0%p" android:toxdelta="75%p" android:duration="800" /> </set>
you can customize animation changing 2 attributes fromxdelta
, toxdelta
. %p refers respect parent means move image 75% respect parent.
step 2
// refer imageview imageview = (imageview) findviewbyid(r.id.img); // load animation animslide = animationutils.loadanimation(getapplicationcontext(), r.anim.slide); // start animation imageview.startanimation(animslide);
you can setinterpolator()
, setlisteners()
if want to. haven't shown them here keep simple. if need, let me know.
note
as have mentioned repeatedly, experiencing laggy animation. have tested animation on 3 real devices , 2 emulators , animation buttery smooth on of them. tested on low end devices moto e high end devices nexus 5 , galaxy s6.
if still have lag running code, test device must reason. code perfect.
update
i checked on moto g running on lollipop too, animation running smoothly. small , light-weight animation, , should never laggy. if still getting lag, it must device testing, or other piece of code on activity making ui slow or unresponsive.
try check 1 applicable you,
- i have tested on total of 6 devices no lag @ all. so, can rest assured production app not have lag, can device slow
- if doing heavy operations accessing file system, database, or other heavy operation, must slowing down ui thread , loosing frames. try use
asynctask
these heavy operations
Comments
Post a Comment