Search code examples
androidandroid-animationandroid-imageviewandroid-imageandroid-windowmanager

Move ImageView To Center Of Screen in android


i m working in an application which have 2 imageview 1st one is left and onother is in right ...when page will loaded imageview move nd it goes to center of screen.. i.e

imageview-------->center<----------imageview

i m using translate animation to move the images which take 4 parameter

i.e

 TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

but the problem is nothing is displayed ..

my XML File Is

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"  
android:id="@+id/topRelativeLayout" >

<LinearLayout
    android:id="@+id/centerLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView_header"
        android:layout_width="150dp"
        android:layout_height="80dp"
        android:layout_gravity="left"
        android:layout_marginTop="80dp"
        android:src="@drawable/logo_footer" />
    <ImageView
        android:id="@+id/imageView_footer"
        android:layout_width="150dp"
        android:layout_height="80dp"
        android:layout_gravity="right"
        android:layout_marginTop="30dp"
        android:src="@drawable/logo_header" />
</LinearLayout>  </RelativeLayout>

in My Applicationn On createview java code is

   DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;


    Log.d("Screen Width",String.valueOf(width));
    Log.d("Screen Height",String.valueOf(height));
    Log.d("Screen Half Width",String.valueOf(width/2));
    Log.d("Screen HalHeight",String.valueOf(height/2));

    header=(ImageView)findViewById(R.id.imageView_header);
    moveImageToLeftToCenter(header,height,width);
    footer=(ImageView)findViewById(R.id.imageView_footer);
    moveImageToRightToCenter( footer,height,width );

and the function code is

  public void moveImageToLeftToCenter(ImageView img, int height, int width ){

    TranslateAnimation anim = new TranslateAnimation( 0, width/2,height/2,height/2);// - originalPos[0] , 0, yDest - originalPos[1] );
    anim.setDuration(3000);
    anim.setFillAfter( true );
    img.startAnimation(anim);
    }

public void moveImageToRightToCenter(ImageView img,int height, int width ){

    TranslateAnimation anim = new TranslateAnimation( width/2, 0,height/2,height/2);// - originalPos[0] , 0, yDest - originalPos[1] );
    anim.setDuration(3000);
    anim.setFillAfter( true );
    img.startAnimation(anim);
}

My Logcat values are

05-11 11:27:33.762: D/Screen Width(4090): 1152
05-11 11:27:33.762: D/Screen Height(4090): 672
05-11 11:27:33.762: D/Screen Half Width(4090): 576
05-11 11:27:33.762: D/Screen HalHeight(4090): 336

i cant understand what is the problem..if anyone know than help me.... thanks in advance friends


Solution

  • I also did something like that and i can manage to do it in onWindowsFocusChanged function. Code is below, I know that you already tried it but maybe you can find something useful from it.

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        AnimationSet set = new AnimationSet(true);
    
        Animation fadeIn = FadeIn(3000);
        fadeIn.setStartOffset(0);
        set.addAnimation(fadeIn);
    
        RelativeLayout root = (RelativeLayout) findViewById(R.id.splashLayout);
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();
    
        int originalPos[] = new int[2];
        splashImage.getLocationOnScreen(originalPos);
    
        int xDest = dm.widthPixels / 2;
        xDest -= (splashImage.getMeasuredWidth() / 2);
        int yDest = dm.heightPixels / 2 - (splashImage.getMeasuredHeight() / 2)
                - statusBarOffset;
    
        TranslateAnimation anim = new TranslateAnimation(0, xDest
                - originalPos[0], 0, yDest - originalPos[1]);
        anim.setDuration(3000);
        set.addAnimation(anim);
    
        Animation fadeOut = FadeOut(1000);
        fadeOut.setStartOffset(3000);
        set.addAnimation(fadeOut);
    
        set.setFillAfter(true);
        set.setFillEnabled(true);
        splashImage.startAnimation(set);
    }
    
    private Animation FadeIn(int t) {
        Animation fade;
        fade = new AlphaAnimation(0.0f, 1.0f);
        fade.setDuration(t);
        fade.setInterpolator(new AccelerateInterpolator());
        return fade;
    }
    
    private Animation FadeOut(int t) {
        Animation fade;
        fade = new AlphaAnimation(1.0f, 0.1f);
        fade.setDuration(t);
        fade.setInterpolator(new AccelerateInterpolator());
        return fade;
    }
    

    Here what I tried to do is, move splash image from a specific position to a bit lower, by applying animation set on the image. Here FadeIn, and FadeOut are just animations for changing the alpha of image, so you just need to supply your own animation to animation set.

    Hope it will help you somehow.

    Regards,