Android moving Image view to center of the screen -


i have 1 button , 1 image view @ bottom of screen . when button clicked want move image view center of screen. i'm trying following code . i'm not getting correctly. please me.

my.java file

      button.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             // todo auto-generated method stub             imageview= (imageview)findviewbyid(r.id.imageview1);             displaymetrics metrics = new displaymetrics();             getwindowmanager().getdefaultdisplay().getmetrics(metrics);          float   x=metrics.heightpixels/2;         float   y=metrics.widthpixels/2;          translateanimation anim = new translateanimation( 0, x , 0, y);          anim.setduration(1000);          anim.setfillafter( true );           imageview.startanimation(anim);          }     }); 

without animations

 button.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                     // todo auto-generated method stub                     relativelayout.layoutparams parms2 = new relativelayout.layoutparams(width,height);                      parms2.addrule(relativelayout.center_vertical);                      parms2.addrule(relativelayout.center_horizontal);                       imageview.setlayoutparams(parms2);                  }             }); 

or
edit
with animated

relativelayout root = (relativelayout) findviewbyid( r.id.rl1 );                 displaymetrics dm = new displaymetrics();                // this.getwindowmanager().getdefaultdisplay().getmetrics( dm );                 getwindowmanager().getdefaultdisplay().getmetrics(dm);                 int statusbaroffset = dm.heightpixels - root.getmeasuredheight();                  int originalpos[] = new int[2];                 imageview.getlocationonscreen( originalpos );                  int xdest = dm.widthpixels/2;                 xdest -= (img.getmeasuredwidth()/2);                 int ydest = dm.heightpixels/2 - (imageview.getmeasuredheight()/2) - statusbaroffset;                  translateanimation anim = new translateanimation( 0, xdest - originalpos[0] , 0, ydest - originalpos[1] );                 anim.setduration(1000);                 anim.setfillafter( true );                 imageview.startanimation(anim); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -