給我一個(gè)Path,還你一個(gè)動(dòng)畫(huà):
最近看到一篇很有意思的文章,只要你能給我一個(gè)Path ,我就可以給你一個(gè)View。文章地址請(qǐng)移步:【Android】 給我一個(gè)Path,還你一個(gè)酷炫動(dòng)畫(huà) - zxt0601的博客 - 博客頻道 - CSDN.NET
但是當(dāng)你看到文章后自己就會(huì)覺(jué)得有點(diǎn)蒙圈,有很多的疑問(wèn)。
這個(gè)Path 怎么來(lái)的?

這些字母問(wèn)路徑是怎么獲取的呢?
接下來(lái)請(qǐng)大家看下如此簡(jiǎn)單的代碼:

沒(méi)錯(cuò)在自定義View時(shí)加上這么一局便可完成這么一個(gè)酷炫的路徑(未加動(dòng)畫(huà))。
相信如果大家仔細(xì)看必然會(huì)發(fā)現(xiàn)以上的字母并不是原始的字母,它是由很多的直線組成的并不圓滑。
不和大家繞彎子,以下是見(jiàn)證奇跡的時(shí)刻:
public classStoreHousePath {
private static finalSparseArraysPointList;
static{
sPointList=newSparseArray();
float[][] LETTERS =new float[][]{
new float[]{
// A
24,0,1,22,
1,22,1,72,
24,0,47,22,
47,22,47,72,
1,48,47,48
},
new float[]{
// B
0,0,0,72,
0,0,37,0,
37,0,47,11,
47,11,47,26,
47,26,38,36,
38,36,0,36,
38,36,47,46,
47,46,47,61,
47,61,38,71,
37,72,0,72,
},
new float[]{
// C
47,0,0,0,
0,0,0,72,
0,72,47,72,
},
new float[]{
// D
0,0,0,72,
0,0,24,0,
24,0,47,22,
47,22,47,48,
47,48,23,72,
23,72,0,72,
},
new float[]{
// E
0,0,0,72,
0,0,47,0,
0,36,37,36,
0,72,47,72,
},
new float[]{
// F
0,0,0,72,
0,0,47,0,
0,36,37,36,
},
new float[]{
// G
47,23,47,0,
47,0,0,0,
0,0,0,72,
0,72,47,72,
47,72,47,48,
47,48,24,48,
},
new float[]{
// H
0,0,0,72,
0,36,47,36,
47,0,47,72,
},
new float[]{
// I
0,0,47,0,
24,0,24,72,
0,72,47,72,
},
new float[]{
// J
47,0,47,72,
47,72,24,72,
24,72,0,48,
},
new float[]{
// K
0,0,0,72,
47,0,3,33,
3,38,47,72,
},
new float[]{
// L
0,0,0,72,
0,72,47,72,
},
new float[]{
// M
0,0,0,72,
0,0,24,23,
24,23,47,0,
47,0,47,72,
},
new float[]{
// N
0,0,0,72,
0,0,47,72,
47,72,47,0,
},
new float[]{
// O
0,0,0,72,
0,72,47,72,
47,72,47,0,
47,0,0,0,
},
new float[]{
// P
0,0,0,72,
0,0,47,0,
47,0,47,36,
47,36,0,36,
},
new float[]{
// Q
0,0,0,72,
0,72,23,72,
23,72,47,48,
47,48,47,0,
47,0,0,0,
24,28,47,71,
},
new float[]{
// R
0,0,0,72,
0,0,47,0,
47,0,47,36,
47,36,0,36,
0,37,47,72,
},
new float[]{
// S
47,0,0,0,
0,0,0,36,
0,36,47,36,
47,36,47,72,
47,72,0,72,
},
new float[]{
// T
0,0,47,0,
24,0,24,72,
},
new float[]{
// U
0,0,0,72,
0,72,47,72,
47,72,47,0,
},
new float[]{
// V
0,0,24,72,
24,72,47,0,
},
new float[]{
// W
0,0,0,72,
0,72,24,49,
24,49,47,72,
47,72,47,0
},
new float[]{
// X
0,0,47,72,
47,0,0,72
},
new float[]{
// Y
0,0,24,23,
47,0,24,23,
24,23,24,72
},
new float[]{
// Z
0,0,47,0,
47,0,0,72,
0,72,47,72
},
};
final float[][] NUMBERS =new float[][]{
new float[]{
// 0
0,0,0,72,
0,72,47,72,
47,72,47,0,
47,0,0,0,
},
new float[]{
// 1
24,0,24,72,
},
new float[]{
// 2
0,0,47,0,
47,0,47,36,
47,36,0,36,
0,36,0,72,
0,72,47,72
},
new float[]{
// 3
0,0,47,0,
47,0,47,36,
47,36,0,36,
47,36,47,72,
47,72,0,72,
},
new float[]{
// 4
0,0,0,36,
0,36,47,36,
47,0,47,72,
},
new float[]{
// 5
0,0,0,36,
0,36,47,36,
47,36,47,72,
47,72,0,72,
0,0,47,0
},
new float[]{
// 6
0,0,0,72,
0,72,47,72,
47,72,47,36,
47,36,0,36
},
new float[]{
// 7
0,0,47,0,
47,0,47,72
},
new float[]{
// 8
0,0,0,72,
0,72,47,72,
47,72,47,0,
47,0,0,0,
0,36,47,36
},
new float[]{
// 9
47,0,0,0,
0,0,0,36,
0,36,47,36,
47,0,47,72,
}
};
// A - Z
for(inti =0;i < LETTERS.length;i++) {
sPointList.append(i +65,LETTERS[i]);
}
// a - z
for(inti =0;i < LETTERS.length;i++) {
sPointList.append(i +65+32,LETTERS[i]);
}
// 0 - 9
for(inti =0;i < NUMBERS.length;i++) {
sPointList.append(i +48,NUMBERS[i]);
}
// blank
addChar(' ', new float[]{});
// -
addChar('-', new float[]{
0,36,47,36
});
// .
addChar('.', new float[]{
24,60,24,72
});
}
public static voidaddChar(charc, float[] points) {
sPointList.append(c,points);
}
public staticArrayListgetPath(String str) {
returngetPath(str,1,14);
}
/**
*@paramstr
*@paramscale
*@paramgapBetweenLetter
*@returnArrayList of float[] {x1, y1, x2, y2}
*/
public staticArrayListgetPath(String str, floatscale, intgapBetweenLetter) {
ArrayList list =newArrayList();
floatoffsetForWidth =0;
for(inti =0;i < str.length();i++) {
intpos = str.charAt(i);
intkey =sPointList.indexOfKey(pos);
if(key == -1) {
continue;
}
float[] points =sPointList.get(pos);
intpointCount = points.length/4;
for(intj =0;j < pointCount;j++) {
float[] line =new float[4];
for(intk =0;k <4;k++) {
floatl = points[j *4+ k];
// x
if(k %2==0) {
line[k] = (l + offsetForWidth) * scale;
}
// y
else{
line[k] = l * scale;
}
}
list.add(line);
}
offsetForWidth +=57+ gapBetweenLetter;
}
returnlist;
}
}
其實(shí)最重要的時(shí) 實(shí)例化好的float[][] LETTERS,這個(gè)數(shù)組將我們的字母進(jìn)行了點(diǎn)陣處理。
說(shuō)白了漢字也是完全可以的,只不過(guò)漢字的點(diǎn)整就不止3*3 或者4*4 了(可以使用點(diǎn)陣生成器)。
以上原來(lái)就是令我深深折服的地方。
下面是GitDemo:GitHub - VampireCarrot/CustomView: 自定義View由菜鳥(niǎo)到大神