【问题标题】:Making a background image move in Android在 Android 中移动背景图像
【发布时间】:2015-10-30 17:10:20
【问题描述】:

我正在做一个简单的项目,我想知道让你的背景图像看起来移动或移动的最佳方法是什么或如何,我有想要无限移动的云?任何帮助将不胜感激。

【问题讨论】:

  • 云是ImageViews吗?还是背景图片的一部分?
  • @UdiIdan 部分图像视图
  • 如果它是ImageView 的一部分,则创建一个自定义Drawable 类并在那里为您的云设置动画
  • @pskink 有什么帮助吗?如果你能提供任何我会感激的链接,我会被困住。
  • 将此作为开始的例子:vogella.com/tutorials/AndroidDrawables/…

标签: java android


【解决方案1】:

<FrameLayout ...>(在布局 xml 中)将其子代绘制在另一个之上。

所以

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    < the background view />
    < the informative view />
</FrameLayout>

应该可以解决问题。但请注意,移动的背景会浪费 CPU 负载和电池等资源。

UPDATE(移动:与任何其他视图一样)

要移动视图,您可以使用动画或以编程方式更改视图位置。我做了后者的回应,一触即发,你大概会选择前者。 SO上有很多about动画。 我会尝试使背景图像比屏幕宽 2 倍,将左侧坐标设置为负值,并通过动画将此值更改为 0(图像会向右移动,至少在 iOS 中是这样)。

注 1。 要在 LinearLayout 或 RelativeLayout 中定位视图,您可以使用辅助透明视图。

例如,下面的布局将屏幕分成 5 个区域:

11111111111
222     333
44444444444

并将图像放置在“2”右侧和“4”上方的角落:

    <RelativeLayout
        android:id="@+id/sight"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <View
            android:id="@+id/sightq1"
            android:layout_width="fill_parent"
            android:layout_height="80px"
            android:layout_alignParentTop="true"
            />
        <View
            android:id="@+id/sightq4"
            android:layout_width="fill_parent"
            android:layout_height="80px"
            android:layout_alignParentBottom="true"
            />
        <View
            android:id="@+id/sightq2"
            android:layout_width="80px"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_above="@id/sightq4"
            android:layout_below="@id/sightq1"
            />
        <View
            android:id="@+id/sightq3"
            android:layout_width="80px"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_above="@id/sightq4"
            android:layout_below="@id/sightq1"
            />
        <!-- example: -->
        <ImageView
            android:id="@+id/..."
            android:layout_toRightOf="@id/sightq2"
            android:layout_alignTop="@id/sightq4"
            android:src="@drawable/..."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </RelativeLayout>

话虽如此,我必须注意,您可能不需要辅助视图,只需定位即可。

注 2。 要获取视图的实际视图宽度/高度,您必须使用 ViewTreeObserver.OnGlobalLayoutListener 并从 调用 v.getMeasuredWidth()v.getMeasuredHeight() onGlobalLayout()。这些宽度和高度只有在布局发生后才知道。 v.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener); 注册一个监听器。

【讨论】:

  • 感谢您提供此示例。
  • 我已经尝试过了,但我怎样才能让信息视图移动?
  • 您想移动信息视图还是背景视图?不同之处在于您可能可以裁剪背景,但必须保持信息完全可见。根据这一点,您可以选择一种或另一种方法。查看更新。
  • 感谢您的赞赏。
猜你喜欢
  • 2014-08-31
  • 2014-11-25
  • 1970-01-01
  • 2013-04-20
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多