animation - How to translate image across the width of relativelayout in android -
i'm trying animate 2 images 1 left right , other right left @ same time. have set left margin left image equal negative of width , same right image out of view. in mainactivity i'm translating them using translationx nothing happening.
here xml file
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context="com.example.bex.mainactivity" > <imageview android:id="@+id/imageview1" android:layout_width="200dp" android:layout_height="133dp" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:src="@drawable/logo" /> <imageview android:id="@+id/left" android:layout_width="100dp" android:layout_height="75dp" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_marginleft="-100dp" android:src="@drawable/left" /> <imageview android:id="@+id/right" android:layout_width="114dp" android:layout_height="75dp" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_marginright="-114dp" android:src="@drawable/right" /> </relativelayout>
here mainactivity.java
import android.app.activity; import android.os.bundle; import android.widget.imageview; import android.widget.relativelayout; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); settheme(r.style.appbasetheme1); setcontentview(r.layout.activity_main); relativelayout r = (relativelayout)findviewbyid(r.id.rlayout); imageview left = (imageview)findviewbyid(r.id.left); imageview right = (imageview)findviewbyid(r.id.right); left.animate().translationx(r.getwidth()).setduration(2000); right.animate().translationx(-r.getwidth()).setduration(2000); } }
please tell me correct or possible way that
thanks in advance
there 2 base types of animation in android.
the first one.
in order animate translation use example:
translateanimation r = new translateanimation(0,0,0,200); r.setduration(2000l); r.setrepeatcount(0); r.setfillafter(true); view.startanimation(r);
the second one.
in case, use viewpropertyanimator
class. second implementation of animation in android. left.animate().translationx(r.getwidth()).setduration(2000)
in case have setup object animator haven't started it.
try this: left.animate().translationx(r.getwidth()).setduration(2000).start()
:)
edit have work:
xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@null"> <imageview android:id="@+id/imageview1" android:layout_width="200dp" android:layout_height="133dp" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:src="@drawable/aqu" /> <imageview android:id="@+id/left" android:layout_width="100dp" android:layout_height="75dp" android:src="@drawable/ar" /> <imageview android:id="@+id/right" android:layout_width="114dp" android:layout_height="75dp" android:src="@drawable/ari" /> </relativelayout>
java-file
@override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.dat); relativelayout r = (relativelayout)findviewbyid(r.id.rlayout); imageview left = (imageview)findviewbyid(r.id.left); imageview right = (imageview)findviewbyid(r.id.right); float distance = 300.f; translateanimation anim = new translateanimation(0,0,0,distance); anim.setfillafter(true); anim.setduration(12000l); left.startanimation(anim); }
but anyway if want animate using ide write this:
objectanimator anim0 = objectanimator.offloat(right, "translationx", 0,300); anim0.setduration(10000l); anim0.start();
it works. i've tested.
Comments
Post a Comment