Example usage for android.graphics.drawable Drawable setBounds

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

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable 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:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * @param context context used to access resources
 * @param drawable a Drawable//from  w w  w .  ja  va2  s  .  com
 * @param w width (pixels or dp)
 * @param h height (pixels or dp)
 * @param pxValues true w and h are in pixels, false w and h are in dp
 * @return a Bitmap measuring w,h of the specified drawable
 */
public static Bitmap drawableToBitmap(Context context, Drawable drawable, int w, int h, boolean pxValues) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    if (!pxValues) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        w = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, w, metrics);
        h = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, h, metrics);
    }
    //Log.d("DEBUG", "drawableToBitmap: " + drawable.toString() + "::" + w + ", " + h);

    if (w <= 0 || h <= 0) {
        Log.w("drawableToBitmap", "invalid width or height: " + w + ", " + h);
        w = h = 1;
    }

    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:com.example.ysh.myapplication.view.readview.TocListAdapter.java

@Override
public void convert(EasyLVHolder holder, int position, BookMixAToc.mixToc.Chapters chapters) {
    TextView tvTocItem = holder.getView(R.id.tvTocItem);
    tvTocItem.setText(chapters.title);// w  ww  . j av  a  2 s.  c  om
    Drawable drawable;
    if (currentChapter == position + 1) {
        tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.light_red));
        drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_toc_item_activated);
    } else if (isEpub || FileUtils.getChapterFile(bookId, position + 1).length() > 10) {
        tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.light_black));
        drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_toc_item_download);
    } else {
        tvTocItem.setTextColor(ContextCompat.getColor(mContext, R.color.light_black));
        drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_toc_item_normal);
    }
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
    tvTocItem.setCompoundDrawables(drawable, null, null, null);
}

From source file:org.gateshipone.odyssey.playbackservice.managers.OdysseyNotificationManager.java

public void updateNotification(TrackModel track, PlaybackService.PLAYSTATE state,
        MediaSessionCompat.Token mediaSessionToken) {
    if (track != null) {
        mNotificationBuilder = new NotificationCompat.Builder(mContext);

        // Open application intent
        Intent contentIntent = new Intent(mContext, OdysseyMainActivity.class);
        contentIntent.putExtra(OdysseyMainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW,
                OdysseyMainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW_NOWPLAYINGVIEW);
        contentIntent.addFlags(/* w  w w  .  j av  a2  s  .  c  o  m*/
                Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NO_HISTORY);
        PendingIntent contentPendingIntent = PendingIntent.getActivity(mContext, NOTIFICATION_INTENT_OPENGUI,
                contentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mNotificationBuilder.setContentIntent(contentPendingIntent);

        // Set pendingintents
        // Previous song action
        Intent prevIntent = new Intent(PlaybackService.ACTION_PREVIOUS);
        PendingIntent prevPendingIntent = PendingIntent.getBroadcast(mContext, NOTIFICATION_INTENT_PREVIOUS,
                prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action prevAction = new NotificationCompat.Action.Builder(
                R.drawable.ic_skip_previous_48dp, "Previous", prevPendingIntent).build();

        // Pause/Play action
        PendingIntent playPauseIntent;
        int playPauseIcon;
        if (state == PlaybackService.PLAYSTATE.PLAYING) {
            Intent pauseIntent = new Intent(PlaybackService.ACTION_PAUSE);
            playPauseIntent = PendingIntent.getBroadcast(mContext, NOTIFICATION_INTENT_PLAYPAUSE, pauseIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            playPauseIcon = R.drawable.ic_pause_48dp;
        } else {
            Intent playIntent = new Intent(PlaybackService.ACTION_PLAY);
            playPauseIntent = PendingIntent.getBroadcast(mContext, NOTIFICATION_INTENT_PLAYPAUSE, playIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            playPauseIcon = R.drawable.ic_play_arrow_48dp;
        }
        NotificationCompat.Action playPauseAction = new NotificationCompat.Action.Builder(playPauseIcon,
                "PlayPause", playPauseIntent).build();

        // Next song action
        Intent nextIntent = new Intent(PlaybackService.ACTION_NEXT);
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(mContext, NOTIFICATION_INTENT_NEXT,
                nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action nextAction = new NotificationCompat.Action.Builder(
                R.drawable.ic_skip_next_48dp, "Next", nextPendingIntent).build();

        // Quit action
        Intent quitIntent = new Intent(PlaybackService.ACTION_QUIT);
        PendingIntent quitPendingIntent = PendingIntent.getBroadcast(mContext, NOTIFICATION_INTENT_QUIT,
                quitIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mNotificationBuilder.setDeleteIntent(quitPendingIntent);

        mNotificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        mNotificationBuilder.setSmallIcon(R.drawable.odyssey_notification);
        mNotificationBuilder.addAction(prevAction);
        mNotificationBuilder.addAction(playPauseAction);
        mNotificationBuilder.addAction(nextAction);
        NotificationCompat.MediaStyle notificationStyle = new NotificationCompat.MediaStyle();
        notificationStyle.setShowActionsInCompactView(1, 2);
        notificationStyle.setMediaSession(mediaSessionToken);
        mNotificationBuilder.setStyle(notificationStyle);
        mNotificationBuilder.setContentTitle(track.getTrackName());
        mNotificationBuilder.setContentText(track.getTrackArtistName());

        // Remove unnecessary time info
        mNotificationBuilder.setWhen(0);

        // Cover but only if changed
        if (mLastTrack == null || !track.getTrackAlbumKey().equals(mLastTrack.getTrackAlbumKey())) {
            mLastTrack = track;
            mLastBitmap = null;
        }

        // Only set image if an saved one is available
        if (mLastBitmap != null) {
            mNotificationBuilder.setLargeIcon(mLastBitmap);
        } else {
            /**
             * Create a dummy placeholder image for versions greater android 7 because it
             * does not automatically show the application icon anymore in mediastyle notifications.
             */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Drawable icon = mContext.getDrawable(R.drawable.notification_placeholder_256dp);

                Bitmap iconBitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(),
                        Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(iconBitmap);
                DrawFilter filter = new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG, 1);

                canvas.setDrawFilter(filter);
                icon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                icon.setFilterBitmap(true);

                icon.draw(canvas);
                mNotificationBuilder.setLargeIcon(iconBitmap);

            } else {
                /**
                 * For older android versions set the null icon which will result in a dummy icon
                 * generated from the application icon.
                 */
                mNotificationBuilder.setLargeIcon(null);

            }
        }

        // Build the notification
        mNotification = mNotificationBuilder.build();

        // Check if run from service and check if playing or pause.
        // Pause notification should be dismissible.
        if (mContext instanceof Service) {
            if (state == PlaybackService.PLAYSTATE.PLAYING) {
                ((Service) mContext).startForeground(NOTIFICATION_ID, mNotification);
            } else {
                ((Service) mContext).stopForeground(false);
            }
        }

        // Send the notification away
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }

}

From source file:cz.maresmar.sfm.view.user.UserDetailFragment.java

/**
 * Transform any Drawable to bitmap//from   ww w  . j av  a 2 s. c om
 * <p>
 * taken from https://stackoverflow.com/a/35574775/1392034
 *
 * @param drawable Input Drawable
 * @return Transformed Bitmap
 */
private Bitmap getBitmap(@NonNull Drawable drawable) {
    Canvas canvas = new Canvas();
    int imageSize = getResources().getDimensionPixelSize(R.dimen.user_image_size);
    Bitmap bitmap = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, imageSize, imageSize);
    drawable.draw(canvas);

    return bitmap;
}

From source file:ru.jango.j0widget.imagebrowser.ImageBrowserView.java

private Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable)
        return ((BitmapDrawable) drawable).getBitmap();

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;//w ww. j a  v a 2s .  c o m
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    final Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:com.coreform.open.android.formidablevalidation.SetErrorHandler.java

public void setError(CharSequence error, Drawable icon, boolean showError,
        boolean showCompoundDrawableOnRight) {
    if (DEBUG)// w w  w.  j av  a 2s .  c om
        Log.d(TAG, ".setError(error, icon, showError, showCompoundDrawableOnRight)...");
    if (icon != null) {
        if (DEBUG)
            Log.d(TAG, "...icon is not null...");
        icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
    }
    error = TextUtils.stringOrSpannedString(error);
    mErrorWasChanged = true;

    mError = error;
    mErrorWasChanged = true;

    final Drawables dr = mDrawables;
    if (mView instanceof TextView && error != null) {
        if (true || dr != null) {
            if (showCompoundDrawableOnRight) {
                if (DEBUG)
                    Log.d(TAG, "...showing CompoundDrawable on right)...");
                //((TextView) mView).setCompoundDrawables(dr.mDrawableLeft, dr.mDrawableTop, icon, dr.mDrawableBottom);
                ((TextView) mView).setCompoundDrawables(null, null, icon, null);
            } else {
                //((TextView) mView).setCompoundDrawables(icon, dr.mDrawableTop, dr.mDrawableRight, dr.mDrawableBottom);
                ((TextView) mView).setCompoundDrawables(icon, null, null, null);
            }
        }
    }

    if (error == null) {
        if (mPopup != null) {
            if (mPopup.isShowing()) {
                mPopup.dismiss();
            }
            if (mView instanceof TextView) {
                ((TextView) mView).setCompoundDrawables(null, null, null, null);
            }
            mPopup = null;
        }
    } else if (showError) {
        //LD - EditTexts use isFocused to show only the focused one, other Views may not be focusable
        //if (isFocused()) {
        showError();
        //}
    }
}

From source file:com.polyvi.xface.extension.XAppExt.java

private String drawableToBase64(Drawable drawable) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);//ww  w  . ja  v a  2 s  .  c om
    String result = null;
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            baos.flush();
            baos.close();
            byte[] bitmapBytes = baos.toByteArray();
            result = XBase64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:fr.simon.marquis.secretcodes.util.ExportContentProvider.java

private void saveImageFiles(ArrayList<SecretCode> secretCodes) {
    for (SecretCode secretCode : secretCodes) {
        try {//from   w w w .  j  av  a2  s.c o  m
            if (secretCode.getDrawableResource() == 0) {
                continue;
            }
            Drawable drawable = getContext().getPackageManager().getDrawable(secretCode.getPackageManager(),
                    secretCode.getDrawableResource(), null);
            if (drawable == null) {
                continue;
            }
            int height = drawable.getIntrinsicHeight();
            int width = drawable.getIntrinsicWidth();
            Bitmap createBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            if (createBitmap == null) {
                continue;
            }
            Canvas canvas = new Canvas(createBitmap);
            drawable.setBounds(0, 0, width, height);
            drawable.draw(canvas);

            FileOutputStream openFileOutput = getContext().openFileOutput(secretCode.getCode() + ".png",
                    Context.MODE_PRIVATE);
            createBitmap.compress(Bitmap.CompressFormat.PNG, 100, openFileOutput);
            openFileOutput.flush();
            openFileOutput.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

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

private void initCommentsBar() {
    mCommentBar = findViewById(R.id.comments_bar);

    if (ActionBarUtil.hasSB()
            && getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        RelativeLayout.LayoutParams params = (LayoutParams) mCommentBar.getLayoutParams();
        params.bottomMargin = getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        mCommentBar.setLayoutParams(params);
    }//from  w w  w.ja v a2 s . c  om
    mBtnSend = (ImageButton) findViewById(R.id.comments_send_btn);
    mCommentText = (EditText) findViewById(R.id.comments_edit);
    mBtnEmotion = findViewById(R.id.comments_emotion_btn);
    mEmotionGrid = (GridView) findViewById(R.id.emotions);
    mBtnSend.setOnClickListener(this);
    mBtnEmotion.setOnClickListener(this);
    mEmotionGrid.setAdapter(mEmotionAdapter);
    mEmotionGrid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int index = mCommentText.getSelectionEnd();
            Editable text = mCommentText.getText();
            String emotion = parent.getItemAtPosition(position).toString();
            text.insert(index, emotion);
            EmotionView v = (EmotionView) parent.getAdapter().getView(position, null, null);
            Drawable drawable = TextViewUtils.convertViewToDrawable(v);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2);
            text.setSpan(new ImageSpan(drawable), index, index + emotion.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

    });
}

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

@Override
public void onResponse(Comments response) {
    isloading = false;//www .jav  a 2  s.c o  m
    mPtr.onRefreshComplete();
    if (response.totalCount == 0) {
        mLoadingBar.setVisibility(View.GONE);
        mTimeOutText.setVisibility(View.VISIBLE);
        mList.setVisibility(View.GONE);
        Drawable drawable = getResources().getDrawable(R.drawable.ac_16);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        mTimeOutText.setCompoundDrawables(drawable, null, null, null);
        mTimeOutText.setText(R.string.no_comment_yet);
        return;
    }

    if (response.page == 1) {
        if (mAdapter != null)
            mAdapter.notifyDataSetInvalidated();
        data.clear();
        commentIdList.clear();
        mLoadingBar.setVisibility(View.GONE);
        mList.setVisibility(View.VISIBLE);
    }
    ArrayUtil.putAll(response.commentArr, data);
    commentIdList.addAll(ArrayUtil.asList(response.commentList));
    hasNextPage = response.nextPage > response.page;
    if (data != null && data.size() > 0) {
        mAdapter.setData(data, commentIdList);
        mAdapter.notifyDataSetChanged();
        isreload = false;
    }

}