Android 平移动画移动以显示其他视图

Android 平移动画移动以显示其他视图,android,translate-animation,Android,Translate Animation,我使用TranslateAnimation使片段(谷歌地图)向下滑动,为EditText和TextView留出可见的空间。 所以我用了这个: 文本:文本视图 编辑:编辑文本 MapLayout:包含地图的线性布局 Animation animation = new TranslateAnimation( MapLayout.getX(), MapLayout.getY(),MapLayout.getY(), text.getHeight()+edit.getHeight()); 问题是我无法制

我使用TranslateAnimation使片段(谷歌地图)向下滑动,为EditText和TextView留出可见的空间。 所以我用了这个:

文本:文本视图

编辑:编辑文本

MapLayout:包含地图的线性布局

Animation animation = new TranslateAnimation(
MapLayout.getX(), MapLayout.getY(),MapLayout.getY(), text.getHeight()+edit.getHeight());
问题是我无法制作幻灯片,因为text.getHeight()+edit.getHeight()返回0,所以没有幻灯片! 我试着使用一个数字(例如100),幻灯片是制作的,但是不同的设备之间是不同的,我在Galaxy S3上测试,幻灯片还不完整,还有一部分EditText不可见,对于模拟器来说,它工作正常。 当我试图使数字大一点,所以幻灯片会更长(例如200),那么。。。这张幻灯片对S3很好,但我对模拟器很感兴趣

所以我想知道的是,如果有任何方法可以使幻灯片移动到某一点,而不依赖于设备,我的意思是不使用像素;因此,该幻灯片在任何设备中都能完美工作/

我希望我的问题是清楚的。 多谢各位

更新:我不知道这是否有帮助,我添加了一条Toast消息,显示EditText和TextView的高度,在模拟器中显示为:85,在S3中显示为181 所以是的,我需要让地图像我说的那样在任何设备上向下滑动

主要活动:

protected Animation animation;
    protected LinearLayout MapLayout;
    protected EditText edit;
    protected TextView text;

    MapLayout = (LinearLayout)findViewById(R.id.MapLayout);
    edit = (EditText)findViewById(R.id.Recherche);
    text = (TextView)findViewById(R.id.CaptionRecherche);
    Toast.makeText(context, "Height: "+(edit.getHeight()+text.getHeight()), 1000).show();
    animation = new TranslateAnimation(MapLayout.getX(), MapLayout.getY(), MapLayout.getY(), text.getHeight()+edit.getHeight());
    animation.setDuration(1000);
    animation.setFillAfter(true);
    MapLayout.startAnimation(animation);
主XML:

-------我正在使用抽屉布局…我在应用程序中显示了幻灯片菜单…仅供参考-------


Main2 XML(我在上面包括的那个):


作为我的应用程序的一部分,我有一个包含一些文本的状态栏。此状态栏将一直隐藏,直到用户单击按钮并向下滑动(隐藏下面布局的最顶部内容)

我用于获取隐藏状态栏的正确高度的代码:

private int hiddenStatusHeight;
private int currentStatusBarHeight;

private void getStatusBarHeight() {
    final ViewTreeObserver observer = hiddenStatus.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @SuppressLint("NewApi") @SuppressWarnings("deprecation") @Override public void onGlobalLayout() {

            hiddenStatus.measure(MeasureSpec.UNSPECIFIED,
                    MeasureSpec.UNSPECIFIED);
            hiddenStatusHeight = hiddenStatus.getMeasuredHeight();

            currentStatusBarHeight = statusBar.getHeight();

            ViewTreeObserver obs = hiddenStatus.getViewTreeObserver();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                obs.removeOnGlobalLayoutListener(this);
            } else {
                obs.removeGlobalOnLayoutListener(this);
            }
        }
    });
}
单击按钮时执行的代码:

private OnClickListener ExpandClickListener = new OnClickListener() {

    @Override public void onClick(View v) {
        boolean isExpanded = (Boolean) expandButton
                .getTag(R.id.TAG_EXPANDED);
        int originalHeight = (Integer) expandButton
                .getTag(R.id.TAG_ORIGINAL_HEIGHT);

        if (isExpanded) {
            expandButton.setTag(R.id.TAG_EXPANDED, false);
            expandButton.setImageResource(R.drawable.ic_action_down);
            // statusBar.setLayoutParams(new FrameLayout.LayoutParams(
            // LayoutParams.MATCH_PARENT, originalHeight));
            Log.d(TAG, "Collapsing to " + originalHeight);
            ValueAnimator va = ValueAnimator.ofInt(currentStatusBarHeight,
                    originalHeight);
            va.setDuration(500);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    statusBar.getLayoutParams().height = value.intValue();
                    statusBar.requestLayout();
                }
            });
            va.start();
        } else {
            expandButton.setTag(R.id.TAG_EXPANDED, true);
            expandButton.setImageResource(R.drawable.ic_action_collapse);
            currentStatusBarHeight = originalHeight + hiddenStatusHeight;
            // statusBar.setLayoutParams(new FrameLayout.LayoutParams(
            // LayoutParams.MATCH_PARENT, currentStatusBarHeight + 15));
            Log.d(TAG, "Expanding to " + originalHeight + "+"
                    + hiddenStatusHeight + "=" + currentStatusBarHeight);
            ValueAnimator va = ValueAnimator.ofInt(originalHeight,
                    currentStatusBarHeight);
            va.setDuration(500);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    statusBar.getLayoutParams().height = value.intValue();
                    statusBar.requestLayout();
                }
            });
            va.start();
        }

    }
};
最后是我的布局XML(它必须是框架布局):


希望其中一些可能对您有所帮助

只是想提一下,我问了一个类似的问题,当菜单展开时,可视布局被向下推。因此,如果您想要这种行为,请看一看:

快乐编码

private int hiddenStatusHeight;
private int currentStatusBarHeight;

private void getStatusBarHeight() {
    final ViewTreeObserver observer = hiddenStatus.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @SuppressLint("NewApi") @SuppressWarnings("deprecation") @Override public void onGlobalLayout() {

            hiddenStatus.measure(MeasureSpec.UNSPECIFIED,
                    MeasureSpec.UNSPECIFIED);
            hiddenStatusHeight = hiddenStatus.getMeasuredHeight();

            currentStatusBarHeight = statusBar.getHeight();

            ViewTreeObserver obs = hiddenStatus.getViewTreeObserver();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                obs.removeOnGlobalLayoutListener(this);
            } else {
                obs.removeGlobalOnLayoutListener(this);
            }
        }
    });
}
private OnClickListener ExpandClickListener = new OnClickListener() {

    @Override public void onClick(View v) {
        boolean isExpanded = (Boolean) expandButton
                .getTag(R.id.TAG_EXPANDED);
        int originalHeight = (Integer) expandButton
                .getTag(R.id.TAG_ORIGINAL_HEIGHT);

        if (isExpanded) {
            expandButton.setTag(R.id.TAG_EXPANDED, false);
            expandButton.setImageResource(R.drawable.ic_action_down);
            // statusBar.setLayoutParams(new FrameLayout.LayoutParams(
            // LayoutParams.MATCH_PARENT, originalHeight));
            Log.d(TAG, "Collapsing to " + originalHeight);
            ValueAnimator va = ValueAnimator.ofInt(currentStatusBarHeight,
                    originalHeight);
            va.setDuration(500);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    statusBar.getLayoutParams().height = value.intValue();
                    statusBar.requestLayout();
                }
            });
            va.start();
        } else {
            expandButton.setTag(R.id.TAG_EXPANDED, true);
            expandButton.setImageResource(R.drawable.ic_action_collapse);
            currentStatusBarHeight = originalHeight + hiddenStatusHeight;
            // statusBar.setLayoutParams(new FrameLayout.LayoutParams(
            // LayoutParams.MATCH_PARENT, currentStatusBarHeight + 15));
            Log.d(TAG, "Expanding to " + originalHeight + "+"
                    + hiddenStatusHeight + "=" + currentStatusBarHeight);
            ValueAnimator va = ValueAnimator.ofInt(originalHeight,
                    currentStatusBarHeight);
            va.setDuration(500);
            va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    statusBar.getLayoutParams().height = value.intValue();
                    statusBar.requestLayout();
                }
            });
            va.start();
        }

    }
};
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="70dp" >

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="8dp"
        android:showDividers="middle" >
    </LinearLayout>
</ScrollView>

<RelativeLayout
    android:id="@+id/displayStatusBar"
    style="@style/DisplayStatusBar"
    android:layout_width="match_parent"
    android:layout_height="65dp" >

    <RelativeLayout
        android:id="@+id/status_always_visible"
        style="@style/StatusBar"
        android:layout_width="match_parent"
        android:layout_height="20dp" >

        <TextView
            android:id="@+id/status_received"
            style="@style/StatusBarText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="@string/received" />

        <TextView
            android:id="@+id/status_time_received"
            style="@style/StatusBarText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/status_received" />

        <TextView
            android:id="@+id/status_time_delete_relative_text"
            style="@style/StatusBarText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/status_time_delete_relative"
            android:text="@string/is_deleted" />

        <TextView
            android:id="@+id/status_time_delete_relative"
            style="@style/StatusBarText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="@string/minutes" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/status_hidden"
        style="@style/StatusHidden"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/status_always_visible" >

        <LinearLayout
            android:id="@+id/status_hydrants_near_address_container"
            style="@style/StatusHiddenText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:divider="@android:drawable/divider_horizontal_bright"
            android:orientation="vertical"
            android:paddingLeft="10dp"
            android:showDividers="middle" >

            <TextView
                style="@style/StatusHiddenText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/no_information" />
        </LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/optionsBar"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#999"
        android:orientation="horizontal"
        android:paddingTop="5dp" >

        <ImageButton
            android:id="@+id/button_hydrants"
            style="@style/android:Widget.ImageButton"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:alpha="50"
            android:contentDescription="@string/module_hydrants"
            android:src="@drawable/ic_action_place" />

        <ImageButton
            android:id="@+id/button_route"
            style="@style/android:Widget.ImageButton"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:contentDescription="@string/module_directions"
            android:src="@drawable/ic_action_directions" />

        <ImageButton
            android:id="@+id/button_pdf"
            style="@style/android:Widget.ImageButton"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:contentDescription="@string/module_accessplan"
            android:src="@drawable/ic_action_attachment" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <ImageButton
                android:id="@+id/button_more"
                style="@style/android:Widget.ImageButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_action_down" />

        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

<!-- The "empty" view to show when there are no items in the "list" view defined above. -->

<TextView
    android:id="@android:id/empty"
    style="?android:textAppearanceSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="32dp"
    android:text="@string/no_information"
    android:textColor="?android:textColorSecondary" />

</FrameLayout>