简体   繁体   中英

Android moving Image view to center of the screen

I have one button and one image view at the bottom of my screen . when the button is clicked i want to move the image view to center of my screen. i'm trying with the following code . i'm not getting correctly. please help 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);

        }
    });

 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

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

Do this on Button click:

LayoutParams params = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.CENTER_VERTICAL);
imageView.setLayoutParams(params);

Your layout file code would be better to debug. Anyways i am assuming you are using a Relative Laytout as a parent for the button and image as it would be easier to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM