zoukankan      html  css  js  c++  java
  • context.getResources()用法总结

    context.getResources()用法总结
    
    1、context.getResources().getConfiguration().orientation;//获取屏幕方向int类型,1:portrait,2:landscape
    
    把资源文件放到应用程序的/raw/raw下,以openRawResource方法(不带后缀的资源文件名)打开这个文件
    2、InputStream fs =this.getResources().openRawResource(R.raw.index.htm); //(资源文件名为index.html, 不需要带后缀.htm)    
        InputStreamReader read = new InputStreamReader (fs,"utf-8");    
        BufferedReader in = new BufferedReader(read);
        
    
    3、读取res/drawable目录下的png或者bmp
    //得到Resources对象    
    Resources r = this.getContext().getResources();    
    //以数据流的方式读取资源    
    Inputstream is = r.openRawResource(R.drawable.mm_image);    
    BitmapDrawable bmpDraw = new BitmapDrawable(is);    
    Bitmap bmp = bmpDraw.getBitmap();
    如果需要利用图片解码器,如下使用:
    InputStream is = getResources().openRawResource(R.drawable.icon);    
    Bitmap mBitmap = BitmapFactory.decodeStream(is);    
    Paint mPaint = new Paint();    
    canvas.drawBitmap(mBitmap, 40, 40, mPaint);
    
    4、float density = getResources().getDisplayMetrics().density;//获取屏幕密度
    
    5、Context.getResources().getDimensionPixelSize();//getDimension,getDimensionPixelOffset和getDimensionPixelSize的一点说明:
    getDimension和getDimensionPixelOffset的功能类似,
    都是获取某个dimen的值,但是如果单位是dp或sp,则需要将其乘以density
    如果是px,则不乘。并且getDimension返回float,getDimensionPixelOffset返回int.
    而getDimensionPixelSize则不管写的是dp还是sp还是px,都会乘以denstiy.
    
    6、Context.getResources().getColor(R.color.colorId);//获取颜色资源
    7、Context.getResources().getString(R.string.stringId);//获取字符串资源
    8、获取字符串数组:  
    String[] roles = context.getResources().getStringArray(R.array.array_role_values);  
    valuesarray.xml
    <string-array name="array_role_values">
            <item>3</item>
            <item>4</item>
            <item>15</item>
            <item>11</item>
            <item>25</item>
            <item>24</item>
            <item>7</item>
            <item>14</item>
            <item>22</item>
            <item>8</item>
            <item>9</item>
        </string-array>
    9、使用getIdentifier()方法可以方便的获各应用包下的指定资源ID。
    方式一 
    int indentify = getResources().getIdentifier(“com.test.demo:drawable/icon”,null,null);
    第一个参数格式是:包名 + : +资源文件夹名 + / +资源名;是这种格式 然后其他的可以为null
    方式二 
    intindentify= getResources().getIdentifier(“icon”, “drawable”, “com.test.demo”);
    第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名。
    example:
    int resourceID = VAssistantConfig.getAppContext().getResources().getIdentifier(string + "_tablet",
                        "string", VAssistantConfig.getAppContext().getPackageName());
    
    
    
     
  • 相关阅读:
    Java 正则表达式的总结和一些小例子
    jquery用div模拟一个下拉列表框
    jquery点击div以外的区域触发事件
    Jackson 框架,轻易转换JSON
    Java Json API:Gson使用简单入门
    linux必学
    Python2 long() 函数
    Python int() 函数
    PyCharm快捷键大全
    PyCharm常用设置
  • 原文地址:https://www.cnblogs.com/sanbianxia/p/7687812.html
Copyright © 2011-2022 走看看