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;
}
}
動(dòng)態(tài)9patch解決方案
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 原文鏈接 前言 在本地開(kāi)發(fā)中,有時(shí)候我們經(jīng)常需要模擬https環(huán)境,比如PWA應(yīng)用要求必須使用https訪問(wèn)。在傳...
- 應(yīng)用場(chǎng)景 用流動(dòng)關(guān)系圖來(lái)映射品牌之間的有效換機(jī)數(shù)量,從而幫助運(yùn)營(yíng)對(duì)手機(jī)品牌的行情做分析和預(yù)測(cè)。 圖形說(shuō)明一期:圖形...
- 介紹 在Castle Windsor Ioc 一個(gè)接口多個(gè)實(shí)現(xiàn)解決方案中,介紹了三種解決一個(gè)接口多個(gè)實(shí)現(xiàn)的方案,在...
- 原文地址:https://toutiao.io/posts/nmflsd/preview[https://tout...
- 注意: 本文檔中描述的修改插件的部分(步驟8~11)并非最佳方法,請(qǐng)直接使用以下git庫(kù)安裝?。?! 描述 Cord...