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

The following examples show how to use android.graphics.Bitmap#getNinePatchChunk() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
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 Project: FloatBall   File: BackGroudSeletor.java    License: 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
@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 Project: letv   File: AuthAgent.java    License: 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 Project: letv   File: TaskGuide.java    License: 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 Project: 365browser   File: NinePatchData.java    License: 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 Project: MusicPlayer   File: Utils.java    License: 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 Project: RxTools-master   File: RxSeekBar.java    License: 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 Project: RangeSeekBar   File: Utils.java    License: 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);
}