Example usage for android.graphics.drawable Drawable getIntrinsicWidth

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

Introduction

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

Prototype

public int getIntrinsicWidth() 

Source Link

Document

Returns the drawable's intrinsic width.

Usage

From source file:com.appsimobile.appsii.module.weather.WeatherActivity.java

void showDrawable(Drawable drawable, boolean isImmediate) {
    mBackgroundImage.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    if (isImmediate) {
        int w = drawable.getIntrinsicWidth();
        int viewWidth = mBackgroundImage.getWidth();
        float factor = viewWidth / (float) w;
        int h = (int) (drawable.getIntrinsicHeight() * factor);
        drawable.setBounds(0, 0, w, h);//from w  w w  .  j a  v a2 s. c  o m
        mBackgroundImage.setImageDrawable(drawable);
    } else {
        Drawable current = mBackgroundImage.getDrawable();
        if (current == null)
            current = new ColorDrawable(Color.TRANSPARENT);
        TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[] { current, drawable });
        transitionDrawable.setCrossFadeEnabled(true);
        mBackgroundImage.setImageDrawable(transitionDrawable);
        transitionDrawable.startTransition(500);
    }

}

From source file:com.cyou.cma.clockscreen.widget.LinePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;/* w  w  w  . ja va2  s . c  o m*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    if (mCurrentPage >= count) {
        setCurrentItem(count - 1);
        return;
    }

    final float lineWidthAndGap = mDrawableSelected.getIntrinsicWidth() + mGapWidth;
    final float indicatorWidth = (count * lineWidthAndGap) - mGapWidth;
    final float paddingLeft = getPaddingLeft();
    final float paddingRight = getPaddingRight();

    float horizontalOffset = paddingLeft;
    if (mCentered) {
        horizontalOffset += (getWidth() / 2.0f - indicatorWidth / 2.0f);
    }

    // Draw stroked circles
    for (int i = 0; i < count; i++) {
        Drawable drawable = null;
        if (i == mCurrentPage) {
            drawable = getResources().getDrawable(R.drawable.indicator_selected);

        } else {
            drawable = getResources().getDrawable(R.drawable.indicator_normal);
        }
        final int right = (int) horizontalOffset + drawable.getIntrinsicWidth();
        drawable.setBounds((int) horizontalOffset, 0, right, drawable.getIntrinsicHeight());
        horizontalOffset += drawable.getIntrinsicWidth() + mGapWidth;
        ;
        drawable.draw(canvas);
    }
}

From source file:com.linkbubble.util.Util.java

/**
 * Returns a bitmap suitable for the all apps view.
 *//*  www .jav  a 2 s  . c  o m*/
static Bitmap createIconBitmap(Drawable icon, Context context) {
    synchronized (sCanvas) { // we share the statics :-(
        if (sIconWidth == -1) {
            initStatics(context);
        }

        int width = sIconWidth;
        int height = sIconHeight;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes.
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down.
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                }
            } else if (sourceWidth < width && sourceHeight < height) {
                // Don't scale up the icon
                width = sourceWidth;
                height = sourceHeight;
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = sIconTextureWidth;
        int textureHeight = sIconTextureHeight;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        icon.draw(canvas);
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}

From source file:org.gateshipone.malp.application.background.NotificationManager.java

public synchronized void updateNotification(MPDTrack track, MPDCurrentStatus.MPD_PLAYBACK_STATE state) {
    if (track != null) {
        mNotificationBuilder = new NotificationCompat.Builder(mService);

        // Open application intent
        Intent contentIntent = new Intent(mService, MainActivity.class);
        contentIntent.putExtra(MainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW,
                MainActivity.MAINACTIVITY_INTENT_EXTRA_REQUESTEDVIEW_NOWPLAYINGVIEW);
        contentIntent.addFlags(// w  w w  .j  av a  2s .  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(mService, INTENT_OPENGUI, contentIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mNotificationBuilder.setContentIntent(contentPendingIntent);

        // Set pendingintents
        // Previous song action
        Intent prevIntent = new Intent(BackgroundService.ACTION_PREVIOUS);
        PendingIntent prevPendingIntent = PendingIntent.getBroadcast(mService, 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 == MPDCurrentStatus.MPD_PLAYBACK_STATE.MPD_PLAYING) {
            Intent pauseIntent = new Intent(BackgroundService.ACTION_PAUSE);
            playPauseIntent = PendingIntent.getBroadcast(mService, INTENT_PLAYPAUSE, pauseIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            playPauseIcon = R.drawable.ic_pause_48dp;
        } else {
            Intent playIntent = new Intent(BackgroundService.ACTION_PLAY);
            playPauseIntent = PendingIntent.getBroadcast(mService, 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();

        // Stop action
        Intent stopIntent = new Intent(BackgroundService.ACTION_STOP);
        PendingIntent stopPendingIntent = PendingIntent.getBroadcast(mService, INTENT_STOP, stopIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action stopActon = new NotificationCompat.Action.Builder(
                R.drawable.ic_stop_black_48dp, "Stop", stopPendingIntent).build();

        // Next song action
        Intent nextIntent = new Intent(BackgroundService.ACTION_NEXT);
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(mService, 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(BackgroundService.ACTION_QUIT_BACKGROUND_SERVICE);
        PendingIntent quitPendingIntent = PendingIntent.getBroadcast(mService, INTENT_QUIT, quitIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mNotificationBuilder.setDeleteIntent(quitPendingIntent);

        mNotificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
        mNotificationBuilder.setSmallIcon(R.drawable.ic_notification_24dp);
        mNotificationBuilder.addAction(prevAction);
        mNotificationBuilder.addAction(playPauseAction);
        mNotificationBuilder.addAction(stopActon);
        mNotificationBuilder.addAction(nextAction);
        NotificationCompat.MediaStyle notificationStyle = new NotificationCompat.MediaStyle();
        notificationStyle.setShowActionsInCompactView(1, 2);
        mNotificationBuilder.setStyle(notificationStyle);

        String title;
        if (track.getTrackTitle().isEmpty()) {
            title = FormatHelper.getFilenameFromPath(track.getPath());
        } else {
            title = track.getTrackTitle();
        }

        mNotificationBuilder.setContentTitle(title);

        String secondRow;

        if (!track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) {
            secondRow = track.getTrackArtist() + mService.getString(R.string.track_item_separator)
                    + track.getTrackAlbum();
        } else if (track.getTrackArtist().isEmpty() && !track.getTrackAlbum().isEmpty()) {
            secondRow = track.getTrackAlbum();
        } else if (track.getTrackAlbum().isEmpty() && !track.getTrackArtist().isEmpty()) {
            secondRow = track.getTrackArtist();
        } else {
            secondRow = track.getPath();
        }

        // Set the media session metadata
        updateMetadata(track, state);

        mNotificationBuilder.setContentText(secondRow);

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

        // Cover but only if changed
        if (mNotification == null || !track.getTrackAlbum().equals(mLastTrack.getTrackAlbum())) {
            mLastTrack = track;
            mLastBitmap = null;
            mCoverLoader.getImage(mLastTrack, true);
        }

        // 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 = mService.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();

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

From source file:org.totschnig.myexpenses.dialog.HelpDialogFragment.java

@Override
public Drawable getDrawable(String name) {
    int resId;//from  w  w  w.  j a  v a 2s.  co m
    Resources.Theme theme = getActivity().getTheme();
    try {
        if (name.startsWith("?")) {
            name = name.substring(1);
            switch (name) {
            case "calcIcon":
                resId = R.drawable.ic_action_equal;
                break;
            default:
                TypedValue value = new TypedValue();
                theme.resolveAttribute(resolve(name, "attr"), value, true);
                resId = value.resourceId;
            }
        } else {
            if (name.startsWith("android:")) {
                name = name.substring(8);
                resId = resolveSystem(name, "drawable");
            } else {
                resId = resolve(name, "drawable");
            }
        }
        @SuppressWarnings("deprecation")
        Drawable d = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP
                ? getResources().getDrawable(resId, getActivity().getTheme())
                : getResources().getDrawable(resId);
        if (d != null) {
            d.setBounds(0, 0, d.getIntrinsicWidth() / 2, d.getIntrinsicHeight() / 2);
        }
        return d;
    } catch (NotFoundException e) {
        return null;
    }

}

From source file:com.android.leanlauncher.IconCache.java

private Bitmap makeDefaultIcon(UserHandleCompat user) {
    Drawable unbadged = getFullResDefaultActivityIcon();
    Drawable d = mUserManager.getBadgedDrawableForUser(unbadged, user);
    Bitmap b = Bitmap.createBitmap(Math.max(d.getIntrinsicWidth(), 1), Math.max(d.getIntrinsicHeight(), 1),
            Bitmap.Config.ARGB_8888);/*from  w w  w  .ja v  a2 s . com*/
    Canvas c = new Canvas(b);
    d.setBounds(0, 0, b.getWidth(), b.getHeight());
    d.draw(c);
    c.setBitmap(null);
    return b;
}

From source file:com.marshalchen.common.uimodule.modifysys.PagerTitleStrip.java

SpannableString changeSpanString(String temp, int position, int color) {

    try {/* w w w.j ava2s  .c  o m*/
        temp = temp.replaceAll("-", " - ");

        SpannableString ss = new SpannableString(temp);
        Matrix matrix = new Matrix();

        matrix.postRotate(180);

        Bitmap bitmaporg = BitmapFactory.decodeResource(getResources(), R.drawable.ico_arrow2x);
        if (color == 2) {
            bitmaporg = BitmapFactory.decodeResource(getResources(), R.drawable.ico_arrow_grey2x);
        }
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmaporg, 0, 0, bitmaporg.getWidth(), bitmaporg.getHeight(),
                matrix, true);
        BitmapDrawable bmd = new BitmapDrawable(getResources(), resizedBitmap);
        BitmapDrawable bm = new BitmapDrawable(getResources(), bitmaporg);
        Drawable drawable = position == 0 ? bm : bmd;
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth() * 12 / 10,
                drawable.getIntrinsicHeight() * 12 / 10);
        ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);

        ss.setSpan(imageSpan, temp.indexOf("-"), temp.indexOf("-") + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e, "");
        return null;
    }
}

From source file:midian.baselib.widget.PagerSlidingTabStrip.java

private void addRightIcon(TextView tab, int i, boolean isUp) {
    String text = pager.getAdapter().getPageTitle(i).toString() + "    ";
    SpannableStringBuilder str = new SpannableStringBuilder(text + "[icon]");

    // ?Drawable?
    Drawable d = getResources().getDrawable(isUp ? R.drawable.icon_tap_up : R.drawable.icon_tap_down);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    // ImageSpan//from   w  w  w . j  a  va 2  s .  com
    ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
    str.setSpan(span, text.length(), text.length() + "[icon]".length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    tab.setText(str);
}

From source file:com.zhangyp.higo.drawingboard.fragment.SketchFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mSketchView.setOnDrawChangedListener(this);

    stroke.setOnClickListener(new OnClickListener() {
        @Override/*from  www.j a v a 2s  .c  o  m*/
        public void onClick(View v) {

            if (mSketchView.getMode() == SketchView.STROKE) {
                showPopup(v, SketchView.STROKE);
            } else {
                mSketchView.setMode(SketchView.STROKE);
                setAlpha(eraser, 0.4f);
                setAlpha(stroke, 1f);
            }
        }
    });

    setAlpha(eraser, 0.4f);
    eraser.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mSketchView.getMode() == SketchView.ERASER) {
                showPopup(v, SketchView.ERASER);
            } else {
                mSketchView.setMode(SketchView.ERASER);
                setAlpha(stroke, 0.4f);
                setAlpha(eraser, 1f);
            }
        }
    });

    undo.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mSketchView.undo();
        }
    });

    redo.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mSketchView.redo();
        }
    });

    erase.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            askForErase();
        }

        private void askForErase() {
            new MaterialDialog.Builder(getActivity()).content("").positiveText("")
                    .callback(new MaterialDialog.ButtonCallback() {
                        @Override
                        public void onPositive(MaterialDialog dialog) {
                            mSketchView.erase();
                        }
                    }).build().show();
        }
    });

    sketchSave.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mSketchView.getPaths().size() == 0) {
                Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT).show();
                return;
            }
            //?
            new MaterialDialog.Builder(getActivity()).title("?").content("")
                    .inputType(InputType.TYPE_CLASS_TEXT)
                    .input("??(.png)", "a.png", new MaterialDialog.InputCallback() {
                        @Override
                        public void onInput(MaterialDialog dialog, CharSequence input) {
                            // Do something
                            Log.i("AAA", input.toString());

                            save(input.toString());
                        }
                    }).show();

        }
    });
    sketchPhoto.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //
            ImageConfig imageConfig = new ImageConfig.Builder(new ImageLoader() {
                @Override
                public void displayImage(Context context, String path, ImageView imageView) {
                    Glide.with(context).load(path)
                            .placeholder(com.yancy.imageselector.R.mipmap.imageselector_photo).centerCrop()
                            .into(imageView);
                }
            }).steepToolBarColor(getResources().getColor(R.color.blue))
                    .titleBgColor(getResources().getColor(R.color.blue))
                    .titleSubmitTextColor(getResources().getColor(R.color.white))
                    .titleTextColor(getResources().getColor(R.color.white))
                    //?
                    //                        .crop()
                    // ??   
                    .singleSelect()
                    // ?? 
                    .showCamera()
                    // ?? /temp/picture 
                    .filePath("/DrawingBoard/Pictures").build();

            ImageSelector.open(getActivity(), imageConfig); // ?
        }
    });

    // Inflate the popup_layout.xml
    LayoutInflater inflater = (LayoutInflater) getActivity()
            .getSystemService(ActionBarActivity.LAYOUT_INFLATER_SERVICE);
    popupLayout = inflater.inflate(R.layout.popup_sketch_stroke, null);
    // And the one for eraser
    LayoutInflater inflaterEraser = (LayoutInflater) getActivity()
            .getSystemService(ActionBarActivity.LAYOUT_INFLATER_SERVICE);
    popupEraserLayout = inflaterEraser.inflate(R.layout.popup_sketch_eraser, null);

    // Actual stroke shape size is retrieved
    strokeImageView = (ImageView) popupLayout.findViewById(R.id.stroke_circle);
    final Drawable circleDrawable = getResources().getDrawable(R.drawable.circle);
    size = circleDrawable.getIntrinsicWidth();
    // Actual eraser shape size is retrieved
    eraserImageView = (ImageView) popupEraserLayout.findViewById(R.id.stroke_circle);
    size = circleDrawable.getIntrinsicWidth();

    setSeekbarProgress(SketchView.DEFAULT_STROKE_SIZE, SketchView.STROKE);
    setSeekbarProgress(SketchView.DEFAULT_ERASER_SIZE, SketchView.ERASER);

    // Stroke color picker initialization and event managing
    mColorPicker = (ColorPicker) popupLayout.findViewById(R.id.stroke_color_picker);
    mColorPicker.addSVBar((SVBar) popupLayout.findViewById(R.id.svbar));
    mColorPicker.addOpacityBar((OpacityBar) popupLayout.findViewById(R.id.opacitybar));

    mColorPicker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() {
        @Override
        public void onColorChanged(int color) {
            mSketchView.setStrokeColor(color);
        }
    });
    mColorPicker.setColor(mSketchView.getStrokeColor());
    mColorPicker.setOldCenterColor(mSketchView.getStrokeColor());
}

From source file:github.madmarty.madsonic.util.Util.java

public static Bitmap createBitmapFromDrawable(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from   w  w w .ja  v a2 s . c  o m

    Bitmap 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;
}