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:tv.acfun.video.CommentsActivity.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int count = mAdapter.getCount();
    if (position > count) {
        if (isreload) {
            mFootview.findViewById(R.id.list_footview_progress).setVisibility(View.VISIBLE);
            TextView textview = (TextView) mFootview.findViewById(R.id.list_footview_text);
            textview.setText(R.string.buffering);
            requestData(pageIndex, false);
        }//from ww  w.  ja  v a  2s  .c  o  m
        return;
    }
    //        showBar(); //TODO: show input bar when selected comment
    Object o = parent.getItemAtPosition(position);
    if (o == null || !(o instanceof Comment))
        return;
    Comment c = (Comment) o;
    int quoteCount = getQuoteCount();
    removeQuote(mCommentText.getText());
    if (quoteCount == c.count)
        return; // ?
    String pre = ":#" + c.count;
    mQuoteSpan = new Quote(c.count);
    SpannableStringBuilder sb = SpannableStringBuilder.valueOf(mCommentText.getText());
    TextView tv = TextViewUtils.createBubbleTextView(this, pre);
    BitmapDrawable bd = (BitmapDrawable) TextViewUtils.convertViewToDrawable(tv);
    bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
    sb.insert(0, pre);
    mQuoteImage = new ImageSpan(bd);
    sb.setSpan(mQuoteImage, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.setSpan(mQuoteSpan, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.append("");
    mCommentText.setText(sb);
    mCommentText.setSelection(mCommentText.getText().length());
}

From source file:org.telegram.ui.GroupCreateActivity.java

public Emoji.XImageSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf = (LayoutInflater) parentActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
    text.setText(Utilities.formatName(user.first_name, user.last_name));

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);//from   w  w w.j  av a 2  s  . c  o m
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    Emoji.XImageSpan span = new Emoji.XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.id, span);
    for (ImageSpan sp : allSpans) {
        ssb.append("<<");
        ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
}

From source file:com.gh4a.utils.HttpImageGetter.java

private Drawable loadImageForUrl(String source) {
    Bitmap bitmap = null;/*from w  ww  .  j a v a2 s  .c om*/

    if (!mDestroyed) {
        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 = renderSvgToBitmap(mContext.getResources(), is, mWidth, mHeight);
                } else {
                    boolean isGif = mime != null && mime.startsWith("image/gif");
                    if (!isGif || canLoadGif()) {
                        output = File.createTempFile("image", ".tmp", mCacheDir);
                        if (FileUtils.save(output, is)) {
                            if (isGif) {
                                GifDrawable d = new GifDrawable(output);
                                d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                                return d;
                            } else {
                                bitmap = getBitmap(output, mWidth, mHeight);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            // fall through to showing the error 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 (mDestroyed && bitmap != null) {
            bitmap.recycle();
            bitmap = null;
        }
    }

    if (bitmap == null) {
        return mErrorDrawable;
    }

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

From source file:com.yahala.ui.GroupCreateActivity.java

public XImageSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf = (LayoutInflater) parentActivity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
    String name = Utilities.formatName(user.first_name, user.last_name);
    if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
        name = PhoneFormat.getInstance().format("+" + user.phone);
    }/*from   w ww  . java  2 s .c  o m*/
    text.setText(name + ", ");

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    XImageSpan span = new XImageSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.jid, span);
    for (ImageSpan sp : allSpans) {
        ssb.append("<<");
        ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap combineDrawablesToBitmap(final Drawable backgroundImage, final BitmapDrawable overlayImage,
        int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.setBounds(0, 0, width, height);
    backgroundImage.draw(imageCanvas);//  ww w .j  a va 2 s .co  m
    overlayImage.setBounds(0, 0, (int) (overlayImage.getIntrinsicWidth() * 0.5),
            (int) (overlayImage.getIntrinsicHeight() * 0.5));
    final int scaleWidth = 24;
    final int scaleHeight = 24;
    final float scaleWidthPercent = ((float) scaleWidth) / overlayImage.getIntrinsicWidth();
    final float scaleHeightPercent = ((float) scaleHeight) / overlayImage.getIntrinsicHeight();
    ScaleDrawable scaleDrawable = new ScaleDrawable(overlayImage, 0, scaleWidthPercent, scaleHeightPercent);
    Drawable scaledOverlay = scaleDrawable.getDrawable();
    scaledOverlay.setBounds(0, 0, width, height);
    scaledOverlay.draw(imageCanvas);

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

From source file:com.fatelon.partyphotobooth.setup.fragments.EventInfoSetupFragment.java

/**
 * Loads the event logo from cache and updates ui when the operation completes. If the logo is unavailable,
 * an attempt will be made to create a new logo based on the source image uri.
 *
 * @param srcUri the {@link Uri} to the source image.
 *///from   ww w .  ja  v  a2  s  .  co  m
private void loadLogo(final String srcUri) {
    mBitmapCache.asyncGet(BaseTitleHeader.EVENT_LOGO_CACHE_KEY, new PersistedBitmapCache.IAsyncGetCallbacks() {

        @Override
        public void onSuccess(String key, Bitmap bitmap) {
            if (isActivityAlive()) {
                mLogoUri.setText(srcUri);

                // Set thumb as compound drawable, fitted to the text height.
                final BitmapDrawable thumb = new BitmapDrawable(getResources(), bitmap);
                int thumbSize = (int) getResources().getDimension(R.dimen.text_size_normal);
                Point drawableSize = ImageHelper.getAspectFitSize(thumbSize, thumbSize, bitmap.getWidth(),
                        bitmap.getHeight());
                thumb.setBounds(0, 0, drawableSize.x, drawableSize.y);
                mLogoUri.setCompoundDrawables(null, null, thumb, null);

                mLogoClear.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onFailure(String key) {
            if (isActivityAlive()) {
                newLogo(Uri.parse(srcUri));
            }
        }
    });
}

From source file:tv.acfun.a63.CommentsActivity.java

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    int count = mAdapter.getCount();
    if (position > count) {
        if (isreload) {
            mFootview.findViewById(R.id.list_footview_progress).setVisibility(View.VISIBLE);
            TextView textview = (TextView) mFootview.findViewById(R.id.list_footview_text);
            textview.setText(R.string.loading);
            requestData(pageIndex, false);
        }/* w w w  .j  a  va  2s  .  co  m*/
        return;
    }
    showBar(); // show input bar when selected comment
    Object o = parent.getItemAtPosition(position);
    if (o == null || !(o instanceof Comment))
        return;
    Comment c = (Comment) o;
    int quoteCount = getQuoteCount();
    removeQuote(mCommentText.getText());
    if (quoteCount == c.count)
        return; // ?
    String pre = ":#" + c.count;
    mQuoteSpan = new Quote(c.count);
    /**
     * @see http 
     *      ://www.kpbird.com/2013/02/android-chips-edittext-token-edittext
     *      .html
     */
    SpannableStringBuilder sb = SpannableStringBuilder.valueOf(mCommentText.getText());
    TextView tv = TextViewUtils.createBubbleTextView(this, pre);
    BitmapDrawable bd = (BitmapDrawable) TextViewUtils.convertViewToDrawable(tv);
    bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
    sb.insert(0, pre);
    mQuoteImage = new ImageSpan(bd);
    sb.setSpan(mQuoteImage, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.setSpan(mQuoteSpan, 0, pre.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sb.append("");
    mCommentText.setText(sb);
    mCommentText.setSelection(mCommentText.getText().length());
}

From source file:org.cnc.mombot.utils.ContactsEditText.java

private Object convertViewToDrawable(View view) {
    BitmapDrawable bd = null;
    try {//from  w w  w .  java  2s  .com
        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        view.measure(spec, spec);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.translate(-view.getScrollX(), -view.getScrollY());
        view.draw(canvas);
        bd = new BitmapDrawable(getContext().getResources(), bitmap);
        bd.setBounds(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    } catch (Exception ex) {
    }
    return bd;
}

From source file:com.fatelon.partyphotobooth.setup.fragments.EventInfoSetupFragment.java

/**
 * Creates and stores a new event logo from an image {@link Uri}. Update ui when the operation completes.
 *
 * @param srcUri the {@link Uri} to the source image.
 */// w w w  .  j  av a 2  s . c o m
private void newLogo(final Uri srcUri) {
    if (TextHelper.isValid(srcUri.toString())) {
        final ContentResolver resolver = getActivity().getContentResolver();
        final Handler workerHandler = new Handler(MyApplication.getWorkerLooper());
        workerHandler.post(new Runnable() {

            @Override
            public void run() {
                // Load and create new logo.
                final Bitmap newLogo = ImageHelper.getScaledBitmap(resolver, srcUri,
                        BaseTitleHeader.EVENT_LOGO_MAX_WIDTH, BaseTitleHeader.EVENT_LOGO_MAX_HEIGHT);
                if (newLogo != null) {
                    // Store new logo in cache.
                    mBitmapCache.asyncPut(BaseTitleHeader.EVENT_LOGO_CACHE_KEY, newLogo,
                            new PersistedBitmapCache.IAsyncPutCallbacks() {

                                @Override
                                public void onSuccess(String key) {
                                    if (isActivityAlive()) {
                                        mLogoUri.setText(srcUri.toString());

                                        // tryGet() should always succeed since the cache has just been populated.
                                        final Bitmap logo = mBitmapCache.tryGet(key);
                                        if (logo != null) {
                                            // Set thumb as compound drawable, fitted to the text height.
                                            final BitmapDrawable thumb = new BitmapDrawable(getResources(),
                                                    logo);
                                            int thumbSize = (int) getResources()
                                                    .getDimension(R.dimen.text_size_normal);
                                            Point drawableSize = ImageHelper.getAspectFitSize(thumbSize,
                                                    thumbSize, logo.getWidth(), logo.getHeight());
                                            thumb.setBounds(0, 0, drawableSize.x, drawableSize.y);
                                            mLogoUri.setCompoundDrawables(null, null, thumb, null);
                                        }

                                        mLogoClear.setVisibility(View.VISIBLE);
                                    }
                                }

                                @Override
                                public void onFailure(String key) {
                                    postLogoError();
                                }
                            });
                } else {
                    postLogoError();
                }
            }
        });
    } else {
        postLogoError();
    }
}

From source file:org.telegram.ui.ChannelCreateActivity.java

private ChipSpan createAndPutChipForUser(TLRPC.User user) {
    try {/*from   ww  w .  j av  a 2s .co  m*/
        LayoutInflater lf = (LayoutInflater) ApplicationLoader.applicationContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View textView = lf.inflate(R.layout.group_create_bubble, null);
        TextView text = (TextView) textView.findViewById(R.id.bubble_text_view);
        String name = UserObject.getUserName(user);
        if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
            name = PhoneFormat.getInstance().format("+" + user.phone);
        }
        text.setText(name + ", ");

        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        textView.measure(spec, spec);
        textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
        Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(b);
        canvas.translate(-textView.getScrollX(), -textView.getScrollY());
        textView.draw(canvas);
        textView.setDrawingCacheEnabled(true);
        Bitmap cacheBmp = textView.getDrawingCache();
        Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
        textView.destroyDrawingCache();

        final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
        bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

        SpannableStringBuilder ssb = new SpannableStringBuilder("");
        ChipSpan span = new ChipSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
        allSpans.add(span);
        selectedContacts.put(user.id, span);
        for (ImageSpan sp : allSpans) {
            ssb.append("<<");
            ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        nameTextView.setText(ssb);
        nameTextView.setSelection(ssb.length());
        return span;
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
    return null;
}