Example usage for android.graphics.drawable BitmapDrawable BitmapDrawable

List of usage examples for android.graphics.drawable BitmapDrawable BitmapDrawable

Introduction

In this page you can find the example usage for android.graphics.drawable BitmapDrawable BitmapDrawable.

Prototype

private BitmapDrawable(BitmapState state, Resources res) 

Source Link

Usage

From source file:com.chaotix.applerefurbwatcher.helpers.ImageDownloader.java

private void setButtonBitmap(Button imageView, Bitmap bitmap) {
    Drawable drawable = new BitmapDrawable(resources, bitmap);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    imageView.setCompoundDrawables(drawable, null, null, null);
}

From source file:com.android.volley.ui.NetworkImageView.java

@SuppressLint("NewApi")
private void setAnimateImageBitmap(final Bitmap bitmap, boolean fadeIn) {

    // If we're fading in and on HC MR1+
    if (fadeIn && Utils.hasHoneycombMR1()) {
        // Use ViewPropertyAnimator to run a simple fade in + fade out animation to update the
        // ImageView
        animate().scaleY(0.95f).scaleX(0.95f).alpha(0f)
                .setDuration(getDrawable() == null ? 0 : HALF_FADE_IN_TIME)
                .setListener(new AnimatorListenerAdapter() {
                    @Override//from w  w  w. j  a  v  a2  s.c o  m
                    public void onAnimationEnd(Animator animation) {
                        setImageBitmap(bitmap);
                        animate().alpha(1f).scaleY(1f).scaleX(1f).setDuration(HALF_FADE_IN_TIME)
                                .setListener(null);
                    }
                });
    } else if (fadeIn) {
        // Otherwise use a TransitionDrawable to fade in
        Drawable initialDrawable;
        if (getDrawable() != null) {
            initialDrawable = getDrawable();
        } else {
            initialDrawable = transparentDrawable;
        }

        BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
        // Use TransitionDrawable to fade in
        final TransitionDrawable td = new TransitionDrawable(
                new Drawable[] { initialDrawable, bitmapDrawable });
        setImageDrawable(td);
        td.startTransition(Utils.ANIMATION_FADE_IN_TIME);
    } else {
        // No fade in, just set bitmap directly
        setImageBitmap(bitmap);
    }
}

From source file:cl.ipp.katbag.fragment.SlidePageBookFragment.java

@SuppressWarnings("deprecation")
public void setPictureBackground(String type_world, int scaleFactor, long id_world) {
    // Get the dimensions of the View
    int targetW = backgroundView.getWidth();
    int targetH = backgroundView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    if (scaleFactor == -1) {
        if (photoW != 0 && targetW != 0 && photoH != 0 && targetH != 0)
            scaleFactor = Math.min(photoW / targetW, photoH / targetH);
        else//from www .  j  a  va 2 s.c  o m
            scaleFactor = 1;

        mainActivity.katbagHandler.updateWorld(id_world, type_world, mCurrentPhotoPath, scaleFactor);
    }

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();

        try {
            bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
        } catch (OutOfMemoryError e2) {
            e2.printStackTrace();
            // handle gracefully.
        }
    }

    BitmapDrawable background = new BitmapDrawable(mainActivity.context.getResources(), bitmap);

    backgroundView.setBackgroundDrawable(background);
}

From source file:com.hippo.android.animator.AnimatorsBase.java

static Animator crossFade(final View from, final View to, ViewGroup ancestor, final boolean toIsTop) {
    // Ensure views is laid out
    if (!ViewCompat.isLaidOut(from) || !ViewCompat.isLaidOut(to)) {
        Log.w(LOG_TAG, "From view and to view must be laid out before calling crossFade().");
        return null;
    }/*from   w ww .  j a  v a  2s  .c  o m*/

    // Get overlay
    final ViewOverlayCompat overlay = ViewOverlayCompat.from(ancestor);
    if (overlay == null) {
        Log.w(LOG_TAG, "The ancestor in crossFade() must be able to create a ViewOverlay.");
        return null;
    }

    // Get the location of from view
    if (!Utils.getLocationInAncestor(from, ancestor, TEMP_LOCATION)) {
        Log.w(LOG_TAG, "From view must be in ancestor in crossFade().");
        return null;
    }
    int fromX = TEMP_LOCATION[0];
    int fromY = TEMP_LOCATION[1];

    // Get the location of to view
    if (!Utils.getLocationInAncestor(to, ancestor, TEMP_LOCATION)) {
        Log.w(LOG_TAG, "From view must be in ancestor in crossFade().");
        return null;
    }
    int toX = TEMP_LOCATION[0];
    int toY = TEMP_LOCATION[1];

    // Get the screenshot of from view
    final Bitmap fromBitmap = Utils.screenshot(from);
    if (fromBitmap == null) {
        Log.w(LOG_TAG, "Can't screenshot from view in crossFade().");
        return null;
    }

    // Get the screenshot of to view
    final Bitmap toBitmap = Utils.screenshot(to);
    if (toBitmap == null) {
        Log.w(LOG_TAG, "Can't screenshot to view in crossFade().");
        fromBitmap.recycle();
        return null;
    }

    // Create start drawables
    final Drawable fromDrawable = new BitmapDrawable(from.getContext().getResources(), fromBitmap);
    int fromWidth = fromBitmap.getWidth();
    int fromHeight = fromBitmap.getHeight();
    fromDrawable.setBounds(fromX, fromY, fromX + fromWidth, fromY + fromHeight);

    int fromCenterX = fromX + fromWidth / 2;
    int fromCenterY = fromY + fromHeight / 2;

    // Create end drawable
    final Drawable toDrawable = new BitmapDrawable(to.getContext().getResources(), toBitmap);
    int toWidth = toBitmap.getWidth();
    int toHeight = toBitmap.getHeight();
    int toStartX = fromCenterX - toWidth / 2;
    int toStartY = fromCenterY - toHeight / 2;
    toDrawable.setBounds(toStartX, toStartY, toStartX + toWidth, toStartY + toHeight);

    int toCenterX = toX + toWidth / 2;
    int toCenterY = toY + toHeight / 2;

    List<Animator> set = new ArrayList<>(4);

    // Create alpha animators
    Animator fromAlpha = ObjectAnimator.ofInt(fromDrawable, DRAWABLE_ALPHA_PROPERTY, 255, 0);
    Animator toAlpha = ObjectAnimator.ofInt(toDrawable, DRAWABLE_ALPHA_PROPERTY, 0, 255);
    set.add(fromAlpha);
    set.add(toAlpha);

    // Create position animators
    if (fromCenterX != toCenterX || fromCenterY != toCenterY) {
        Path path = ACR_PATH_MOTION.getPath(fromCenterX, fromCenterY, toCenterX, toCenterY);
        Animator fromPosition = Animators.ofPointF(fromDrawable, DRAWABLE_POSITION_PROPERTY, path);
        Animator toPosition = Animators.ofPointF(toDrawable, DRAWABLE_POSITION_PROPERTY, path);
        set.add(fromPosition);
        set.add(toPosition);
    }

    Animator animator = Animators.playTogether(set);
    animator.addListener(new AnimatorListenerAdapter() {
        float fromAlpha;
        float toAlpha;

        @Override
        public void onAnimationStart(Animator animation) {
            // Add drawables to overlay
            if (toIsTop) {
                overlay.add(fromDrawable);
                overlay.add(toDrawable);
            } else {
                overlay.add(toDrawable);
                overlay.add(fromDrawable);
            }
            // Hide from view and to view
            fromAlpha = from.getAlpha();
            toAlpha = to.getAlpha();
            from.setAlpha(0.0f);
            to.setAlpha(0.0f);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            // Remove drawables from overlay
            overlay.remove(fromDrawable);
            overlay.remove(toDrawable);
            // Show from view and to view
            from.setAlpha(fromAlpha);
            to.setAlpha(toAlpha);
            // Recycle bitmaps
            fromBitmap.recycle();
            toBitmap.recycle();
        }
    });

    return animator;
}

From source file:com.bayapps.android.robophish.ui.tv.TvPlaybackFragment.java

private void updateAlbumArt(Uri artUri) {
    AlbumArtCache.getInstance().fetch(artUri.toString(), new AlbumArtCache.FetchListener() {
        @Override/*w  w  w .j  a v a  2  s .  c o  m*/
        public void onFetched(String artUrl, Bitmap bitmap, Bitmap icon) {
            if (bitmap != null) {
                Drawable artDrawable = new BitmapDrawable(TvPlaybackFragment.this.getResources(), bitmap);
                Drawable bgDrawable = new BitmapDrawable(TvPlaybackFragment.this.getResources(), bitmap);
                mPlaybackControlsRow.setImageDrawable(artDrawable);
                mBackgroundManager.setDrawable(bgDrawable);
                mRowsAdapter.notifyArrayItemRangeChanged(mRowsAdapter.indexOf(mPlaybackControlsRow), 1);
            }
        }
    });
}

From source file:org.yaoha.YaohaActivity.java

public Drawable readDrawableFromSD(String fileName) {
    File file = new File(getExternalFilesDir(null), fileName);
    Drawable d;//from  w  w  w . j av  a 2 s .c o  m
    if (file.exists()) {
        Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        d = new BitmapDrawable(getResources(), myBitmap);
    } else {
        Toast.makeText(this, "Could not find File, loading default picture.", Toast.LENGTH_LONG).show();
        d = getResources().getDrawable(R.drawable.placeholder_logo);
    }
    return d;
}

From source file:com.hunch.ImageManager.java

protected Drawable getCachedTopicImage(final Context context, final URL url) {
    // is the drawable in the level one cache (in memory)
    Drawable drawable = getDrawableFromMemoryCache(context, url);
    if (drawable != null) {
        // we got lucky, it was in memory
        return drawable;
    }/*  w w  w.ja  va  2  s  .co  m*/

    // otherwise check the internal file cache
    FileInputStream imageFileStream = getImageFileStreamFromCache(context, url);

    // if the stream doesn't exist, we don't have the image cached
    if (imageFileStream == null) {
        return null;
    }

    Bitmap bitmap = BitmapFactory.decodeStream(imageFileStream);

    return new BitmapDrawable(context.getResources(), bitmap);
}

From source file:com.github.topbottomsnackbar.TBSnackbar.java

private Drawable fitDrawable(Drawable drawable, int sizePx) {
    if (drawable.getIntrinsicWidth() != sizePx || drawable.getIntrinsicHeight() != sizePx) {

        if (drawable instanceof BitmapDrawable) {

            drawable = new BitmapDrawable(mContext.getResources(),
                    Bitmap.createScaledBitmap(getBitmap(drawable), sizePx, sizePx, true));
        }/* ww  w  . j a  v a  2  s  .co  m*/
    }
    drawable.setBounds(0, 0, sizePx, sizePx);

    return drawable;
}

From source file:com.atinternet.tracker.Tool.java

/**
 * Resize an image//from   w w w .  jav  a 2 s .  c o  m
 *
 * @param imageID int
 * @param context Context
 * @param width   int
 * @param height  int
 * @return Drawable
 */
static Drawable getResizedImage(int imageID, Context context, int width, int height) {
    Bitmap b = BitmapFactory.decodeResource(context.getResources(), imageID);
    Bitmap bitmapResized = Bitmap.createScaledBitmap(b, width, height, false);
    return new BitmapDrawable(context.getResources(), bitmapResized);
}

From source file:android.com.example.contactslist.util.ImageLoader.java

/**
 * Called when the processing is complete and the final bitmap should be set on the ImageView.
 *
 * @param imageView The ImageView to set the bitmap to.
 * @param bitmap The new bitmap to set./*from   www  .j  a v  a  2  s .c  o m*/
 */
private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
    if (mFadeInBitmap) {
        // Transition drawable to fade from loading bitmap to final bitmap
        final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
                new ColorDrawable(android.R.color.transparent), new BitmapDrawable(mResources, bitmap) });
        imageView.setBackgroundDrawable(imageView.getDrawable());
        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    } else {
        imageView.setImageBitmap(bitmap);
    }
}