在Image中将ImageView移动到屏幕中心

时间:2015-05-11 06:11:10

标签: android android-animation android-imageview android-image android-windowmanager

我在一个有2个imageview的应用程序中工作,第一个是剩下的,另一个是在右边...当页面将加载imageview移动和它到屏幕的中心.. 即

imageview的-------->中心< ---------- imageview的

我使用翻译动画来移动带有4个参数的图像

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

但问题是没有显示..

我的XML文件

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

在我的应用程序中在createview java代码是

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

,功能代码为

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

我的Logcat值是

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

我无法理解这是什么问题..如果有人知道,请帮助我.... 在此先感谢朋友们

2 个答案:

答案 0 :(得分:2)

我也做了类似的事情,我可以设法在onWindowsFocusChanged函数中做到这一点。代码如下,我知道你已经尝试过了,但也许你可以从中找到有用的东西。

@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;
}

这里我尝试做的是,通过在图像上应用动画设置,将初始图像从特定位置移动到更低位置。这里FadeIn和FadeOut只是用于更改图像alpha的动画,因此您只需要为动画集提供自己的动画。

希望它会以某种方式帮助你。

此致

答案 1 :(得分:0)

这可能对你有帮助,它对我有用

public void moveImageToRightToCenter(ImageView img ,int screenWidth)
    {
        img.setTranslationX(screenWidth);
        img.animate()
                .translationX(0)
                .setDuration(300)
                .setStartDelay(300);
    }
相关问题