Java Code Examples for android.graphics.Bitmap#getNinePatchChunk()

The following examples show how to use android.graphics.Bitmap#getNinePatchChunk() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XulNinePatchDrawable.java    From starcor.xul with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static XulDrawable build(Bitmap bmp, String url, String imageKey) {
	if (bmp == null) {
		return null;
	}

	byte[] ninePatchChunk = bmp.getNinePatchChunk();
	XulNinePatchDrawable drawable;
	if (ninePatchChunk != null) {
		drawable = new XulNinePatchDrawable();
		if (!drawable.attach(bmp, ninePatchChunk)) {
			return null;
		}
	} else if (bmp.getWidth() > 3 && bmp.getHeight() > 3) {
		drawable = new XulNinePatchDrawable();
		drawable.attach(bmp);
	} else {
		return null;
	}
	drawable._url = url;
	drawable._key = imageKey;
	return drawable;
}
 
Example 2
Source File: BackGroudSeletor.java    From FloatBall with Apache License 2.0 6 votes vote down vote up
/**
 * 获取asset下面的.9 png
 *
 * @param imagename 图片名
 * @param context   上下文对象
 */
public static NinePatchDrawable get9png(String imagename, Context context) {
    Bitmap toast_bitmap;
    try {
        toast_bitmap = BitmapFactory.decodeStream(context.getAssets().open("image/" + imagename + ".9.png"));
        byte[] temp = toast_bitmap.getNinePatchChunk();
        boolean is_nine = NinePatch.isNinePatchChunk(temp);
        if (is_nine) {
            NinePatchDrawable nine_draw = new NinePatchDrawable(context.getResources(), toast_bitmap, temp, new Rect(), null);
            return nine_draw;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: NinePatchChunkTest.java    From NinePatchChunk with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "drawablesToTest")
public void testImageLoadingFromAssets(@DrawableRes Integer drawableResId, String drawableName) {
	Context context = AndroidTestNGSupport.getContext();
	assertThat("Context is null", context, notNullValue());

	Bitmap bitmap = getBitmap(drawableResId, drawableName, context);
	InputStream is = getInputStreamFromAssets(drawableName, context);

	ImageLoadingResult loadingResult = NinePatchChunk.createChunkFromRawBitmap(context, is);
	NinePatchChunk chunk = loadingResult.chunk;
	Bitmap testBitmap = loadingResult.bitmap;
	assertThat("Loaded test bitmap can't be considered as raw NinePatch bitmap", chunk, notNullValue());
	assertThat("Test end system bitmaps have different density.", bitmap.getDensity(), equalTo(testBitmap.getDensity()));
	assertThat("Test and system bitmaps have different sizes",
			new SizePair(bitmap.getWidth(), bitmap.getHeight()),
			equalTo(new SizePair(testBitmap.getWidth(), testBitmap.getHeight())));
	assertThat("Images are not fully identical", testBitmap, sameBitmap(bitmap));
	byte[] expectedChunk = bitmap.getNinePatchChunk();
	byte[] createdChunk = chunk.toBytes();
	assertThat("Created chunk different from expected", createdChunk, sameArray(expectedChunk, 4, 8, 28));//See NinePatchChunk implementation why this bytes are skipped.
}
 
Example 4
Source File: AuthAgent.java    From letv with Apache License 2.0 5 votes vote down vote up
private Drawable a(String str, Context context) {
    Drawable createFromStream;
    IOException e;
    try {
        InputStream open = context.getApplicationContext().getAssets().open(str);
        if (open == null) {
            return null;
        }
        if (str.endsWith(".9.png")) {
            Bitmap decodeStream = BitmapFactory.decodeStream(open);
            if (decodeStream == null) {
                return null;
            }
            byte[] ninePatchChunk = decodeStream.getNinePatchChunk();
            NinePatch.isNinePatchChunk(ninePatchChunk);
            return new NinePatchDrawable(decodeStream, ninePatchChunk, new Rect(), null);
        }
        createFromStream = Drawable.createFromStream(open, str);
        try {
            open.close();
            return createFromStream;
        } catch (IOException e2) {
            e = e2;
            e.printStackTrace();
            return createFromStream;
        }
    } catch (IOException e3) {
        IOException iOException = e3;
        createFromStream = null;
        e = iOException;
        e.printStackTrace();
        return createFromStream;
    }
}
 
Example 5
Source File: TaskGuide.java    From letv with Apache License 2.0 5 votes vote down vote up
private Drawable a(String str, Context context) {
    Drawable createFromStream;
    IOException e;
    try {
        InputStream open = context.getApplicationContext().getAssets().open(str);
        if (open == null) {
            return null;
        }
        if (str.endsWith(".9.png")) {
            Bitmap decodeStream = BitmapFactory.decodeStream(open);
            if (decodeStream == null) {
                return null;
            }
            byte[] ninePatchChunk = decodeStream.getNinePatchChunk();
            NinePatch.isNinePatchChunk(ninePatchChunk);
            return new NinePatchDrawable(decodeStream, ninePatchChunk, new Rect(), null);
        }
        createFromStream = Drawable.createFromStream(open, str);
        try {
            open.close();
            return createFromStream;
        } catch (IOException e2) {
            e = e2;
            e.printStackTrace();
            return createFromStream;
        }
    } catch (IOException e3) {
        IOException iOException = e3;
        createFromStream = null;
        e = iOException;
        e.printStackTrace();
        return createFromStream;
    }
}
 
Example 6
Source File: NinePatchData.java    From 365browser with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to decode 9-patch data from a {@link Bitmap}.
 * @param bitmap The {@link Bitmap} to check.
 * @return       An instance of {@link NinePatchData} representing the 9-patch information
 *               encoded in {@code bitmap} or {@code null} if the {@link Bitmap} wasn't a
 *               9-patch.
 */
public static NinePatchData create(Bitmap bitmap) {
    if (bitmap == null) return null;

    try {
        byte[] chunk = bitmap.getNinePatchChunk();
        if (chunk == null || !NinePatch.isNinePatchChunk(chunk)) return null;

        ByteBuffer buffer = ByteBuffer.wrap(chunk).order(ByteOrder.nativeOrder());

        // int8_t wasDeserialized
        if (buffer.get() == 0) return null;

        // int8_t numXDivs
        int numDivX = buffer.get();
        if (numDivX == 0 || (numDivX & 0x01) != 0) return null;

        // int8_t numYDivs
        int numDivY = buffer.get();
        if (numDivY == 0 || (numDivY & 0x01) != 0) return null;

        // int8_t numColors
        buffer.get();

        // uint32_t xDivsOffset
        buffer.getInt();

        // uint32_t yDivsOffset
        buffer.getInt();

        Rect padding = new Rect();

        // uint32_t paddingLeft
        padding.left = buffer.getInt();

        // uint32_t paddingRight
        padding.right = buffer.getInt();

        // uint32_t paddingTop
        padding.top = buffer.getInt();

        // uint32_t paddingBottom
        padding.bottom = buffer.getInt();

        // uint32_t colorsOffset
        buffer.getInt();

        // uint32_t uint32_t uint32_t ...
        int[] divX = new int[numDivX];
        for (int i = 0; i < numDivX; i++) divX[i] = buffer.getInt();

        // uint32_t uint32_t uint32_t ...
        int[] divY = new int[numDivY];
        for (int i = 0; i < numDivY; i++) divY[i] = buffer.getInt();

        return new NinePatchData(bitmap.getWidth(), bitmap.getHeight(), padding, divX, divY);
    } catch (BufferUnderflowException ex) {
        return null;
    }
}
 
Example 7
Source File: Utils.java    From MusicPlayer with GNU General Public License v3.0 2 votes vote down vote up
/**
 * draw 9Path
 *
 * @param canvas Canvas
 * @param bmp 9path bitmap
 * @param rect 9path rect
 */
public static void drawNinePath(Canvas canvas, Bitmap bmp, Rect rect) {
    NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null);
    patch.draw(canvas, rect);
}
 
Example 8
Source File: RxSeekBar.java    From RxTools-master with Apache License 2.0 2 votes vote down vote up
/**
 * 绘制 9Path
 *
 * @param c
 * @param bmp
 * @param rect
 */
public void drawNinePath(Canvas c, Bitmap bmp, Rect rect) {
    NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null);
    patch.draw(c, rect);
}
 
Example 9
Source File: Utils.java    From RangeSeekBar with Apache License 2.0 2 votes vote down vote up
/**
 * draw 9Path
 *
 * @param canvas Canvas
 * @param bmp    9path bitmap
 * @param rect   9path rect
 */
public static void drawNinePath(Canvas canvas, Bitmap bmp, Rect rect) {
    NinePatch.isNinePatchChunk(bmp.getNinePatchChunk());
    NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null);
    patch.draw(canvas, rect);
}