Java Code Examples for android.graphics.drawable.BitmapDrawable#getBitmap()

The following examples show how to use android.graphics.drawable.BitmapDrawable#getBitmap() . 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: ContactsUtils14.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Bitmap getContactPhoto(Context ctxt, Uri uri, boolean hiRes, Integer defaultResource) {
    Bitmap img = null;
    InputStream s = Contacts.openContactPhotoInputStream(
            ctxt.getContentResolver(), uri, hiRes);
    img = BitmapFactory.decodeStream(s);

    if (img == null && defaultResource != null) {
        BitmapDrawable drawableBitmap = ((BitmapDrawable) ctxt.getResources().getDrawable(
                defaultResource));
        if (drawableBitmap != null) {
            img = drawableBitmap.getBitmap();
        }
    }
    return img;
}
 
Example 2
Source File: Util.java    From Jockey with Apache License 2.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    int width = Math.max(drawable.getIntrinsicWidth(), 1);
    int height = Math.max(drawable.getIntrinsicHeight(), 1);
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}
 
Example 3
Source File: ConvertUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * drawable转bitmap
 *
 * @param drawable drawable对象
 * @return bitmap
 */
public static Bitmap drawable2Bitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if(bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }
    Bitmap bitmap;
    if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}
 
Example 4
Source File: XposedApp.java    From EdXposedManager with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}
 
Example 5
Source File: WatchFaceUtil.java    From PixelWatchFace with GNU General Public License v3.0 6 votes vote down vote up
public static Bitmap drawableToBitmap(Drawable drawable) {
  Bitmap mBitmap = null;

  if (drawable instanceof BitmapDrawable) {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    if (bitmapDrawable.getBitmap() != null) {
      return bitmapDrawable.getBitmap();
    }
  }

  if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
    mBitmap = Bitmap.createBitmap(1, 1,
        Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
  } else {
    mBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
        Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(mBitmap);
  drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  drawable.draw(canvas);
  return mBitmap;
}
 
Example 6
Source File: WrappingUtils.java    From fresco with MIT License 5 votes vote down vote up
/**
 * Rounds the given drawable with a {@link RoundedBitmapDrawable} or {@link RoundedColorDrawable}.
 *
 * <p>If the given drawable is not a {@link BitmapDrawable} or a {@link ColorDrawable}, it is
 * returned without being rounded.
 *
 * @return the rounded drawable, or the original drawable if rounding didn't take place
 */
private static Drawable applyLeafRounding(
    Drawable drawable, RoundingParams roundingParams, Resources resources) {
  if (drawable instanceof BitmapDrawable) {
    final BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    RoundedBitmapDrawable roundedBitmapDrawable =
        new RoundedBitmapDrawable(
            resources, bitmapDrawable.getBitmap(), bitmapDrawable.getPaint());
    applyRoundingParams(roundedBitmapDrawable, roundingParams);
    return roundedBitmapDrawable;
  } else if (drawable instanceof NinePatchDrawable) {
    final NinePatchDrawable ninePatchDrawableDrawable = (NinePatchDrawable) drawable;
    RoundedNinePatchDrawable roundedNinePatchDrawable =
        new RoundedNinePatchDrawable(ninePatchDrawableDrawable);
    applyRoundingParams(roundedNinePatchDrawable, roundingParams);
    return roundedNinePatchDrawable;
  } else if (drawable instanceof ColorDrawable
      && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    RoundedColorDrawable roundedColorDrawable =
        RoundedColorDrawable.fromColorDrawable((ColorDrawable) drawable);
    applyRoundingParams(roundedColorDrawable, roundingParams);
    return roundedColorDrawable;
  } else {
    FLog.w(TAG, "Don't know how to round that drawable: %s", drawable);
  }
  return drawable;
}
 
Example 7
Source File: ViewImagePresenter.java    From guanggoo-android with Apache License 2.0 5 votes vote down vote up
@Override
public void saveImage(final PhotoView photoViewTemp) {
    if (photoViewTemp != null) {
        BitmapDrawable glideBitmapDrawable = (BitmapDrawable) photoViewTemp.getDrawable();
        if (glideBitmapDrawable == null) {
            return;
        }
        Bitmap bitmap = glideBitmapDrawable.getBitmap();
        if (bitmap == null) {
            return;
        }
        mView.startLoading();
        FileUtils.saveImage(photoViewTemp.getContext(), bitmap, new FileUtils.SaveResultCallback() {
            @Override
            public void onSavedSuccess() {
                photoViewTemp.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.stopLoading();
                        mView.onSaveImageSucceed();
                    }
                });
            }

            @Override
            public void onSavedFailed() {
                photoViewTemp.post(new Runnable() {
                    @Override
                    public void run() {
                        mView.stopLoading();
                        mView.onSaveImageFailed("保存失败");
                    }
                });
            }
        });
    }
}
 
Example 8
Source File: WatermarkBuilder.java    From AndroidWM with Apache License 2.0 5 votes vote down vote up
/**
 * load a bitmap as background image from a ImageView.
 *
 * @param imageView the {@link ImageView} we need to use.
 */
private void backgroundFromImageView(ImageView imageView) {
    imageView.invalidate();
    if (imageView.getDrawable() != null) {
        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
        if (resizeBackgroundImg) {
            backgroundImg = resizeBitmap(drawable.getBitmap(), MAX_IMAGE_SIZE);
        } else {
            backgroundImg = drawable.getBitmap();
        }
    }
}
 
Example 9
Source File: SimpleTagImageView.java    From SprintNBA with Apache License 2.0 5 votes vote down vote up
private Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        return bitmapDrawable.getBitmap();
    }
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}
 
Example 10
Source File: SPUtils.java    From MvpRxJavaRetrofitOkhttp with MIT License 5 votes vote down vote up
/**
 * 保存图片到SharedPreferences
 *
 * @param key
 * @param imageView
 */
public static void putImage(String key, ImageView imageView) {
    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    // 将Bitmap压缩成字节数组输出流
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, baos);
    // 利用Base64将我们的字节数组输出流转换成String
    byte[] byteArray = baos.toByteArray();
    String imgString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
    // 将String保存shareUtils
    SPUtils.put(key, imgString);
}
 
Example 11
Source File: TouchImageView.java    From Klyph with MIT License 5 votes vote down vote up
@Override
public void setImageDrawable(Drawable drawable)
{
	super.setImageDrawable(drawable);
	if (drawable instanceof PicassoDrawable)
	{
		PicassoDrawable pDrawable = (PicassoDrawable) drawable;
		BitmapDrawable bitmapDrawable = (BitmapDrawable) pDrawable.getImage();
		if (bitmapDrawable.getBitmap() != null)
		{
			bmWidth = bitmapDrawable.getBitmap().getWidth();
			bmHeight = bitmapDrawable.getBitmap().getHeight();
		}
	}
}
 
Example 12
Source File: ShowMaxImageView.java    From JianDan_OkHttp with Apache License 2.0 5 votes vote down vote up
private Bitmap drawableToBitamp(Drawable drawable) {

		if (drawable != null) {
			BitmapDrawable bd = (BitmapDrawable) drawable;
			return bd.getBitmap();
		} else {
			return null;
		}
	}
 
Example 13
Source File: CropView.java    From scissors with Apache License 2.0 5 votes vote down vote up
@Override
public void setImageDrawable(@Nullable Drawable drawable) {
    final Bitmap bitmap;
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        bitmap = bitmapDrawable.getBitmap();
    } else if (drawable != null) {
        bitmap = Utils.asBitmap(drawable, getWidth(), getHeight());
    } else {
        bitmap = null;
    }

    setImageBitmap(bitmap);
}
 
Example 14
Source File: ImageUtils_Deprecated.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static Bitmap drawableToBitmapOriginals(Drawable drawable) {

        if (null == drawable) {
            return null;
        }
        BitmapDrawable bitDw = ((BitmapDrawable) drawable);
        Bitmap bitmap = bitDw.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
        System.out.println("........length......" + imageInByte);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
        return bitmap;
    }
 
Example 15
Source File: FrameAnimDrawable.java    From AndroidUiKit with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
    if (mResources != null) {
        BitmapDrawable drawable = (BitmapDrawable) mResources.getDrawable(RES_IDS[resIndex % RES_IDS.length]);
        Bitmap bitmap = drawable.getBitmap();
        canvas.drawBitmap(bitmap, 0, 0, mPaint);
        System.out.println("FrameAnimDrawable  draw resIndex " + resIndex
                + " ;\n bitmap =" + bitmap + " canvas.width" + canvas.getWidth()
                + " height" + canvas.getHeight()
                + "w" + drawable.getIntrinsicWidth() + "h" + drawable.getIntrinsicHeight());
    }
}
 
Example 16
Source File: ImageUtils_Deprecated.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static Bitmap drawableToBitmapOriginals(Drawable drawable) {

        if (null == drawable) {
            return null;
        }
        BitmapDrawable bitDw = ((BitmapDrawable) drawable);
        Bitmap bitmap = bitDw.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
        System.out.println("........length......" + imageInByte);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
        return bitmap;
    }
 
Example 17
Source File: DefaultImageLoadHandler.java    From cube-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public void onLoadFinish(ImageTask imageTask, CubeImageView imageView, BitmapDrawable drawable) {

    if (imageView == null) {
        return;
    }

    Drawable d = drawable;
    if (drawable != null) {

        if (mResizeImageViewAfterLoad) {
            int w = drawable.getBitmap().getWidth();
            int h = drawable.getBitmap().getHeight();

            if (w > 0 && h > 0) {

                ViewGroup.LayoutParams lyp = imageView.getLayoutParams();
                if (lyp != null) {
                    lyp.width = w;
                    lyp.height = h;
                    imageView.setLayoutParams(lyp);
                }
            }
        }

        // RoundedDrawable will not recycle automatically when API level is lower than 11
        if ((mDisplayTag & DISPLAY_ROUNDED) == DISPLAY_ROUNDED && Version.hasHoneycomb()) {
            d = new RoundedDrawable(drawable.getBitmap(), mCornerRadius);
        }
        if ((mDisplayTag & DISPLAY_FADE_IN) == DISPLAY_FADE_IN) {
            int loadingColor = android.R.color.transparent;
            if (mLoadingColor != -1 && (mDisplayTag & DISPLAY_ROUNDED) != DISPLAY_ROUNDED) {
                loadingColor = mLoadingColor;
            }
            final TransitionDrawable td = new TransitionDrawable(new Drawable[]{new ColorDrawable(loadingColor), d});
            imageView.setImageDrawable(td);
            td.startTransition(200);
        } else {

            if (DEBUG) {
                Drawable oldDrawable = imageView.getDrawable();
                int w = 0, h = 0;
                if (oldDrawable != null) {
                    w = oldDrawable.getIntrinsicWidth();
                    h = oldDrawable.getIntrinsicHeight();
                }
                CLog.d(LOG_TAG, MSG_LOAD_FINISH,
                        imageTask, imageView, w, h, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            }
            imageView.setImageDrawable(drawable);
        }
    }
}
 
Example 18
Source File: BitmapUtil.java    From NonViewUtils with Apache License 2.0 4 votes vote down vote up
/**
 * Drawable转Bitmap
 */
public static Bitmap drawable2Bitmap(Drawable drawable) {
    BitmapDrawable bd = (BitmapDrawable) drawable;
    return bd.getBitmap();
}
 
Example 19
Source File: MediaNotificationManager.java    From 365browser with Apache License 2.0 4 votes vote down vote up
private Bitmap drawableToBitmap(Drawable drawable) {
    if (!(drawable instanceof BitmapDrawable)) return null;

    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
    return bitmapDrawable.getBitmap();
}
 
Example 20
Source File: CircularImageView.java    From EasyAbout with MIT License 4 votes vote down vote up
private void loadBitmap() {
    BitmapDrawable bitmapDrawable = (BitmapDrawable) getDrawable();
    if (bitmapDrawable != null)
        image = bitmapDrawable.getBitmap();
}