public class NinePatchBitmapFactory {
private static final int NO_COLOR = 0x00000001;
private static final int TRANSPARENT_COLOR = 0x00000000;
public static NinePatchDrawable createNinePatchDrawable(Resources res, Bitmap bitmap) {
RangeLists rangeLists = checkBitmap(bitmap);
Bitmap trimedBitmap = trimBitmap(bitmap);
NinePatchDrawable drawable = createNinePatchWithCapInsets( res , trimedBitmap , rangeLists.rangeListX , rangeLists.rangeListY , null );
return drawable;
}
public static NinePatchDrawable createNinePatchWithCapInsets(Resources res, Bitmap bitmap,
List<Range> rangeListX, List<Range> rangeListY , String srcName) {
ByteBuffer buffer = getByteBuffer(rangeListX,rangeListY);
NinePatchDrawable drawable = new NinePatchDrawable(res,bitmap, buffer.array(), new Rect(), srcName);
return drawable;
}
private static ByteBuffer getByteBuffer(List<Range> rangeListX, List<Range> rangeListY) {
ByteBuffer buffer = ByteBuffer.allocate(4 + 4*7 + 4*2*rangeListX.size() + 4*2*rangeListY.size() + 4*9).order(ByteOrder.nativeOrder());
buffer.put((byte)0x01); // was serialised
buffer.put((byte) (rangeListX.size() * 2) ); // x div
buffer.put((byte) (rangeListY.size() * 2) ); // y div
buffer.put((byte)0x09); // color
// skip
buffer.putInt(0);
buffer.putInt(0);
// padding
buffer.putInt(0);
buffer.putInt(0);
buffer.putInt(0);
buffer.putInt(0);
// skip 4 bytes
buffer.putInt(0);
for (Range range : rangeListX) {
buffer.putInt( range.start );
buffer.putInt( range.end );
}
for (Range range : rangeListY) {
buffer.putInt( range.start);
buffer.putInt( range.end );
}
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
buffer.putInt(NO_COLOR);
return buffer;
}
public static class RangeLists {
public List<Range> rangeListX;
public List<Range> rangeListY;
}
public static class Range {
public int start;
public int end;
}
public static RangeLists checkBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
List<Range> rangeListX = new ArrayList<Range>();
int pos = -1;
for (int i=1;i<width-1;i++) {
int color = bitmap.getPixel(i, 0);
int alpha = Color.alpha( color );
int red = Color.red( color );
int green = Color.green( color );
int blue = Color.blue( color );
if (alpha == 255 && red == 0 && green == 0 && blue == 0) {
if (pos == -1) {
pos = i-1;
}
} else {
if (pos != -1) {
Range range = new Range();
range.start = pos;
range.end = i-1;
rangeListX.add( range );
pos = -1;
}
}
}
if (pos != -1) {
Range range = new Range();
range.start = pos;
range.end = width-2;
rangeListX.add( range );
}
List<Range> rangeListY = new ArrayList<Range>();
pos = -1;
for (int i=1;i<height-1;i++) {
int color = bitmap.getPixel(0, i);
int alpha = Color.alpha( color );
int red = Color.red( color );
int green = Color.green( color );
int blue = Color.blue( color );
if (alpha == 255 && red == 0 && green == 0 && blue == 0) {
if (pos == -1) {
pos = i-1;
}
} else {
if (pos != -1) {
Range range = new Range();
range.start = pos;
range.end = i-1;
rangeListY.add( range );
pos = -1;
}
}
}
if (pos != -1) {
Range range = new Range();
range.start = pos;
range.end = height-2;
rangeListY.add( range );
}
RangeLists rangeLists = new RangeLists();
rangeLists.rangeListX = rangeListX;
rangeLists.rangeListY = rangeListY;
return rangeLists;
}
public static Bitmap trimBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap result = Bitmap.createBitmap(bitmap , 1 , 1 , width -2 , height -2 );
return result;
}
public static Bitmap loadBitmap(File file) {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream( new FileInputStream(file) );
return BitmapFactory.decodeStream(bis);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (Exception e) {
}
}
return null;
}
public static String getDensityPostfix(Resources res) {
String result = null;
switch (res.getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
result = "ldpi";
break;
case DisplayMetrics.DENSITY_MEDIUM:
result = "mdpi";
break;
case DisplayMetrics.DENSITY_HIGH:
result = "hdpi";
break;
case DisplayMetrics.DENSITY_XHIGH:
result = "xhdpi";
break;
case DisplayMetrics.DENSITY_XXHIGH:
result = "xxhdpi";
break;
case DisplayMetrics.DENSITY_XXXHIGH:
result = "xxxhdpi";
break;
}
return result;
}
}
動態(tài)9patch解決方案
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來芋忿,“玉大人炸客,你說我怎么就攤上這事「旮郑” “怎么了痹仙?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長逆趣。 經(jīng)常有香客問我蝶溶,道長,這世上最難降的妖魔是什么宣渗? 我笑而不...
- 正文 為了忘掉前任抖所,我火速辦了婚禮,結果婚禮上痕囱,老公的妹妹穿的比我還像新娘田轧。我一直安慰自己,他們只是感情好鞍恢,可當我...
- 文/花漫 我一把揭開白布傻粘。 她就那樣靜靜地躺著,像睡著了一般帮掉。 火紅的嫁衣襯著肌膚如雪弦悉。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼良拼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了充边?” 一聲冷哼從身側響起庸推,我...
- 正文 年R本政府宣布墓拜,位于F島的核電站港柜,受9級特大地震影響,放射性物質發(fā)生泄漏咳榜。R本人自食惡果不足惜夏醉,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望涌韩。 院中可真熱鬧畔柔,春花似錦、人聲如沸臣樱。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽雇毫。三九已至玄捕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間棚放,已是汗流浹背枚粘。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 原文鏈接 前言 在本地開發(fā)中庄涡,有時候我們經(jīng)常需要模擬https環(huán)境量承,比如PWA應用要求必須使用https訪問。在傳...
- 應用場景 用流動關系圖來映射品牌之間的有效換機數(shù)量拿穴,從而幫助運營對手機品牌的行情做分析和預測。 圖形說明一期:圖形...
- 介紹 在Castle Windsor Ioc 一個接口多個實現(xiàn)解決方案中忧风,介紹了三種解決一個接口多個實現(xiàn)的方案默色,在...
- 原文地址:https://toutiao.io/posts/nmflsd/preview[https://tout...
- 注意: 本文檔中描述的修改插件的部分(步驟8~11)并非最佳方法,請直接使用以下git庫安裝Jㄍ取M仍住! 描述 Cord...