缩放父级而非子级视图

时间:2014-03-12 13:03:30

标签: android android-layout

我正在创建一个活动 - 它上面有一个平面图,其中的针脚在平面图上找到了东西。

以下是活动的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:windowSoftInputMode="stateHidden"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:id="@+id/layout">
<requestFocus />    
</RelativeLayout>

我将RelativeLayout的背景图片设置为平面图。

private RelativeLayout _layout;
_layout = (RelativeLayout)findViewById(R.id.layout);
_layout.setBackground(new BitmapDrawable(getResources(), map));             

我在地图上的不同位置动态放置引脚(与我的apk文件一起打包的png drawable)。我在运行中创建了一个relativelayout - 然后添加了一个imageview(带有png文件),然后在relativelayout上添加一个TextView(想象就像一个gps引脚,引脚内有一个数字)。

    RelativeLayout grouping = new RelativeLayout(this);
    Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.symbol);
    ImageView symbol = new ImageView(this);   
    symbol.setImageBitmap(img);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    grouping.addView(symbol);
TextView mapNum = new TextView(this);
    grouping.addView(mapNum, params);
    _layout.addView(grouping, params);

所有这一切都很有效。

因此,在活动中,我还提供了放大平面图的功能。

_layout.setScaleX(_scaling);
_layout.setScaleY(_scaling);

放大后,我允许用户在平面图上移动。这也很有效。当您在平面图上移动时,gps针脚应保留在应有的位置(一切都很好)。

我遇到的问题是 - 当我放大布局时 - 引脚也正在缩放。我希望地板图上的gps引脚保持原始大小。

任何指针如何缩放父级,但不缩放子视图?

1 个答案:

答案 0 :(得分:2)

我在创建自己的地图时遇到了类似的问题。但答案很简单。当您将父视图设置为两倍大(200%)时,您必须使子项小两倍(50%)以补偿放大。这样孩子们应该总是在屏幕上看起来一样。在我的情况下,我使用的是ScaleGestureDetector,因此在缩放父级时,我必须将比例设置为它的子项,如下所示:

float childScale = 1.0f / parentScale;
child->setScaleX(childScale);
child->setScaleY(childScale);
相关问题