Example usage for android.graphics Canvas getWidth

List of usage examples for android.graphics Canvas getWidth

Introduction

In this page you can find the example usage for android.graphics Canvas getWidth.

Prototype

public int getWidth() 

Source Link

Document

Returns the width of the current drawing layer

Usage

From source file:com.abhinavjhanwar.android.egg.neko.Cat.java

@Override
public void draw(@NonNull Canvas canvas) {
    final int hw = Math.min(canvas.getWidth(), canvas.getHeight());

    if (mBitmap == null || mBitmap.getWidth() != hw || mBitmap.getHeight() != hw) {
        mBitmap = Bitmap.createBitmap(hw, hw, Bitmap.Config.ARGB_8888);
        final Canvas bitCanvas = new Canvas(mBitmap);
        slowDraw(bitCanvas, 0, 0, hw, hw);
    }/*from   w  w w . j a  va2s.c om*/
    canvas.drawBitmap(mBitmap, 0, 0, null);
}

From source file:com.android.incallui.CallCardFragment.java

/**
 * Converts a drawable into a bitmap./*w  w w.j  av  a 2  s.  c o m*/
 *
 * @param drawable the drawable to be converted.
 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            // Needed for drawables that are just a colour.
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                    Bitmap.Config.ARGB_8888);
        }

        Log.i(TAG, "Created bitmap with width " + bitmap.getWidth() + ", height " + bitmap.getHeight());

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    }
    return bitmap;
}

From source file:de.hs_bremen.aurora_hunter.ui.views.KpIndexChartView.java

public void onDraw(Canvas canvas) {

    if (points.size() == 0) {
        return;/*from   ww  w  .j  a v  a2s  .c om*/
    }
    Path path = new Path();
    int height = canvas.getHeight();
    int width = canvas.getWidth();

    for (Point point : points) {
        point.x = point.percentageX * width + 60;
        // Log.i("scaleFactor", " : " + scaleFactor);
        //Log.i("percent", " : " + percent);

        point.y = (float) ((1 - point.percentageY * scaleFactor) * height * 0.7 + 30);
    }

    if (points.size() > 1) {
        //calcuate x/y based on size of canvas
        for (int i = 0; i < points.size(); i++) {
            if (i >= 0) {
                Point point = points.get(i);
                //  Log.i("dx",point.x + " - " + point.y );
                if (i == 0) {
                    Point next = points.get(i + 1);
                    point.dx = ((next.x - point.x) / 5);
                    point.dy = ((next.y - point.y) / 5);
                } else if (i == points.size() - 1) {
                    Point prev = points.get(i - 1);
                    point.dx = ((point.x - prev.x) / 5);
                    point.dy = ((point.y - prev.y) / 5);
                } else {
                    Point next = points.get(i + 1);
                    Point prev = points.get(i - 1);
                    point.dx = ((next.x - prev.x) / 5);
                    point.dy = ((next.y - prev.y) / 5);
                }
            }
        }
    }

    if (points.size() > 0) {
        path.moveTo(0, (float) (canvas.getHeight() * 0.8));
        path.lineTo(0, points.get(0).y);

    }
    boolean first = true;

    for (int i = 0; i < points.size(); i++) {
        Point point = points.get(i);
        if (first) {
            first = false;
            path.cubicTo(point.x - point.x * 2, point.y - point.y / 5, point.x - point.dx, point.y - point.dy,
                    point.x, point.y);
        } else {
            Point prev = points.get(i - 1);
            //  Log.i("Draw", point.dx  + " " + point.dy);
            path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x,
                    point.y);
        }
    }
    path.lineTo(width, (float) (height * 0.8));

    path.lineTo(width, (float) (height * 0.9));
    path.lineTo(0, (float) (height * 0.9));

    canvas.drawPath(path, mGraphPaint);
    canvas.drawRect(0, (float) (height * 0.9), width, height, mGraphPaint);

    double Kp1Height = (1 - 0.6666666) * height;//points.get(points.size()-1).y;
    canvas.drawRect(0, (float) Kp1Height, width, (float) Kp1Height + 1, paintG1Line);
    canvas.drawText("G1", 2, (float) Kp1Height, paintTxt);
    int detlaY = 15;

    for (Point p : points) {
        int val = (int) Math.round(9 * p.percentageY);
        //if last element
        if (points.indexOf(p) == points.size() - 1) {
            //Log.i("last", p.toString());
            //   canvas.drawText(val + "", p.x-150,p.y - detlaY, paintTxt);
        } else {
            canvas.drawText(val + "", p.x - 10, p.y - detlaY, paintTxt);
        }
        //Log.i("point", p.toString());
    }
    // Log.i("Lenght", points.size() + " ");

}

From source file:net.naonedbus.appwidget.HoraireWidgetProvider.java

/**
 * Prparer le widget avec les lments fixes (nom, ligne, sens...)
 *//*  w w w  .  j  a va 2  s.  c o m*/
protected void prepareWidgetViewInit(final Context context, final RemoteViews views, final Favori favori) {
    views.setTextViewText(R.id.itemSymbole, favori.getLettre());

    // Uniquement pour 2.2 et sup
    if (Build.VERSION.SDK_INT > 7) {
        views.setTextColor(R.id.itemSymbole, favori.getCouleurTexte());
        views.setTextColor(R.id.itemTitle, favori.getCouleurTexte());
        views.setTextColor(R.id.itemDescription, favori.getCouleurTexte());

        final GradientDrawable gradientDrawable = ColorUtils.getGradiant(favori.getCouleurBackground());
        final Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        gradientDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        gradientDrawable.draw(canvas);
        views.setImageViewBitmap(R.id.widgetHeaderBackground, bitmap);
    }

    if (favori.getNomFavori() == null) {
        views.setTextViewText(R.id.itemTitle, favori.getNomArret());
        views.setTextViewText(R.id.itemDescription, FormatUtils.formatSens(favori.getNomSens()));
    } else {
        views.setTextViewText(R.id.itemTitle, favori.getNomArret());
        views.setTextViewText(R.id.itemDescription,
                FormatUtils.formatSens(favori.getNomArret(), favori.getNomSens()));
    }

    views.setViewVisibility(R.id.widgetLoading, View.GONE);
}

From source file:org.totschnig.myexpenses.util.Utils.java

public static Bitmap getTintedBitmapForTheme(Context context, int drawableResId, int themeResId) {
    Context wrappedContext = new ContextThemeWrapper(context, themeResId);
    Drawable d = AppCompatDrawableManager.get().getDrawable(wrappedContext, drawableResId);
    Bitmap b = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    d.setBounds(0, 0, c.getWidth(), c.getHeight());
    d.draw(c);//from   w w  w.  j av  a  2s .c  o  m
    return b;
}

From source file:com.cocosw.accessory.views.layout.CollapsingTitleLayout.java

@Override
public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    final int toolbarHeight = mToolbar.getHeight();
    canvas.clipRect(0, 0, canvas.getWidth(), interpolate(canvas.getHeight(), toolbarHeight, mScrollOffset));

    // Now call super and let it draw the background, etc
    super.draw(canvas);

    if (mTitleToDraw != null) {
        float x = mTextLeft;
        float y = mTextTop;

        if (!mUseTexture) {
            // If we're not drawing a texture, we need to properly offset the text
            x -= mDrawnTextBounds.left;/*  w  ww. ja  v a 2  s.c o  m*/
            y -= mDrawnTextBounds.top;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mTextLeft, mTextTop, mTextRight, mTextTop + mDrawnTextBounds.height(),
                    DEBUG_DRAW_PAINT);
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (mUseTexture && mExpandedTitleTexture != null) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTitleToDraw, x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.journeyapps.barcodescanner.WXViewfinderView.java

@SuppressLint("DrawAllocation")
@Override/*from w  w  w.j a va  2 s . co  m*/
public void onDraw(Canvas canvas) {
    refreshSizes();
    if (framingRect == null || previewFramingRect == null) {
        return;
    }

    Rect frame = framingRect;
    Rect previewFrame = previewFramingRect;

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    maskPaint.setColor(maskColor);
    canvas.drawRect(0, 0, width, frame.top, maskPaint);
    canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, maskPaint);
    canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, maskPaint);
    canvas.drawRect(0, frame.bottom + 1, width, height, maskPaint);
    //drawable the border
    canvas.drawRect(frame.left + 1, frame.top + 1, frame.right, frame.bottom, borderPaint);
    int halfWidth = (int) (cornerWidth / 2);
    //draw four corner
    Path corner1 = new Path();
    corner1.moveTo(frame.left, frame.top + cornerLength);
    corner1.lineTo(frame.left, frame.top);
    corner1.lineTo(frame.left + cornerLength, frame.top);
    Matrix translate1 = new Matrix();
    translate1.setTranslate(halfWidth, halfWidth);
    corner1.transform(translate1);
    canvas.drawPath(corner1, cornerPaint);

    Path corner2 = new Path();
    corner2.moveTo(frame.right + 1 - cornerLength, frame.top);
    corner2.lineTo(frame.right + 1, frame.top);
    corner2.lineTo(frame.right + 1, frame.top + cornerLength);
    Matrix translate2 = new Matrix();
    translate2.setTranslate(-halfWidth, halfWidth);
    corner2.transform(translate2);
    canvas.drawPath(corner2, cornerPaint);

    Path corner3 = new Path();
    corner3.moveTo(frame.left, frame.bottom + 1 - cornerLength);
    corner3.lineTo(frame.left, frame.bottom + 1);
    corner3.lineTo(frame.left + cornerLength, frame.bottom + 1);
    Matrix translate3 = new Matrix();
    translate3.setTranslate(halfWidth, -halfWidth);
    corner3.transform(translate3);
    canvas.drawPath(corner3, cornerPaint);

    Path corner4 = new Path();
    corner4.moveTo(frame.right + 1 - cornerLength, frame.bottom + 1);
    corner4.lineTo(frame.right + 1, frame.bottom + 1);
    corner4.lineTo(frame.right + 1, frame.bottom + 1 - cornerLength);
    Matrix translate4 = new Matrix();
    translate4.setTranslate(-halfWidth, -halfWidth);
    corner4.transform(translate4);
    canvas.drawPath(corner4, cornerPaint);

    offset += speed;
    if (offset >= frame.bottom - frame.top) {
        offset = 0;
    }
    Rect rect = new Rect();
    rect.left = frame.left + 1 + laserPadding;
    rect.top = frame.top + 1 + offset;
    rect.right = frame.right - laserPadding;
    rect.bottom = frame.top + 1 + offset + 3;

    Bitmap laserBitmap = ((BitmapDrawable) ResourcesCompat.getDrawable(getResources(), R.drawable.scan_laser,
            null)).getBitmap();
    canvas.drawBitmap(laserBitmap, null, rect, linePaint);

    textPaint.setTextAlign(Paint.Align.CENTER);

    canvas.drawText(statusText, (frame.right + frame.left) / 2,
            frame.bottom + statusTextPadding + statusTextSize, textPaint);

    postInvalidateDelayed(animationDelay, frame.left, frame.top, frame.right, frame.bottom);

}

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(//from  ww w .j  a  v a 2  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:com.forrestguice.suntimeswidget.LightMapView.java

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean drawRect(SuntimesRiseSetData data, Canvas c, Paint p) {
    Calendar riseTime = data.sunriseCalendarToday();
    Calendar setTime = data.sunsetCalendarToday();
    if (riseTime == null && setTime == null) {
        return false;
    }//from  w  ww. j ava 2  s.  c o m

    int w = c.getWidth();
    int h = c.getHeight();

    int left = 0;
    if (riseTime != null) {
        double riseMinute = riseTime.get(Calendar.HOUR_OF_DAY) * 60 + riseTime.get(Calendar.MINUTE);
        double riseR = riseMinute / MINUTES_IN_DAY;
        left = (int) Math.round(riseR * w);
    }

    int right = w;
    if (setTime != null) {
        double setMinute = setTime.get(Calendar.HOUR_OF_DAY) * 60 + setTime.get(Calendar.MINUTE);
        double setR = setMinute / MINUTES_IN_DAY;
        right = (int) Math.round(setR * w);
    }

    boolean setTimeBeforeRiseTime = (riseTime != null && setTime != null
            && setTime.getTime().before(riseTime.getTime()));
    if (setTimeBeforeRiseTime) {
        c.drawRect(0, 0, right, h, p);
        c.drawRect(left, 0, w, h, p);

    } else {
        c.drawRect(left, 0, right, h, p);
    }
    return true;
}

From source file:com.gruporaido.tasker_library.util.Helper.java

/**
 * @param drawableId/*from w  w w.  j  a v  a 2 s .  c  o m*/
 * @param text
 * @param textSize
 * @param offsetX
 * @param offsetY
 * @return
 */
public Bitmap drawTextOnDrawable(int drawableId, String text, int textSize, int offsetX, int offsetY) {

    Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(), drawableId).copy(Bitmap.Config.ARGB_8888,
            true);

    Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTypeface(tf);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(dpToPx(textSize));

    Rect textRect = new Rect();
    paint.getTextBounds(text, 0, text.length(), textRect);

    Canvas canvas = new Canvas(bm);

    //If the text is bigger than the canvas , reduce the font size
    if (textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
        paint.setTextSize(dpToPx(textSize / 2)); //Scaling needs to be used for different dpi's

    //Calculate the positions
    int xPos = (canvas.getWidth() / 2) - 2 + dpToPx(offsetX); //-2 is for regulating the x position offset

    //"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) + dpToPx(offsetY);

    canvas.drawText(text, xPos, yPos, paint);

    return bm;
}