将图像放在屏幕边界的一半,外面的一半

时间:2011-06-27 08:53:11

标签: android android-layout

我需要定位一个图像,使其中一半在屏幕边界之外,但我无法弄清楚如何做到这一点。如果有人想知道的话,那是我在飞机上旋转的轮子。

将alignParentBottom设置为true,然后在代码中将图像的高度偏移一半是一个选项,但我更倾向于在xml中完成所有操作。

有什么想法吗?

编辑:我被要求显示我的xml布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/icon"
    />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

使用以下代码创建动画xml:

<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:toXDelta="-50%p"
android:fromXDelta="-50%p"
 android:toYDelta="50%p"
android:fromYDelta="50%p"
android:duration="1" 
android:fillAfter="true"
>
</translate>

在活动中创建动画并使用上面的动画

Animation transAnimation = AnimationUtils.loadAnimation(this, R.anim.image_out);

将动画和图像分成两半:

circleView.startAnimation(transAnimation);

答案 1 :(得分:0)

给图像视图的高度等于图像大小,添加布局边距 = -height/2 意味着布局边距将为图像高度的一半。您也可以使用以下代码。

 <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cv_home_frag_1"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_200"
        android:layout_marginTop="-100dp"
        android:background="@drawable/bottom_round_rectangle"
        android:backgroundTint="@color/top_sheet_background"
        android:visibility="visible"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/iv_top_sheet_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>