Android中的坐標(biāo)系
- 屏幕坐標(biāo)系
移動設(shè)備一般定義屏幕左上角為坐標(biāo)原點,向右為x軸增大方向,向下為y軸增大方向
- View的坐標(biāo)系
View的坐標(biāo)系統(tǒng)是相對于父控件而言的项炼。
getTop(); //獲取子View左上角距父View頂部的距離
getLeft(); //獲取子View左上角距父View左側(cè)的距離
getBottom(); //獲取子View右下角距父View頂部的距離
getRight(); //獲取子View右下角距父View左側(cè)的距離
- MotionEvent中 get 和 getRaw 的區(qū)別
event.getX(); //觸摸點相對于其所在組件坐標(biāo)系的坐標(biāo)
event.getY();
event.getRawX(); //觸摸點相對于屏幕默認(rèn)坐標(biāo)系的坐標(biāo)
event.getRawY();
角度與弧度
在我們自定義View鸽凶,尤其是制作一些復(fù)雜炫酷的效果的時候,實際上是將一些簡單的東西通過數(shù)學(xué)上精密的計算組合到一起形成的效果尖飞。這其中可能會涉及到畫布的相關(guān)操作(旋轉(zhuǎn)),以及一些正余弦函數(shù)的計算等,這些內(nèi)容就會用到一些角度政基、弧度相關(guān)的知識贞铣。
- 角度:兩條射線從圓心向圓周射出,形成一個夾角和夾角正對的一段弧沮明。當(dāng)這段弧長正好等于圓周長的360分之一時辕坝,兩條射線的夾角的大小為1度。
- 弧度:兩條射線從圓心向圓周射出荐健,形成一個夾角和夾角正對的一段弧酱畅。當(dāng)這段弧長正好等于圓的半徑時,兩條射線的夾角大小為1弧度摧扇。
圓一周對應(yīng)的角度為360度(角度)圣贸,對應(yīng)的弧度為2π弧度。
故得等價關(guān)系:360(角度) = 2π(弧度) ==> 180(角度) = π(弧度)
//rad 是弧度扛稽, deg 是角度
rad = deg x π / 180
deg = rad x 180 / π
顏色
幾種創(chuàng)建或使用顏色的方式
- java中定義顏色
int color = Color.GRAY; //灰色
int color = Color.argb(127, 255, 0, 0); //半透明紅色
int color = 0xaaff0000; //帶有透明度的紅色
- 在xml文件中定義顏色
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
#f00 //低精度 - 不帶透明通道紅色
#af00 //低精度 - 帶透明通道紅色
#ff0000 //高精度 - 不帶透明通道紅色
#aaff0000 //高精度 - 帶透明通道紅色
自定義屬性
Android自定義屬性可分為以下幾步:
- 自定義屬性的聲明文件
編寫values/attrs.xml吁峻,在其中編寫styleable和item等標(biāo)簽元素
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="test">
<attr name="text" format="string" />
<attr name="testAttr" format="integer" />
</declare-styleable>
</resources>
2.自定義View中獲取屬性
//在View的構(gòu)造方法中通過TypedArray獲取
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
String text = ta.getString(R.styleable.test_testAttr);
int textAttr = ta.getInteger(R.styleable.test_text, -1);
3.在布局中使用
<com.example.test.MyView
android:layout_width="120dp"
android:layout_height="120dp"
app:testAttr="520"
app:text="helloworld" />
4.屬性值的類型歸納
- reference資源id
//屬性定義
<attr name = "background" format = "reference" />
//屬性使用
<ImageView android:background = "@drawable/圖片ID"/>
- color顏色值
//屬性定義
<attr name = "textColor" format = "color" />
//屬性使用
<TextView android:textColor = "#00FF00" />
- boolean布爾值
//屬性定義
<attr name = "focusable" format = "boolean" />
//屬性使用
<Button android:focusable = "true"/>
- dimension尺寸值
//屬性定義
<attr name = "layout_width" format = "dimension" />
//屬性使用
<Button android:layout_width = "42dp"/>
- float浮點值
//屬性定義
<attr name = "fromAlpha" format = "float" />
//屬性使用
<alpha android:fromAlpha = "1.0"/>
- integer整型值
//屬性定義
<attr name = "framesCount" format="integer" />
//屬性使用
<animated-rotate android:framesCount = "12"/>
- string字符串
//屬性定義
<attr name = "text" format = "string" />
//屬性使用
<TextView android:text = "我是文本"/>
- fraction百分?jǐn)?shù)
//屬性定義
<attr name = "pivotX" format = "fraction" />
//屬性使用
<rotate android:pivotX = "200%"/>
- enum枚舉
//屬性定義
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
//屬性使用
<LinearLayout
android:orientation = "vertical">
</LinearLayout>
- flag位或運算
//屬性定義
<attr name="gravity">
<flag name="top" value="0x01" />
<flag name="bottom" value="0x02" />
<flag name="left" value="0x04" />
<flag name="right" value="0x08" />
<flag name="center_vertical" value="0x16" />
...
</attr>
//屬性使用
<TextView android:gravity="bottom|left"/>
- 混合類型
//屬性定義
<attr name = "background" format = "reference|color" />
//屬性使用
<ImageView
android:background = "@drawable/圖片ID" />
或者:
<ImageView
android:background = "#00FF00" />