Example usage for android.graphics.drawable BitmapDrawable setBounds

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

Introduction

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

Prototype

public void setBounds(int left, int top, int right, int bottom) 

Source Link

Document

Specify a bounding rectangle for the Drawable.

Usage

From source file:Main.java

public static Drawable toGreyDrawable(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }//  w  w  w. j a  v  a 2  s.c  om
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

public static Drawable toGrey(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }/*from w w  w  .jav a  2s.  com*/
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageScale(Canvas canvas, Drawable image, double Angle, int x, int y, double scale) {

    if (scale == 0.0)
        return 0;

    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);
    float newHeight = (int) Math.round((float) image.getIntrinsicHeight() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);//  ww w . ja v  a 2 s  . c  o  m

    return bmd.getIntrinsicWidth();

}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageTargetHeight(Canvas canvas, Drawable image, double Angle, int x, int y,
        int newHeight) {

    float scale = (float) newHeight / (float) image.getIntrinsicHeight();
    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);/*from  www  .ja v  a2 s  .  c  o m*/

    return bmd.getIntrinsicWidth();

}

From source file:com.eutectoid.dosomething.usersettings.UserSettingsFragment.java

private void processImageResponse(String id, ImageResponse response) {
    if (response != null) {
        Bitmap bitmap = response.getBitmap();
        if (bitmap != null) {
            BitmapDrawable drawable = new BitmapDrawable(UserSettingsFragment.this.getResources(), bitmap);
            drawable.setBounds(0, 0,
                    getResources().getDimensionPixelSize(R.dimen.usersettings_fragment_profile_picture_width),
                    getResources().getDimensionPixelSize(R.dimen.usersettings_fragment_profile_picture_height));
            userProfilePic = drawable;/*from   w  w w.  ja  v  a2 s .c om*/
            userProfilePicID = id;
            connectedStateLabel.setCompoundDrawables(null, drawable, null, null);
            connectedStateLabel.setTag(response.getRequest().getImageUri());
        }
    }
}

From source file:org.jitsi.android.gui.util.DrawableCache.java

/**
 * Gets cached <tt>BitmapDrawable</tt> for given <tt>resId</tt>. If it
 * doesn't exist in the cache it will be loaded and stored for later use.
 *
 * @param resId bitmap drawable resource id(it must be bitmap resource)
 *
 * @return <tt>BitmapDrawable</tt> for given <tt>resId</tt>
 *
 * @throws Resources.NotFoundException if there's no bitmap for given
 *                                     <tt>resId</tt>
 *///from   w ww .j  a  v  a  2  s. c om
public BitmapDrawable getBitmapFromMemCache(Integer resId) throws Resources.NotFoundException {
    String key = "res:" + resId;
    // Check for cached bitmap
    BitmapDrawable img = cache.get(key);
    // Eventually loads the bitmap
    if (img == null) {
        // Load and store the bitmap
        Resources res = JitsiApplication.getAppResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, resId);
        img = new BitmapDrawable(res, bmp);
        img.setBounds(0, 0, img.getIntrinsicWidth(), img.getIntrinsicHeight());
        cache.put(key, img);
    }
    return cache.get(key);
}

From source file:com.ruesga.rview.misc.PicassoHelper.java

private static void loadWithFallbackUrls(final Context context, final Picasso picasso, final Object into,
        final Drawable placeholder, final List<String> urls) {
    final String nextUrl;
    synchronized (urls) {
        nextUrl = urls.isEmpty() ? null : urls.get(0);
    }//from w ww .  j  a  v a  2s  .  c  o  m

    final Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            sTargets.remove(this);

            synchronized (urls) {
                urls.clear();
                urls.add(nextUrl);
            }

            BitmapDrawable dw = new BitmapDrawable(context.getResources(), bitmap);
            drawInto(dw);
        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            sTargets.remove(this);

            // Next url
            synchronized (urls) {
                if (urls.contains(nextUrl)) {
                    urls.remove(nextUrl);
                }
            }
            loadWithFallbackUrls(context, picasso, into, placeholder, urls);
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {
            drawInto(drawable);
        }

        private void drawInto(Drawable dw) {
            if (into instanceof ImageView) {
                ((ImageView) into).setImageDrawable(dw);

            } else if (into instanceof MenuItem) {
                // Resize to fit into menu icon
                int size = context.getResources()
                        .getDimensionPixelSize(com.ruesga.rview.drawer.R.dimen.drawer_navigation_icon_size);
                Bitmap resized = Bitmap.createScaledBitmap(BitmapUtils.convertToBitmap(dw), size, size, false);
                Drawable icon = new BitmapDrawable(context.getResources(), resized);
                dw.setBounds(0, 0, size, size);
                ((MenuItem) into).setIcon(icon);
            }
        }
    };
    sTargets.add(target);

    if (nextUrl != null) {
        picasso.load(nextUrl).placeholder(placeholder).transform(new CircleTransform()).into(target);
    } else {
        // Placeholder
        target.onPrepareLoad(placeholder);
    }
}

From source file:com.github.mobile.util.HttpImageGetter.java

@Override
public Drawable getDrawable(String source) {
    Bitmap bitmap = null;/*from   ww w  .j a v  a2  s  .  com*/

    if (!destroyed) {
        File output = null;
        InputStream is = null;
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) new URL(source).openConnection();
            is = connection.getInputStream();
            if (is != null) {
                String mime = connection.getContentType();
                if (mime == null) {
                    mime = URLConnection.guessContentTypeFromName(source);
                }
                if (mime == null) {
                    mime = URLConnection.guessContentTypeFromStream(is);
                }
                if (mime != null && mime.startsWith("image/svg")) {
                    bitmap = ImageUtils.renderSvgToBitmap(context.getResources(), is, width, height);
                } else {
                    boolean isGif = mime != null && mime.startsWith("image/gif");
                    if (!isGif || canLoadGif()) {
                        output = File.createTempFile("image", ".tmp", dir);
                        if (FileUtils.save(output, is)) {
                            if (isGif) {
                                GifDrawable d = new GifDrawable(output);
                                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                                return d;
                            } else {
                                bitmap = ImageUtils.getBitmap(output, width, height);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            // fall through to showing the loading bitmap
        } finally {
            if (output != null) {
                output.delete();
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // ignored
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    synchronized (this) {
        if (destroyed && bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        } else if (bitmap != null) {
            loadedBitmaps.add(new WeakReference<>(bitmap));
        }
    }

    if (bitmap == null) {
        return loading.getDrawable(source);
    }

    BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    return drawable;
}

From source file:com.gdgdevfest.android.apps.devfestbcn.ui.tablet.TracksDropdownFragment.java

private void loadTrack(Cursor cursor, boolean triggerCallback) {
    final int trackColor;
    final Resources res = getResources();

    if (cursor != null) {
        trackColor = cursor.getInt(TracksAdapter.TracksQuery.TRACK_COLOR);

        mTrackId = cursor.getString(TracksAdapter.TracksQuery.TRACK_ID);

        String trackName = cursor.getString(TracksAdapter.TracksQuery.TRACK_NAME);

        mTitle.setText(trackName);//  w w  w . ja v a  2 s  .co m
        mAbstract.setText(cursor.getString(TracksAdapter.TracksQuery.TRACK_ABSTRACT));

        int iconResId = res.getIdentifier("track_" + ParserUtils.sanitizeId(trackName), "drawable",
                getActivity().getPackageName());
        if (iconResId != 0) {
            BitmapDrawable sourceIconDrawable = (BitmapDrawable) res.getDrawable(iconResId);
            Bitmap icon = Bitmap.createBitmap(sourceIconDrawable.getIntrinsicWidth(),
                    sourceIconDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(icon);
            sourceIconDrawable.setBounds(0, 0, icon.getWidth(), icon.getHeight());
            sourceIconDrawable.draw(canvas);
            BitmapDrawable iconDrawable = new BitmapDrawable(res, icon);
            mIcon.setImageDrawable(iconDrawable);
        } else {
            mIcon.setImageDrawable(null);
        }
    } else {
        trackColor = res.getColor(R.color.all_track_color);
        mTrackId = ScheduleContract.Tracks.ALL_TRACK_ID;

        mIcon.setImageDrawable(null);
        switch (mViewType) {
        case VIEW_TYPE_SESSIONS:
            mTitle.setText(R.string.all_tracks_sessions);
            mAbstract.setText(R.string.all_tracks_subtitle_sessions);
            break;
        case VIEW_TYPE_SANDBOX:
            mTitle.setText(R.string.all_tracks_sandbox);
            mAbstract.setText(R.string.all_tracks_subtitle_sandbox);
            break;
        }
    }

    mRootView.setBackgroundColor(trackColor);
    mCallbacks.onTrackNameAvailable(mTrackId, mTitle.getText().toString());

    if (triggerCallback) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mCallbacks.onTrackSelected(mTrackId);
            }
        });
    }
}

From source file:com.conferenceengineer.android.iosched.ui.tablet.TracksDropdownFragment.java

private void loadTrack(Cursor cursor, boolean triggerCallback) {
    final int trackColor;
    final Resources res = getResources();

    if (cursor != null) {
        trackColor = cursor.getInt(TracksAdapter.TracksQuery.TRACK_COLOR);

        mTrackId = cursor.getString(TracksAdapter.TracksQuery.TRACK_ID);

        String trackName = cursor.getString(TracksAdapter.TracksQuery.TRACK_NAME);

        mTitle.setText(trackName);/*from   w  ww.ja  v  a2  s  .  c o  m*/
        mAbstract.setText(cursor.getString(TracksAdapter.TracksQuery.TRACK_ABSTRACT));

        int iconResId = res.getIdentifier("track_" + ParserUtils.sanitizeId(trackName), "drawable",
                getActivity().getPackageName());
        if (iconResId != 0) {
            BitmapDrawable sourceIconDrawable = (BitmapDrawable) res.getDrawable(iconResId);
            Bitmap icon = Bitmap.createBitmap(sourceIconDrawable.getIntrinsicWidth(),
                    sourceIconDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(icon);
            sourceIconDrawable.setBounds(0, 0, icon.getWidth(), icon.getHeight());
            sourceIconDrawable.draw(canvas);
            BitmapDrawable iconDrawable = new BitmapDrawable(res, icon);
            mIcon.setImageDrawable(iconDrawable);
        } else {
            mIcon.setImageDrawable(null);
        }
    } else {
        trackColor = res.getColor(R.color.all_track_color);
        mTrackId = ScheduleContract.Tracks.ALL_TRACK_ID;

        mIcon.setImageDrawable(null);
        switch (mViewType) {
        case VIEW_TYPE_SESSIONS:
            mTitle.setText(R.string.all_tracks_sessions);
            mAbstract.setText(R.string.all_tracks_subtitle_sessions);
            break;
        case VIEW_TYPE_OFFICE_HOURS:
            mTitle.setText(R.string.all_tracks_office_hours);
            mAbstract.setText(R.string.all_tracks_subtitle_office_hours);
            break;
        case VIEW_TYPE_SANDBOX:
            mTitle.setText(R.string.all_tracks_sandbox);
            mAbstract.setText(R.string.all_tracks_subtitle_sandbox);
            break;
        }
    }

    mRootView.setBackgroundColor(trackColor);
    mCallbacks.onTrackNameAvailable(mTrackId, mTitle.getText().toString());

    if (triggerCallback) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mCallbacks.onTrackSelected(mTrackId);
            }
        });
    }
}