Android 的一些实用的函数
2011-03-25 18:01
267 查看
1. 获得屏幕的密度, 用于屏幕适配
public static float getDensity(Context ctx) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
return metrics.density;
}
2. 获得版本的名字:
public static String getVersionName(Context context, String packageName) {
PackageInfo pInfo = null;
String rs = "";
try {
pInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
rs = pInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
3. 获得图片的倒影,同时倒影渐变效果.
public static Bitmap createMirro(Bitmap srcbitmap) {
int width = srcbitmap.getWidth();
int height = srcbitmap.getHeight();
int shadow_height = 15; // TODO
int[] pixels = new int[width * height];
srcbitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// shadow effect
int alpha = 0x00000000;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int index = y * width + x;
int r = (pixels[index] >> 16) & 0xff;
int g = (pixels[index] >> 8) & 0xff;
int b = pixels[index] & 0xff;
pixels[index] = alpha | (r << 16) | (g << 8) | b;
}
if (y >= (height - shadow_height)) {
alpha = alpha + 0x1F000000;
}
}
// invert effect
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int y = 0; y < height; y++) {
bm.setPixels(pixels, y * width, width, 0, height - y - 1, width, 1);
}
return Bitmap.createBitmap(bm, 0, 0, width, shadow_height);
}
public static float getDensity(Context ctx) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
return metrics.density;
}
2. 获得版本的名字:
public static String getVersionName(Context context, String packageName) {
PackageInfo pInfo = null;
String rs = "";
try {
pInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
rs = pInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
3. 获得图片的倒影,同时倒影渐变效果.
public static Bitmap createMirro(Bitmap srcbitmap) {
int width = srcbitmap.getWidth();
int height = srcbitmap.getHeight();
int shadow_height = 15; // TODO
int[] pixels = new int[width * height];
srcbitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// shadow effect
int alpha = 0x00000000;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int index = y * width + x;
int r = (pixels[index] >> 16) & 0xff;
int g = (pixels[index] >> 8) & 0xff;
int b = pixels[index] & 0xff;
pixels[index] = alpha | (r << 16) | (g << 8) | b;
}
if (y >= (height - shadow_height)) {
alpha = alpha + 0x1F000000;
}
}
// invert effect
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int y = 0; y < height; y++) {
bm.setPixels(pixels, y * width, width, 0, height - y - 1, width, 1);
}
return Bitmap.createBitmap(bm, 0, 0, width, shadow_height);
}
相关文章推荐
- Android一些实用的函数
- 【File】Android中关于内部存储的一些重要函数
- 一些非常实用的 Android 开发资源( 开源框架)
- Android中关于内部存储的一些重要函数
- 分享一些本人自己收藏实用的Android OpenGL 书籍文档
- Android中关于外部存储的一些重要函数
- 关于Android RenderScript 的详细说明和一些实用文档
- 【File】Android中关于外部存储的一些重要函数
- Android 一些非常实用的 Android 开发资源
- 一些前端开发实用的函数—2(jquery)
- Android和“时间”有关的一些常用函数
- Android工具类一些方法函数
- Android平台调用so库中函数的流程及一些坑
- Android中关于内部存储的一些重要函数
- 一些实用小函数
- Android开发点点滴滴——一些基础的但实用的知识(2)
- Android应用中,退出activity时对一些资源以及状态的操作保存,可以在生命周期的哪个函数中进行?
- Android中关于内部存储的一些重要函数
- PB一些有关日期的实用函数
- C#File类常用的文件操作方法(创建、移动、删除、复制等) File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和