在Android中将ImageView移动到屏幕中心 [英] Move ImageView To Center Of Screen in android

查看:96
本文介绍了在Android中将ImageView移动到屏幕中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个有2个imageview的应用程序中工作,第一个留在左边,另一个在右边.当页面加载imageview移动到屏幕中心时.即

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

图像视图-------->居中< ----------图像视图

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

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

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

但问题是什么都没有显示..

but the problem is nothing is displayed ..

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

在我的Applicationn中,在createview上的Java代码是

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

,功能代码为

  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

我不明白问题出在什么地方..如果有人知道帮助我....在此先感谢朋友

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

推荐答案

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

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

在这里我想做的是,通过在图像上应用动画设置,将启动图像从特定位置移到更低的位置.这里的FadeIn和FadeOut只是用于更改图像的Alpha的动画,因此您只需要向动画集提供自己的动画即可.

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.

此致

这篇关于在Android中将ImageView移动到屏幕中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆