main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/titleTextView" android:layout_width="150px" android:layout_height="50px" android:text="@string/hello_world" android:gravity="center" android:background="@android:color/black" android:textColor="@android:color/white" android:layout_centerHorizontal="true" /> <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="200px" android:layout_marginTop="80px" android:orientation="horizontal" > <Button android:id="@+id/firstButton" android:layout_width="150px" android:layout_height="100px" android:layout_marginLeft="50px" android:layout_marginTop="40px" android:orientation="horizontal" android:text="Button1" /> <Button android:id="@+id/secondButton" android:layout_width="150px" android:layout_height="100px" android:layout_marginLeft="100px" android:layout_marginTop="40px" android:orientation="horizontal" android:text="Button2" /> </LinearLayout> <RelativeLayout android:id="@+id/relativeLayout" android:layout_width="fill_parent" android:layout_height="300px" android:layout_marginTop="280px" > <ImageView android:id="@+id/firstImageView" android:layout_width="150px" android:layout_height="200px" android:layout_alignParentLeft="true" android:src="@drawable/icon" /> <ImageView android:id="@+id/secondImageView" android:layout_width="150px" android:layout_height="200px" android:layout_alignParentRight="true" android:src="@drawable/icon" /> </RelativeLayout> </RelativeLayout>
ViewScaleUtil如下:
package cn.utils; import android.app.Activity; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.View; import android.widget.TextView; public class ViewScaleUtil { public static float sScale = 1.0f; public static final int BASE_SCREEN_WIDTH = 480; public static final int BASE_SCREEN_HEIGHT = 854; public static final int LAYOUT_PARAMS_TYPE_LINEAR_LAYOUT = 0; public static final int LAYOUT_PARAMS_TYPE_RELATIVE_LAYOUT = 1; public static final int LAYOUT_PARAMS_TYPE_FRAME_LAYOUT = 2; public static final int LAYOUT_PARAMS_WIDTH = 0; public static final int LAYOUT_PARAMS_HEIGHT = 1; public static final int LAYOUT_PARAMS_TOP_MARGIN = 2; public static final int LAYOUT_PARAMS_LEFT_MARGIN = 3; public static final int LAYOUT_PARAMS_BOTTOM_MARGIN = 4; public static final int LAYOUT_PARAMS_RIGHT_MARGIN = 5; public static final int LAYOUT_PARAMS_COUNT = 6; public static void setScale(Activity context) { int width, height; DisplayMetrics displayMetrics = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); height = displayMetrics.heightPixels; width = displayMetrics.widthPixels; sScale = (float) width / BASE_SCREEN_WIDTH; System.out.println("---> sScale="+sScale); } public static int[] scaleProcess(View view, int paramType) { if (view == null || sScale == 1.0f){ return null; } int[] orgLayoutParams = new int[LAYOUT_PARAMS_COUNT]; switch (paramType) { case LAYOUT_PARAMS_TYPE_LINEAR_LAYOUT: android.widget.LinearLayout.LayoutParams llp = (android.widget.LinearLayout.LayoutParams) view.getLayoutParams(); orgLayoutParams[LAYOUT_PARAMS_WIDTH] = llp.width; orgLayoutParams[LAYOUT_PARAMS_HEIGHT] = llp.height; orgLayoutParams[LAYOUT_PARAMS_TOP_MARGIN] = llp.topMargin; orgLayoutParams[LAYOUT_PARAMS_LEFT_MARGIN] = llp.leftMargin; orgLayoutParams[LAYOUT_PARAMS_BOTTOM_MARGIN] = llp.bottomMargin; orgLayoutParams[LAYOUT_PARAMS_RIGHT_MARGIN] = llp.rightMargin; // Size if (llp.width > 0){ llp.width = (int) Math.ceil(llp.width * sScale); } if (llp.height > 0){ llp.height = (int) Math.ceil(llp.height * sScale); } // Margin llp.topMargin = (int) Math.ceil(llp.topMargin * sScale); llp.leftMargin = (int) Math.ceil(llp.leftMargin * sScale); llp.bottomMargin = (int) Math.ceil(llp.bottomMargin * sScale); llp.rightMargin = (int) Math.ceil(llp.rightMargin * sScale); view.setLayoutParams(llp); break; case LAYOUT_PARAMS_TYPE_RELATIVE_LAYOUT: android.widget.RelativeLayout.LayoutParams rlp = (android.widget.RelativeLayout.LayoutParams) view.getLayoutParams(); orgLayoutParams[LAYOUT_PARAMS_HEIGHT] = rlp.height; orgLayoutParams[LAYOUT_PARAMS_TOP_MARGIN] = rlp.topMargin; orgLayoutParams[LAYOUT_PARAMS_LEFT_MARGIN] = rlp.leftMargin; orgLayoutParams[LAYOUT_PARAMS_BOTTOM_MARGIN] = rlp.bottomMargin; orgLayoutParams[LAYOUT_PARAMS_RIGHT_MARGIN] = rlp.rightMargin; // Size if (rlp.width > 0){ rlp.width = (int) Math.ceil(rlp.width * sScale); } if (rlp.height > 0){ rlp.height = (int) Math.ceil(rlp.height * sScale); } // Margin rlp.topMargin = (int) Math.ceil(rlp.topMargin * sScale); rlp.leftMargin = (int) Math.ceil(rlp.leftMargin * sScale); rlp.bottomMargin = (int) Math.ceil(rlp.bottomMargin * sScale); rlp.rightMargin = (int) Math.ceil(rlp.rightMargin * sScale); view.setLayoutParams(rlp); break; case LAYOUT_PARAMS_TYPE_FRAME_LAYOUT: android.widget.FrameLayout.LayoutParams flp = (android.widget.FrameLayout.LayoutParams) view.getLayoutParams(); orgLayoutParams[LAYOUT_PARAMS_WIDTH] = flp.width; orgLayoutParams[LAYOUT_PARAMS_HEIGHT] = flp.height; orgLayoutParams[LAYOUT_PARAMS_TOP_MARGIN] = flp.topMargin; orgLayoutParams[LAYOUT_PARAMS_LEFT_MARGIN] = flp.leftMargin; orgLayoutParams[LAYOUT_PARAMS_BOTTOM_MARGIN] = flp.bottomMargin; orgLayoutParams[LAYOUT_PARAMS_RIGHT_MARGIN] = flp.rightMargin; // Size if (flp.width > 0){ flp.width = (int) Math.ceil(flp.width * sScale); } if (flp.height > 0){ flp.height = (int) Math.ceil(flp.height * sScale); } // Margin flp.topMargin = (int) Math.ceil(flp.topMargin * sScale); flp.leftMargin = (int) Math.ceil(flp.leftMargin * sScale); flp.bottomMargin = (int) Math.ceil(flp.bottomMargin * sScale); flp.rightMargin = (int) Math.ceil(flp.rightMargin * sScale); view.setLayoutParams(flp); break; } return orgLayoutParams; } public static int[] scaleProcessTextView(TextView view, int paramType) { if (view == null || sScale == 1.0f){ return null; } int[] orgLayoutParams = scaleProcess(view, paramType); scaleProcessTextViewTextSize(view); return orgLayoutParams; } public static void scaleProcessTextViewTextSize(TextView view) { if (view == null || sScale == 1.0f){ return; } float size = view.getTextSize(); size *= sScale; view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); } }
MainActivity如下:
package cn.testviewscaleutil; import cn.utils.ViewScaleUtil; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.app.Activity; public class MainActivity extends Activity { private TextView mTitleTextView; private View mLinearLayout; private Button mFirstButton; private Button mSecondButton; private View mRelativeLayout; private ImageView mFirstImageView; private ImageView mSecondImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mLinearLayout=findViewById(R.id.linearLayout); mTitleTextView=(TextView) findViewById(R.id.titleTextView); mLinearLayout=findViewById(R.id.linearLayout); mFirstButton=(Button) findViewById(R.id.firstButton); mSecondButton=(Button) findViewById(R.id.secondButton); mRelativeLayout=findViewById(R.id.relativeLayout); mFirstImageView=(ImageView) findViewById(R.id.firstImageView); mSecondImageView=(ImageView) findViewById(R.id.secondImageView); ViewScaleUtil.setScale(MainActivity.this); ViewScaleUtil.scaleProcess(mTitleTextView, ViewScaleUtil.LAYOUT_PARAMS_TYPE_RELATIVE_LAYOUT); ViewScaleUtil.scaleProcess(mLinearLayout, ViewScaleUtil.LAYOUT_PARAMS_TYPE_RELATIVE_LAYOUT); ViewScaleUtil.scaleProcess(mFirstButton, ViewScaleUtil.LAYOUT_PARAMS_TYPE_LINEAR_LAYOUT); ViewScaleUtil.scaleProcess(mSecondButton, ViewScaleUtil.LAYOUT_PARAMS_TYPE_LINEAR_LAYOUT); ViewScaleUtil.scaleProcess(mRelativeLayout, ViewScaleUtil.LAYOUT_PARAMS_TYPE_RELATIVE_LAYOUT); ViewScaleUtil.scaleProcess(mFirstImageView, ViewScaleUtil.LAYOUT_PARAMS_TYPE_RELATIVE_LAYOUT); ViewScaleUtil.scaleProcess(mSecondImageView, ViewScaleUtil.LAYOUT_PARAMS_TYPE_RELATIVE_LAYOUT); } }