android细小知识点整理
2015-10-17 23:04
393 查看
在开发android的过程中,经常会遇到一些细小简单的知识点记不太清了。然后又得花时间到网上查,针对这个问题今天把一些常用的知识进行整理及说明。
1.getX,Y 和getRawX,Y;
在event事件中我们常常会在用到getX,getRawX的时间不记得哪个是获取以屏幕左上角为坐标,哪个是以view左上角为坐标,getX以自身View为坐标,getRawX以屏幕为坐标。
2.dp2px,px2dp;
android开发过程中相比dp跟px的转换也是常常会用到的。虽然不是什么难点但还是把代码贴出来方便查看。
设备密度 320*240(0.75) 480*320(1) 1280*720(2)
Density
3.代码实例化View的几种方式
View.inflate(context, resource, root); //常用
LayoutInflater.from(this).inflate(resource, root);
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
4.EditText实用方法
setSelection(index)//设置光标显示位置
requestFocus();//请求获取焦点
getSelectionStart()//获取光标显示位置
getSelectionEnd()//获取光标结束位置
5.canvas.drawText()坐标计算公式
x= cellWidth/2 - textWidth/2;
y=cellHeight/2 +textHeight/2 +index*cellHeight;
drawText的坐标是以文字的左下角为起点。
paint.getTextBounds可以测量出文字的巨型,从而算出文字的高度跟宽度。
1.getX,Y 和getRawX,Y;
在event事件中我们常常会在用到getX,getRawX的时间不记得哪个是获取以屏幕左上角为坐标,哪个是以view左上角为坐标,getX以自身View为坐标,getRawX以屏幕为坐标。
2.dp2px,px2dp;
android开发过程中相比dp跟px的转换也是常常会用到的。虽然不是什么难点但还是把代码贴出来方便查看。
设备密度 320*240(0.75) 480*320(1) 1280*720(2)
public class DensityUtils { public static int dp2px(Context ctx,float num){ float density = ctx.getResources().getDisplayMetrics().density; int px = (int) (num*density+0.5f); return px; } public static int px2dp(Context ctx,float num){ float density = ctx.getResources().getDisplayMetrics().density; int dp = (int) (num/density+0.5f); return dp; } }
Density
3.代码实例化View的几种方式
View.inflate(context, resource, root); //常用
LayoutInflater.from(this).inflate(resource, root);
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
4.EditText实用方法
setSelection(index)//设置光标显示位置
requestFocus();//请求获取焦点
getSelectionStart()//获取光标显示位置
getSelectionEnd()//获取光标结束位置
5.canvas.drawText()坐标计算公式
x= cellWidth/2 - textWidth/2;
y=cellHeight/2 +textHeight/2 +index*cellHeight;
drawText的坐标是以文字的左下角为起点。
paint.getTextBounds可以测量出文字的巨型,从而算出文字的高度跟宽度。
相关文章推荐
- android 水波纹
- ViewGroup的事件分发
- Android 推门效果
- Android中最佳常量定义方法
- android Gif播放
- Android OpenGL ES(1)
- Android中ListView之性能提高
- java4Android(23):equals 的使用方法 instance 操作符【==与equals方法的区别】
- android简易跑马灯
- Android TextView不带边框问题
- Android library上传到jcenter
- Android滑动菜单特效实现
- Android SDK下载和更新失败的解决方法
- View的事件分发机制
- android 34 ListView进阶
- android 水果老虎机 文档一
- Android第三方开源框架之SlidingMenu详解
- 六款值得推荐的android(安卓)开源框架简介
- android框架XUtils使用方法
- Android 使用ColorMatrix改变图片颜色