为什么要屏幕适配?为此我就不说了,网上处理方法要么让你用几套不同分辨率的图片,要么写几套布局文件,要么就是在xml中写dip(这个还是可以的),前面两种感觉过程工作量太大了,由加载大图片的优化思想
同样对一个小算法来实现此功能。。
喜欢多媒体播放器的朋友,可以去看我的一个还未成熟的播放器,里面用到了屏幕适配,下载地址:http://www.eoemarket.com/soft/370334.html,正在完善中
先来测试代码:
package cn.marsXTU.Screenadapter; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.DisplayMetrics; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getDisplayMetrics(MainActivity.this); } public static String getDisplayMetrics(Context cx) { String str = ""; DisplayMetrics dm = new DisplayMetrics(); // 取得DisplayMetrics对象方法一 // dm = cx.getApplicationContext().getResources().getDisplayMetrics(); // 取得DisplayMetrics对象方法二 ((Activity) cx).getWindowManager().getDefaultDisplay().getMetrics(dm); int screenWidth = dm.widthPixels; int screenHeight = dm.heightPixels; float density = dm.density; float xdpi = dm.xdpi; float ydpi = dm.ydpi; str += "The absolute width:" + String.valueOf(screenWidth) + "pixels\n"; Log.i(TAG,str); str += "The absolute heightin:" + String.valueOf(screenHeight) + "pixels\n"; Log.i(TAG,str); str += "The logical density of the display.:" + String.valueOf(density) + "\n"; Log.i(TAG,str); str += "X dimension :" + String.valueOf(xdpi) + "pixels per inch\n"; Log.i(TAG,str); str += "Y dimension :" + String.valueOf(ydpi) + "pixels per inch\n"; Log.i(TAG,str); return str; } }
打印信息如下:
09-14 20:01:43.327: I/MainActivity(1384): The absolute width:480pixels
09-14 20:01:43.327: I/MainActivity(1384): The absolute width:480pixels
09-14 20:01:43.327: I/MainActivity(1384): The absolute heightin:800pixels
09-14 20:01:43.339: I/MainActivity(1384): The absolute width:480pixels
09-14 20:01:43.339: I/MainActivity(1384): The absolute heightin:800pixels
09-14 20:01:43.339: I/MainActivity(1384): The logical density of the display.:1.5
09-14 20:01:43.339: I/MainActivity(1384): The absolute width:480pixels
09-14 20:01:43.339: I/MainActivity(1384): The absolute heightin:800pixels
09-14 20:01:43.339: I/MainActivity(1384): The logical density of the display.:1.5
09-14 20:01:43.339: I/MainActivity(1384): X dimension :240.0pixels per inch
09-14 20:01:43.343: I/MainActivity(1384): The absolute width:480pixels
09-14 20:01:43.343: I/MainActivity(1384): The absolute heightin:800pixels
09-14 20:01:43.343: I/MainActivity(1384): The logical density of the display.:1.5
09-14 20:01:43.343: I/MainActivity(1384): X dimension :240.0pixels per inch
09-14 20:01:43.343: I/MainActivity(1384): Y dimension :240.0pixels per inch
09-14 20:01:43.539: D/libEGL(1384): loaded /system/lib/egl/libEGL_emulation.so
09-14 20:01:43.551: D/(1384): HostConnection::get() New Host Connection established 0xb7d17680, tid 1384
09-14 20:01:43.571: D/libEGL(1384): loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-14 20:01:43.571: D/libEGL(1384): loaded /system/lib/egl/libGLESv2_emulation.so
09-14 20:01:43.707: W/EGL_emulation(1384): eglSurfaceAttrib not implemented
09-14 20:01:43.751: D/OpenGLRenderer(1384): Enabling debug mode 0
09-14 20:01:43.859: D/OpenGLRenderer(1384): TextureCache::get: create texture(0xb7cd38e0): name, size, mSize = 1, 1048576, 1048576
09-14 20:01:43.991: D/OpenGLRenderer(1384): TextureCache::get: create texture(0xb7cead10): name, size, mSize = 2, 5184, 1053760
09-14 20:01:44.119: D/OpenGLRenderer(1384): TextureCache::get: create texture(0xb7d0fa70): name, size, mSize = 4, 20736, 1074496
09-14 20:01:44.159: D/OpenGLRenderer(1384): TextureCache::get: create texture(0xb7cce840): name, size, mSize = 6, 2304, 1076800
09-14 20:06:09.639: D/OpenGLRenderer(1384): TextureCache::flush: target size: 646080
09-14 20:06:09.639: D/OpenGLRenderer(1384): TextureCache::callback: name, removed size, mSize = 1, 1048576, 28224
情景
将1280*720的apk适配到800*480的设备上
//首先在程序启动的适合,获取屏幕的真实分辨率,然后计算对应的缩放比例
DisplayMetrics dm = new DisplayMetrics();
dm = getApplicationContext().getResources().getDisplayMetrics();
Constant.SCREEN_WIDTH = dm.widthPixels;
Constant.SCREEN_HEIGHT = dm.heightPixels;
if (Constant.SCREEN_WIDTH > Constant.SCREEN_HEIGHT) {
Constant.SCREEN_HEIGHT = dm.widthPixels;
Constant.SCREEN_WIDTH = dm.heightPixels;
}
Constant.xScale = (float) Constant.SCREEN_WIDTH / Constant.CAMERA_WIDTH;
Constant.yScale = (float) Constant.SCREEN_HEIGHT/ Constant.CAMERA_HEIGHT;
下面对图片进行缩放:
/**
得到缩放后的bitmap
*/
protected Bitmap scaleBitmap(Bitmap bitmap) {
Bitmap newBitmap = null;
if (Constant.xScale != 1 || Constant.yScale != 1) {
bitmapWidth = (int) (bitmap.getWidth() * Constant.xScale);
bitmapHeight = (int) (bitmap.getHeight() * Constant.yScale);
newBitmap = Bitmap.createScaledBitmap(bitmap, (int) (bitmapWidth), (int) (bitmapHeight), true);
bitmap.recycle();
return newBitmap;
}
return bitmap;
}