gpt4 book ai didi

android - 是否可以使用 XML 文件中的动画将图像居中?

转载 作者:行者123 更新时间:2023-11-29 14:01:34 25 4
gpt4 key购买 nike

我可以用 java 代码执行此操作,但我想知道是否可以通过 XML 动画文件使 imageView 居中。事实上,我的 ImageView 是一个线性布局,边距为 100dp,在屏幕的右上角,所以当我这样做时,图像没有居中,而是在距顶部和右侧居中 100dp 处:

<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0%"
android:toXDelta="-50%p"
android:fromYDelta="0%"
android:toYDelta="50%p"
android:duration="1000"
android:fillEnabled="false"
android:fillAfter="true">
</translate>

编辑:我的 imageView,“cardsPileImage”在这个布局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/board_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="100dp"
android:orientation="vertical">

<ImageView
android:id="@+id/cardsPileImage"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/back"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true">
</ImageView>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_button"
android:paddingLeft="60dp"
android:paddingRight="60dp"
android:textSize="25dp"
android:text="@string/start_game"
android:onClick="startGame">
</Button>

</LinearLayout>

<ImageView
android:id="@+id/discardPileImage"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/back"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true">
</ImageView>

</LinearLayout>

</LinearLayout>

谢谢你提前:-)

最佳答案

是的,你可以做到。

但是为此您必须采用 RelativeLayout 因为 LinearLayout 将无法为您的情况执行此操作。

为什么所有时间和所有其他 View 都采用 linearLayout ??

只需将此 xml 与您的翻译动画一起使用并尝试。

xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/board_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical">

<ImageView
android:id="@+id/cardsPileImage"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="right"
android:layout_marginTop="110dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
</ImageView>

<Button
android:layout_below="@+id/cardsPileImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:id="@+id/backButton"
android:paddingLeft="60dp"
android:paddingRight="60dp"
android:textSize="25dp"
android:text="Start Game"
android:onClick="startGame">
</Button>

<ImageView
android:id="@+id/discardPileImage"
android:layout_below="@+id/backButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:adjustViewBounds="true">
</ImageView>

</RelativeLayout>

如果它不起作用,请告诉我。 :)

已更新

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/board_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">

<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_margin="100dp"
android:gravity="center"
android:layout_gravity="center">

<ImageView
android:id="@+id/cardsPileImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="right"
android:layout_alignParentRight="true"
>
</ImageView>
<Button
android:layout_below="@+id/cardsPileImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon"
android:id="@+id/backButton"
android:layout_centerInParent="true"
android:textSize="20dp"
android:layout_margin="10dp"
android:text="Start Game"
android:onClick="startGame">
</Button>

<ImageView
android:id="@+id/discardPileImage"
android:layout_below="@+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_gravity="right"
android:layout_alignParentRight="true"
>
</ImageView>

</RelativeLayout>
</LinearLayout>

关于android - 是否可以使用 XML 文件中的动画将图像居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9256892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com