note 39-关于启动界面,屏幕尺寸,自适应函数,与屏幕密度
android?不会自动检测屏幕密度, ?所有屏幕密度默认都当作 1?(160dpi )处理 . 因此问题就来了,?我的模拟器密度是1,?因此所有像素设置都没有问题.?但在我的屏幕密度 1.5?(240 dpi )的手机上,?所有单位都自动乘了1.5 !
?
幸好网上有高人:
http://www.cnblogs.com/wangtianxj/archive/2011/03/18/1988358.html
?
? ?
DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); int width = metric.widthPixels; // 屏幕宽度(像素) int height = metric.heightPixels; // 屏幕高度(像素) float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5) int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)?
?
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:resizeable="true" android:anyDensity="true" />
?
只要加入这一句, android 就会检测你屏幕的密度,自动作出正确的换算. 否则的话, 当我设置了一个宽度为屏幕一半的图片组件, 系统会自动把它乘以 1.5 , 变得无厘头的大 !
?
于是我的自适应算法就成立了,根据不同屏幕组件自动适应:
?
LinearLayout ll=(LinearLayout)this.findViewById(R.id.button_bar); ViewGroup.LayoutParams paramButtonBar=ll.getLayoutParams(); paramButtonBar.width=(int)(display.getWidth()*0.93); paramButtonBar.height=(int)(display.getWidth()*0.93*0.14); ll.setLayoutParams(paramButtonBar); Button bCal=(Button)this.findViewById(R.id.cal_btn); ViewGroup.LayoutParams paramsCal=bCal.getLayoutParams(); paramsCal.width=(int)(paramButtonBar.width/3); paramsCal.height=paramButtonBar.height; bCal.setLayoutParams(paramsCal);?
?
?
?
?
?
另一个话题,启动界面,也来自网上,不解释了:
?http://blog.sina.com.cn/s/blog_4d142b550100rs3f.html
?
?
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sunshine.splash" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Splash" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Main"> </activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>然后是JAVA代码:package net.hlovey.splash; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class Splash extends Activity { private final int SPLASH_DISPLAY_LENGHT = 3000; //延迟三秒 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); new Handler().postDelayed(new Runnable(){ @Override public void run() { Intent mainIntent = new Intent(Splash.this,Main.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } }, SPLASH_DISPLAY_LENGHT); } }?
?
public class MyActivity extends Activity implements Runnable { private boolean isOver=false; private LinearLayout screenup; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch(msg.what){ case 0: //隐藏启动的View screenup.setVisibility(View.GONE); //取消全屏 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); break; case 1: Toast.makeText(getApplicationContext(), "加载中", Toast.LENGTH_SHORT).show(); break; } super.handleMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); screenup=(LinearLayout)findViewById(R.id.screenup); new Thread(this).start(); } private void show(){ Message msg=new Message(); if(isOver){ msg.what=0; handler.sendMessage(msg); }else{ msg.what=1; handler.sendMessage(msg); } } @Override public void run() { // TODO Auto-generated method stub //加载数据。。。。。。。。此处没有数据加载,延时2秒 try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } isOver=true; show(); } }?