首页 > 代码库 > Android px转dip px转sp法则

Android px转dip px转sp法则

在一些情况下,美工往往给我的都是px 这个时候我们就需要根据屏幕分辨率把px转成dip或者sp 下面就是计算公式:

<FrameLayout 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"
    tools:context=".MainActivity" 
    android:background="@android:color/white">
    
    
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="20dp"
        android:text="开始"/>

   

</FrameLayout>
package com.example.test5;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast;

public class MainActivity extends Activity {

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		
		findViewById(R.id.button).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Test();
				
			}
		});
		
	}

	public boolean isGreaterThan(String time){
		String time1=time.replace(":", "").trim();
		String time2="0130";
		return Integer.parseInt(time2)-Integer.parseInt(time1)<=0;
		
	}
	
	
	private void Test() {
		final float fontScale = this.getResources().getDisplayMetrics().scaledDensity;  
		final float scale = this.getResources().getDisplayMetrics().density;
		int screenWidth  = getWindowManager().getDefaultDisplay().getWidth();		
	    int screenHeight = getWindowManager().getDefaultDisplay().getHeight();	
	    DisplayMetrics dm = new DisplayMetrics();  
	    dm = new DisplayMetrics();  
	    getWindowManager().getDefaultDisplay().getMetrics(dm);  
	    float density  = dm.density;     
	    float densityDPI = dm.densityDpi;  
		Log.d("text", "scale="+scale+"fontScale="+fontScale+"screenWidth="+screenWidth+"screenHeight="+screenHeight);
		Log.d("text", "density="+density+"densityDPI="+densityDPI);
		 int a=px2sp(26,scale);
		 
		
		String s1=px2dip(this,13)+"";
		String s6=px2dip(this,60)+"";
		String s7=px2dip(this,11)+"";
		String s8=px2dip(this,162)+"";
		String s9=px2dip(this,232)+"";
		String s10=px2dip(this,44)+"";
		String s11=px2dip(this,178)+"";
		String s12=px2dip(this,610)+"";
		String s13=px2dip(this,420)+"";
		String s14=px2dip(this,126)+"";
		
		String s2=px2dip(this,32)+"";
		String s3=px2sp(26, fontScale)+"";
		String s4=px2sp(22, fontScale)+"";
		String s5=px2sp(18, fontScale)+"";
		
		System.out.println("s1: "+s1+"-----"
				+"s6:"+s6+"-----"
				+"s7:"+s7+"-----"
				+"s8:"+s8+"-----"
				+"s9:"+s9+"-----"
				+"s10:"+s10+"-----"
				+"s11:"+s11+"-----"
				+"s12:"+s12+"-----"
				+"s13:"+s13+"-----"
				+"s14:"+s14+"-----"
		+"s2:"+s2+"-----"
		+"s3"+s3+"-----"
		+"s4"+s4+"-----"
		+"s5"+s5+"-----");
		
	}
	
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
    
    public static int px2dip(Context context, float pxValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}
    
    public static int px2sp(float pxValue, float fontScale) {
		return (int) (pxValue / fontScale + 0.5f);
	}
    
    public static int sp2px(float spValue, float fontScale) {
		return (int) (spValue * fontScale + 0.5f);
	}
    
    public void sT(String s){
    	Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

}
源码下载


Android px转dip px转sp法则