Scale a view and its layered subviews relatively

后端 未结 2 1010
天涯浪人
天涯浪人 2021-01-15 05:46

(This is somewhat a follow-up on Android: How do you scale multiple views together?)

My task is to port an iPhone / iPad app on Android that consists of a simple ima

2条回答
  •  再見小時候
    2021-01-15 06:18

    I lately needed to do something similar, i also had to port a IPad app to android, the screen had many images that had to be in specific locations.

    I solved this slightly differently, absolute layout, and run through all the views and set the coordinated and size of each.

    //This gets the scale of the screen change:
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Drawable image = getResources().getDrawable(R.drawable.background_image);
    float scaleW = displaymetrics.widthPixels / (float)image.getIntrinsicWidth();
    float scaleH = displaymetrics.heightPixels / (float)image.getIntrinsicHeight();
    
    //And this scales each view accordingly:
    for(int i = 0; i < mainLayout.getChildCount(); i++)
    {
        View v = mainLayout.getChildAt(i);
        v.setLayoutParams(new AbsoluteLayout.LayoutParams(
                Math.round(scaleW * v.getMeasuredWidth()),
                Math.round(scaleH * v.getMeasuredHeight()),
                Math.round(scaleW * ((AbsoluteLayout.LayoutParams)v.getLayoutParams()).x),
                Math.round(scaleH * ((AbsoluteLayout.LayoutParams)v.getLayoutParams()).y)));
    }
    

提交回复
热议问题