Android常用代码片段
来源:互联网 发布:淘宝上的摩托车靠谱吗 编辑:程序博客网 时间:2024/05/18 03:19
一. 获取资源图片Drawable的四种方式:
//方式一Drawable drawable = mContext.getResources().getDrawable(R.drawable.image);mImageview.setBackground(drawable);//方式二InputStream is = mContext.getResources().openRawResource(R.drawable.image); Bitmap bitmap = BitmapFactory.decodeStream(is);//方式三 Inputstream is = mContext.getResources().openRawResource(R.drawable.image);BitmapDrawable bitmapDrawable = new BitmapDrawable(is);Bitmap bitmap = bmpDraw.getBitmap();//方式四Bitmap bitmap=BitmapFactory.decodeResource(r, R.drawable.image);
二. 从assets中读取图片和文本
- 从Assets中读取图片
public static Drawable getImageFromAssets(final Context context, String fileName) { try { InputStream is = context.getResources().getAssets().open(fileName); return Drawable.createFromStream(is, null); } catch (IOException e) { if (e != null) { e.printStackTrace(); } } catch (OutOfMemoryError e) { if (e != null) { e.printStackTrace(); } } catch (Exception e) { if (e != null) { e.printStackTrace(); } } return null; }
- 从Assets读取文本
public static String getTextFromAssets(final Context context, String fileName) { String result = ""; try { InputStream in = context.getResources().getAssets().open(fileName); int lenght = in.available(); byte[] buffer = new byte[lenght]; in.read(buffer); result = EncodingUtils.getString(buffer, "UTF-8"); in.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
三.Android 切换全屏
1.切换成全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
- 非全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
四. px, dp, sp三者互相转换
记录常用的工具方法,为了以后方便使用。
//1. dp-->px public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); }//2. px-->dppublic static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f);}//3. sp-->pxpublic static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f);}//4. px-->sppublic static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f);}
0 0
- android常用代码片段
- android常用代码片段
- android常用代码片段
- Android常用代码片段
- Android 常用代码片段
- android常用代码片段
- Android常用代码片段
- 常用android代码片段
- android 常用代码片段
- Android 常用代码片段
- android 常用代码片段
- android 常用代码片段
- Android常用代码片段
- android开发常用代码片段
- Android 开发常用代码片段
- Android 开发常用代码片段
- Android 开发常用代码片段
- Android 开发常用代码片段
- canvas霓虹灯
- HDU 2036 叉乘求三角形面积
- 嵌入式开源GUI项目
- IE8下ajaxFileUpload上传文件成功,但不进入回调函数,而是提示下载信息
- https配置从tomcat迁移到Nginx
- Android常用代码片段
- LocationManager定位国内偏移,WGS-84、GCJ-02、BD-09之间转换
- Codeforces 246B Increase and Decrease
- 架构设计:系统存储(6)——MySQL数据库性能优化(2)
- SDL2 with OpenGL
- bzoj2141: 排队(树套树 或 分块)
- Mysql的增删改查
- 使用科大讯飞SDK实现语音功能
- C/C++通过WMI和系统API函数获取系统硬件配置信息