图像翻译没有显示和移动

问题描述 投票:0回答:1

我想将黑色方块向右移动。假设方格从位置X = 0开始。一秒后,我希望它在X = 2的位置,另一秒后将它放在X = 3的位置,依此类推......

我试图在X轴上做一个方块移动,但由于某些原因我不知道它根本不动。

这是我的片段XML代码(我知道在开头有一个拼写错误<,我取消了否则它不会出于某种原因显示,只是忽略错字):

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#edf7f2"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingTop="20dp" >

    <!-- Phone -->
    <ImageView
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:contentDescription="phone"
        android:paddingLeft="10dp"
        android:paddingRight="20dp"
        android:src="@drawable/phone" />

    <LinearLayout
        android:id="@+id/linearLayoutLoadingBar"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:layout_toLeftOf="@+id/pc"
        android:layout_toRightOf="@+id/phone"
        android:orientation="horizontal" 
        android:layout_centerInParent="true">

        <ImageView
            android:id="@+id/loading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rectangle"
            android:contentDescription="loading image"
            android:paddingLeft="20dp"
            android:paddingRight="20dp" />
    </LinearLayout>

    <!-- PC -->
    <ImageView
        android:id="@+id/pc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_margin="20dp"
        android:contentDescription="pc"
        android:src="@drawable/pc" />
</RelativeLayout>

<TextView
    android:id="@+id/tv_home_fragment_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:textSize="40sp" />

现在这是我的形状XML代码:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:width="20dp" android:height="20dp"/>

    <solid
        android:color="@android:color/black" />

</shape>

这是我的动画XML文件:

<?xml version="1.0" encoding="utf-8"?>

<translate
    android:duration="200"
    android:fillAfter="true"
    android:fromXDelta="0%p"
    android:startOffset="0"
    android:toXDelta="20%p" />
<translate
    android:duration="200"
    android:fillAfter="true"
    android:fromXDelta="20%p"
    android:startOffset="300"
    android:toXDelta="40%p" />
<translate
    android:duration="200"
    android:fillAfter="true"
    android:fromXDelta="40%p"
    android:startOffset="600"
    android:toXDelta="60%p" />
<translate
    android:duration="200"
    android:fillAfter="true"
    android:fromXDelta="60%p"
    android:startOffset="900"
    android:toXDelta="80%p" />
<translate
    android:duration="200"
    android:fillAfter="true"
    android:fromXDelta="80%p"
    android:startOffset="1200"
    android:toXDelta="100%p" />
</set>

片段看起来像这样:

我希望黑色方块向右移动(慢慢地)。但是当我运行应用程序时,它不会移动甚至出现在设备上。

这是我调用和设置动画的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    ImageView imageView = (ImageView) rootView.findViewById(R.id.loading);

    TextView textView = (TextView) rootView.findViewById(R.id.tv_home_fragment_content);
    textView.setText("Landing screen");

    Animation animFadein = AnimationUtils.loadAnimation(rootView.getContext(), R.animator.loading_square); 
    animFadein.setRepeatMode(Animation.REVERSE);
    imageView.startAnimation(animFadein);

    return rootView;
}
android xml android-fragments translate-animation
1个回答
1
投票

问题不明确。他们可能错误地将XML放在正确的目录中等。在res文件夹中创建文件夹名称“animator”,然后在其中创建loading_square.xml。

并且仅标记足够的例如fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

   <alpha android:duration="2000"
        android:fromAlpha="0.0"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0"/>


</set>

例如,可以

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="50%p"
        android:fromYDelta="0%p"
        android:toYDelta="50%"
        android:duration="2000" />
</set> 
© www.soinside.com 2019 - 2024. All rights reserved.