Android Bitmap - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android Bitmap class.

This code example shows how to use the following methods:getHeight, getWidth
	 * and the GitHub Android code. 
	 * https://github.com/github/android 
	 */ 
	 
	public static Bitmap roundCorners(Bitmap source, final float radius) {
        int width = source.getWidth();
        int height = source.getHeight();
 
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(WHITE);
 
        Bitmap clipped = Bitmap.createBitmap(width, height, ARGB_8888);
        Canvas canvas = new Canvas(clipped);
        canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(DST_IN));
         
        Bitmap rounded = Bitmap.createBitmap(width, height, ARGB_8888);
        canvas = new Canvas(rounded);
        canvas.drawBitmap(source, 0, 0, null);
10
CodeRank
This code example shows how to use the following methods:getConfig, getHeight, getWidth
	 * new Bitmap using default return statement + original texture coordinates 
	 * are stored into RectF. 
	 */ 
	private Bitmap getTexture(Bitmap bitmap, RectF textureRect) {
		// Bitmap original size. 
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		// Bitmap size expanded to next power of two. This is done due to 
		// the requirement on many devices, texture width and height should 
		// be power of two. 
		int newW = getNextHighestPO2(w);
		int newH = getNextHighestPO2(h);
 
		// TODO: Is there another way to create a bigger Bitmap and copy 
		// original Bitmap to it more efficiently? Immutable bitmap anyone? 
		Bitmap bitmapTex = Bitmap.createBitmap(newW, newH, bitmap.getConfig());
		Canvas c = new Canvas(bitmapTex);
		c.drawBitmap(bitmap, 0, 0, null);
 
		// Calculate final texture coordinates. 
		float texX = (float) w / newW;
9
CodeRank
This code example shows how to use the following methods:getHeight, getWidth
    @Override 
    public void draw(Canvas canvas) {
        if (mNeedRebuild) {
 
            if (mMapPin == null) {
                mMapPin = Bitmap.createBitmap(mBase.getWidth(), mBase.getHeight(), Bitmap.Config.ARGB_8888);
            } 
            Canvas c = new Canvas(mMapPin);
            c.drawRect(0, 0, mMapPin.getWidth(), mMapPin.getHeight(), sClearerPaint);
 
            // 1 - Draw the base 
            c.drawBitmap(mBase, 0, 0, null);
            // 2 - Draw the pin on top of it 
            mPaint.setColorFilter(new LightingColorFilter(Color.BLACK, mCurrentPinColor));
            c.drawBitmap(mPin, 0, 0, mPaint);
            // 3 - Draw the dot on top of everything 
            mPaint.setColorFilter(new LightingColorFilter(Color.BLACK, mCurrentDotColor));
            c.drawBitmap(mDot, 0, 0, mPaint);
 
            mNeedRebuild = false;
10
CodeRank
This code example shows how to use the following methods:copyPixelsToBuffer, getHeight, getRowBytes, getWidth
        } 
        return bmp;
    } 
     
    private static void compareImages(Bitmap bmp1, Bitmap bmp2) {
        assertTrue(bmp1.getHeight() == bmp2.getHeight());
        assertTrue(bmp1.getWidth() == bmp1.getWidth());
        ByteBuffer buffer1 = ByteBuffer.allocate(bmp1.getHeight() * bmp1.getRowBytes());
        bmp1.copyPixelsToBuffer(buffer1);
 
        ByteBuffer buffer2 = ByteBuffer.allocate(bmp2.getHeight() * bmp2.getRowBytes());
        bmp2.copyPixelsToBuffer(buffer2);
 
        assertTrue(Arrays.equals(buffer1.array(), buffer2.array()));
    } 
 
    private Context safeGetContext() {
        for (;;) { 
            if ((getContext() != null) && (getContext().getApplicationContext() != null)) {
                return getContext();
9
CodeRank
This code example shows how to use the following methods:getHeight, getRowBytes
		super(sizeLimit);
	} 
 
	@Override 
	public boolean put(String key, Bitmap value) {
		if (super.put(key, value)) {
			valueSizes.put(value, getSize(value));
			return true; 
		} else { 
			return false; 
		} 
	} 
 
	@Override 
	public void remove(String key) {
		Bitmap value = super.get(key);
		if (value != null) {
			valueSizes.remove(value);
		} 
		super.remove(key);

9
CodeRank
This code example shows how to use the following methods:getHeight, getWidth, recycle
     * @param source 
     * @param radius 
     * @return rounded corner bitmap 
     */ 
    public static Bitmap roundCorners(final Bitmap source, final float radius) {
        int width = source.getWidth();
        int height = source.getHeight();
 
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(WHITE);
 
        Bitmap clipped = Bitmap.createBitmap(width, height, ARGB_8888);
        Canvas canvas = new Canvas(clipped);
        canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius,
                paint);
        paint.setXfermode(new PorterDuffXfermode(DST_IN));
 
        Bitmap rounded = Bitmap.createBitmap(width, height, ARGB_8888);
        canvas = new Canvas(rounded);

9
CodeRank
This code example shows how to use the following methods:getConfig, getHeight, getWidth
			try { 
				// As pointed out by kbielenberg, Bitmap.sameAs() was only added in level 12/Android 3.1/Honeycomb MR1 
				Method sameAs = Bitmap.class.getMethod("sameAs", Bitmap.class);
				return (Boolean)sameAs.invoke( expectedBitmap, actualBitmap );
			} catch (Exception e) {
				if( expectedBitmap.getWidth() != actualBitmap.getWidth() ) { return false; }
				if( expectedBitmap.getHeight() != actualBitmap.getHeight() ) { return false; }
				if( expectedBitmap.getConfig() != actualBitmap.getConfig() ) { return false; }
				boolean bitmapsEqual = expectedBitmap.equals(actualBitmap);
				if( !bitmapsEqual ) {
					Log.i(TAG, "Bitmaps are not equal");
				} 
				boolean drawablesEqual = expected.equals(actual);
				if( !drawablesEqual ) {
					Log.i(TAG, "Drawables are not equal");
				} 
				return bitmapsEqual && drawablesEqual;
			} 
		} 
		return false; 
9
CodeRank
This code example shows how to use the following methods:getConfig
	// =========================================================== 
	// Methods 
	// =========================================================== 
 
	protected Bitmap swapColor(final Bitmap pBitmap) {
		final Config config = pBitmap.getConfig();
		switch (config) {
			case ARGB_8888:
				return ColorSwapBitmapTextureAtlasSource.swapColorARGB_8888(pBitmap, this.mColorKeyColorARGBPackedInt, this.mTolerance, this.mColorSwapColorARGBPackedInt);
			default: 
				Debug.w("Unexpected " + Bitmap.Config.class.getSimpleName() + ": '" + config + "'.");
				return pBitmap;
		} 
	} 
 
	protected static final Bitmap swapColorARGB_8888(final Bitmap pBitmap, final int pColorKeyColorARGBPackedInt, final int pTolerance, final int pColorSwapColorARGBPackedInt) {
		final int[] pixelsARGB_8888 = GLHelper.getPixelsARGB_8888(pBitmap);
		pBitmap.recycle();
 
		final int keyRed = (pColorKeyColorARGBPackedInt >> Color.ARGB_PACKED_RED_SHIFT) & 0xFF;
9
CodeRank
This code example shows how to use the following methods:recycle
This code example shows how to use the following methods:createScaledBitmap, getHeight, getWidth
	 * @return Bitmap A new Bitmap object of the same bitmap after scaling. 
	 */ 
	public Bitmap scaleBitmap(Bitmap bitmap) {
		int newWidth = this.targetWidth;
		int newHeight = this.targetHeight;
		int origWidth = bitmap.getWidth();
		int origHeight = bitmap.getHeight();
 
		// If no new width or height were specified return the original bitmap 
		if (newWidth <= 0 && newHeight <= 0) {
			return bitmap;
		} 
		// Only the width was specified 
		else if (newWidth > 0 && newHeight <= 0) {
			newHeight = (newWidth * origHeight) / origWidth;
		} 
		// only the height was specified 
		else if (newWidth <= 0 && newHeight > 0) {
			newWidth = (newHeight * origWidth) / origHeight;
		} 

8
CodeRank

Related Android Bitmap Questions & Answers:

Other Android Bitmap Examples

17:	    ImageView button = (ImageView) findViewById(R.id.image);
18:	    InputStream resource = getResources().openRawResource(R.drawable.dog);
19:	    Bitmap bitmap = BitmapFactory.decodeStream(resource);
20:	    button.setBackground(new MyRoundCornerDrawable(bitmap));
21:	  }

Read More

14:	public class ImagePickActivity extends Activity {
15:	  private static final int REQUEST_CODE = 1;
16:	  private Bitmap bitmap;
17:	  private ImageView imageView;
18:

Read More

7:	public class MemoryCache {
8:	    private Map&lt;String, SoftReference&lt;Bitmap&gt;&gt; cache=Collections.synchronizedMap(new HashMap&lt;String, SoftReference&lt;Bitmap&gt;&gt;());
9:	
10:	    public Bitmap get(String id){
11:	        if(!cache.containsKey(id))
-
7:	public class MemoryCache {
8:	    private Map&lt;String, SoftReference&lt;Bitmap&gt;&gt; cache=Collections.synchronizedMap(new HashMap&lt;String, SoftReference&lt;Bitmap&gt;&gt;());
9:	
10:	    public Bitmap get(String id){
11:	        if(!cache.containsKey(id))
-
9:	
10:	    public Bitmap get(String id){
11:	        if(!cache.containsKey(id))
12:	            return null;
13:	        SoftReference&lt;Bitmap&gt; ref=cache.get(id);
-
12:	            return null;
13:	        SoftReference&lt;Bitmap&gt; ref=cache.get(id);
14:	        return ref.get();
15:	    }
16:	
-
16:	
17:	    public void put(String id, Bitmap bitmap){
18:	        cache.put(id, new SoftReference&lt;Bitmap&gt;(bitmap));
19:	    }
20:	
-
17:	    public void put(String id, Bitmap bitmap){
18:	        cache.put(id, new SoftReference&lt;Bitmap&gt;(bitmap));
19:	    }
20:	
21:	    public void clear() {

Read More

23:	
24:		@Override
25:		protected int sizeOf(String key, Bitmap value) {
26:			return value.getRowBytes() * value.getHeight() / 1024;
27:		}
-
28:	
29:		@Override
30:		public Bitmap getBitmap(String url) {
31:			return get(url);
32:		}
-
33:	
34:		@Override
35:		public void putBitmap(String url, Bitmap bitmap) {
36:			put(url, bitmap);
37:		}

Read More

9:	    private Map<String, SoftReference<Bitmap>> cache=Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());
10:	 
11:	    public Bitmap get(String id){
12:	        if(!cache.containsKey(id))
13:	            return null;
-
16:	    }
17:	 
18:	    public void put(String id, Bitmap bitmap){
19:	        cache.put(id, new SoftReference<Bitmap>(bitmap));
20:	    }

Read More

See Code Examples for Android Bitmap Methods: