达内首页 > IT技术学院 > Android学院 > Android 的一些实用的函数
Android 的一些实用的函数
作者:广州达内科技 更新时间:2014-05-09 15:57 来源:Android开发培训

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);

}


上一篇:Android开发学习小TIPS
下一篇:【技术分享】Android如何创建新项目及开发

相关资讯

  • [2014-05-22 14:36:34] 【技术分享】Android如何创建新项目及开发
  • [2014-05-09 15:57:48] Android 的一些实用的函数
  • [2014-04-21 11:44:42] Android开发学习小TIPS
  • [2014-04-18 11:26:21] android raw文件夹导入数据库
  • [2014-04-16 11:45:38] Android开发中java命名规则包的命名
  • [2014-04-10 11:51:29] 关于Android 中Service生命周期的介绍
  • 【2013-2月28日】
    Java 软件工程师就业班
    >>热招中!


    【2013-2月28日】
    3G-Android 工程师就业班
    >>热招中!


    【2013-2月28日】
    3G-Android 工程师周末班
    >>热招中!

    【2013-2月28日】
    软件测试 工程师就业班
    >>热招中!