Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling parent but not children views

I'm creating an activity - that has a floor plan on it with pins where things are found on the floor plan.

Here is the layout for the activity:

<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>

I'm setting the RelativeLayout's background image as the floor plan.

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

I dynamically place pins (a png drawable packaged with my apk file) at various locations on the floor map. I create a relativelayout on the fly - then add an imageview (with the png file) then a TextView on the relativelayout (think like a gps pin with a number inside the pin).

    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);

All of that is working great.

So in the activity, I'm also providing the ability to zoom in on the floor plan.

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

Once zoomed in, I allow the user to move around on the floor plan. That works great too. As you move around the floor plan the gps pins remain where they should be (all good).

The problem I'm running into -- is when I zoom in on the layout -- the pins are also being scaling. I'd like for the gps pins on the floor map to remain their original size.

Any pointers how to scale the parent, but not scale the child views?

like image 238
user3410642 Avatar asked Mar 12 '14 13:03

user3410642


1 Answers

I've ran to the similar problem while creating my own map. However answer is quite simple. When you make parent view twice as big (200%) you have to make children two times smaller (50%) to compensate scaling up. That way children should always look the same on screen. In my case I was using ScaleGestureDetector so when scaling parent I had to set scale to it's children like this:

float childScale = 1.0f / parentScale;
child->setScaleX(childScale);
child->setScaleY(childScale);
like image 113
Makalele Avatar answered Sep 28 '22 02:09

Makalele