2011-01-28 79 views
12

我有两个图像的布局:级图像到屏幕宽度

  • 一个应该STRETCH时到屏幕宽度
  • 一个上面应该扩展到相同的比例的第一个是automaticaly缩放(相对于原始图像的大小)

更具体:两个图像是相同的图像切片,并因此一些细节内他们应该匹配。
我可以使用XML吗?

如果我不能通过XML来完成,也许我可以对图形进行预分频。在这种情况下,我应该如何对它们进行预分频?

+0

你解决了这个问题吗?顺便说一句,不知道你是否看过,但我更新了我对你的`Canvas.onDraw`问题的答案。 – techiServices 2011-02-13 21:47:09

回答

6

这是一个黑客,但它可以让你在XML中做到这一点。

如果你知道,例如,上面的图像是底部一个大小的X%,那么你可以使用的LinearLayout的layout_weight在屏幕的百分比的形式位置和大小,顶部图像:

<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <ImageView android:id="@+id/left_filler" android:layout_weight="20" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
    <ImageView android:id="@+id/top_image" android:layout_weight="50" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
    <ImageView android:id="@+id/right_filler" android:layout_weight="30" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
</LinearLayout> 
... bottom image

上面的代码会在屏幕的50%处调整top_image的大小,左边的偏移量为20%。只要top_image是bottom_image大小的50%,这将保持相似的比例。

或者,执行此操作的“正确”方法可能是在自定义视图中覆盖onDraw()并使用画布绘制方法。

0

您可以使用Canvas类方法drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) 通过自动缩放/翻译以填充目标矩形来绘制指定的位图。这可以用于具有不同Rect的位图。 Rect可以通过划分布局的当前宽度和高度来制定。因此,程序将根据具有不同屏幕尺寸的设备来缩放图像。