zoukankan      html  css  js  c++  java
  • Android 获取 上下文环境参数 getResources

    1----context.getResources().getConfiguration().orientation;//获取屏幕方向int类型,1:portrait,2:landscape

    2---把资源文件放到应用程序的/raw/raw下,以openRawResource方法(不带后缀的资源文件名)打开这个文件

    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 r = this.getContext().getResources(); //得到Resources对象 
    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

    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_item_values);
    valuesarray.xml
    <string-array name="array_item_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>

  • 相关阅读:
    2012 金华 现场赛
    依据错误原理解决Hibernate执行出现No CurrentSessionContext configured!错误
    【python】a[::-1]翻转
    【算法】各种哈希算法
    【wireshark】打开后显示There are no interfaces on which a capture can be done
    【python】在python中调用mysql
    【mysql】执行mysql脚本
    【mysql】用navicat连接虚拟机mysql出现错误代码(10038)
    【python-mysql】在ubuntu下安装python-mysql环境
    【python】lamda表达式,map
  • 原文地址:https://www.cnblogs.com/polo/p/9922867.html
Copyright © 2011-2022 走看看