android自定义View学习1-准备基础知识

参考

英勇青铜5
hencoder
Carson_Ho

基础

坐标系

View的位置只要由它的四个顶点来决定。分别对应于View的四个属性:

  • top ,getTop()左上角的纵坐标
  • left,getLeft() 左上角的横坐标
  • right,getRight() 右下角的横坐标
  • bottom,getBottom() 右下角的纵坐标
    view坐标体系

获得方式

1
2
3
4
5
6
7
8
// 获取Top位置
public final int getTop() {
return mTop;
}
// 其余如下:
getLeft(); //获取子View左上角距父View左侧的距离
getBottom(); //获取子View右下角距父View顶部的距离
getRight(); //获取子View右下角距父View左侧的距离
  • 与MotionEvent中 get()和getRaw()的区别
    1
    2
    3
    4
    5
    6
    //get() :触摸点相对于其所在组件坐标系的坐标
    event.getX();
    event.getY();
    //getRaw() :触摸点相对于屏幕默认坐标系的坐标
    event.getRawX();
    event.getRawY();

MotionEvent

角度

从左向右顺时针是安卓角度增大的方向
android角度

颜色

Android中颜色相关内容
Android中的颜色相关内容包括颜色模式,创建颜色的方式,以及颜色的混合模式等。

颜色模式

Android支持的颜色模式:
Android颜色模式

以ARGB8888为例介绍颜色定义:
ARGB88888

定义颜色的方式

在java中定义颜色
1
2
3
4
5
//java中使用Color类定义颜色
int color = Color.GRAY; //灰色
//Color类是使用ARGB值进行表示
int color = Color.argb(127, 255, 0, 0); //半透明红色
int color = 0xaaff0000; //带有透明度的红色
在xml文件中定义颜色

在/res/values/color.xml 文件中如下定义:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<resources>

//定义了红色(没有alpha(透明)通道)
<color name="red">#ff0000</color>
//定义了蓝色(没有alpha(透明)通道)
<color name="green">#00ff00</color>
</resources>

在xml文件中以”#“开头定义颜色,后面跟十六进制的值,有如下几种定义方式:

1
2
3
4
#f00            //低精度 - 不带透明通道红色
#af00 //低精度 - 带透明通道红色
#ff0000 //高精度 - 不带透明通道红色
#aaff0000 //高精度 - 带透明通道红色

引用颜色的方式

在java文件中引用xml中定义的颜色:
1
2
3
4
//方法1
int color = getResources().getColor(R.color.mycolor);
//方法2(API 23及以上)
int color = getColor(R.color.myColor);
在xml文件(layout或style)中引用或者创建颜色
1
2
3
4
5
6
7
8
<!--在style文件中引用-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/red</item>
</style>
<!--在layout文件中引用在/res/values/color.xml中定义的颜色-->
android:background="@color/red"
<!--在layout文件中创建并使用颜色-->
android:background="#ff0000"

测量(Measure)