一些小功能的总结

来源:互联网 发布:彩票源码论坛php 编辑:程序博客网 时间:2024/05/16 08:23

一些常用的小功能,不算是技术,一个小总结。

1.获取屏幕尺寸、密度等信息。
    1)最常用的方法:
        WindowManager windowManager = getWindowManager();   
        Display display = windowManager.getDefaultDisplay();   
        int w = display.getWidth();   
        int h = display.getHeight();
    2)用DisplayMetrics来获得参数:
        DisplayMetrics displayMetrics = new DisplayMetrics() ;
        displayMetrics = getResources().getDisplayMetrics();
        int width = display.getWidth();
        int height = display.getHeight();
        int densityDpi = displayMetrics.densityDpi;
    但是,需要注意的是,在一个低密度的小屏手机上,仅靠上面的代码是不能获取正确的尺寸的。比如说,一部240x320像素的低密度手机,如果运行上述代码,获取到的屏幕尺寸是320x427。因此,研究之后发现,若没有设定多分辨率支持的话,Android系统会将240x320的低密度(120)尺寸转换为中等密度(160)对应的尺寸,这样的话就大大影响了程序的编码。所以,需要在工程的AndroidManifest.xml文件中,加入supports-screens节点,具体的内容如下:
        <supports-screens
            android:smallScreens="true"
            android:normalScreens="true"
            android:largeScreens="true"
            android:xlargeScreens="true"
            android:resizeable="true"
            android:anyDensity="true" />
    这样的话,当前的Android程序就支持了多种分辨率,那么就可以得到正确的物理尺寸了。
px与dip转换公式:
 pixs =dips * (densityDpi/160). 
 dips=(pixs*160)/densityDpi


dp与px转换的方法
 public static int dip2px(Context context, float dipValue){
   final float scale = context.getResources().getDisplayMetrics().density;
   return (int)(dipValue * scale +0.5f);
  }

  public static int px2dip(Context context, float pxValue){
   final float scale = context.getResource().getDisplayMetrics().density;
   return (int)(pxValue / scale +0.5f);
  }

2.空间置顶:永远保留在最顶层,类似于天天动听的桌面歌词那样,始终在最顶层。
API中有这样一个常量:WindowManager.LayoutParams.TYPE_SYSTEM_ALERT。文档中对它的的描述为:“Window type: system window, such as low power alert. These windows are always on top of application windows.”也就是说用这个type可以显示在其他application的顶层,但注意:当用到这个常量的时候,要加上相应的权限,即:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />。这个权限在文档中的描述为:“Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. Very few applications should use this permission; these windows are intended for system-level interaction with the user.”
WindowManager mWin = (WindowManager)getSystemService(Context.WINDOW_SERVICE)
然后使用addView的方法创建窗体,这样的窗体时凌驾于任何程序之上的。

3.设置全屏和无标题:在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果。其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏。
其一:在代码中设置(如下)

 public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
          
        //设置无标题  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        //设置全屏  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
          
        setContentView(R.layout.main);  
 }  
 但要注意的是:在代码中设置的话,设置无标题和设置全屏的两段代码要放置在 setContentView(R.layout.main); 这段代码的前面。要不然会报错。

    其二:在manifest配置文件中设置
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="com.andyidea"  
      android:versionCode="1"  
      android:versionName="1.0">  
    <uses-sdk android:minSdkVersion="8" />  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".login.LoginActivity"   
                  android:theme="@android:style/android.NoTitleBar.Fullscreen"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
 </manifest>
 在相应的Activity中节点中添加属性:android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 即可以设置某个Activity全屏显示。若设置成         android:theme="@android:style/Theme.NoTitleBar" 即是只是设置成无标题状态。

4.在代码中设置背景:
  Resources res = getResources();
      Drawable drawable = res.getDrawable(R.drawable.bkcolor);
      this.getWindow().setBackgroundDrawable(drawable);
  需要在value目录下的string.xml文件中加入一段代码,如<drawable name="bkcolor">#ff00ff</drawable>

5.在ondraw中获取text文本的长和宽:
  1)字符串宽度 Paint.measureText 方法,这个方法很简单。
  2)字符高度使用 FontMetrics 类:
  Paint pFont = new Paint();
  Rect rect = new Rect();
  //返回包围整个字符串的最小的一个Rect区域
  pFont.getTextBounds(str, 0, 1, rect);
  strwid = rect.width();
  strhei = rect.height();

6.抗锯齿:在画图时,由于旋转或者缩放后,会产生锯齿,可以通过这样解决。
  1)直接在paint上加上抗锯齿
       paint.setAntiAlias(true);
  2)在不能给paint加抗锯齿的地方,可以直接给canvas加抗锯齿
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));

7.获取sd卡的绝对路径:
  Environment.getExternalStorageDirectory().toString();//要是加"//music//xxxx.mp3"


http://www.cnblogs.com/vtianyun/articles/2450996.html

原创粉丝点击