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:org.stockchart.stickers.FibonacciSticker.java

@Override
protected void draw(SeriesPaintInfo info, PointF p1, PointF p2, Canvas c) {
    PointF rightMost = Utils.getRightmost(p1, p2);
    PointF leftMost = Utils.getLeftmost(p1, p2);

    boolean isRising = Utils.isRising(p1, p2);

    float maxY = Math.max(p1.y, p2.y);
    float minY = Math.min(p1.y, p2.y);

    float height = maxY - minY;
    float originY = isRising ? minY : maxY;

    float m = fAppearance.getOutlineWidth();

    // guideline// www  .ja v a2 s  .co  m
    //      fAppearance.applyOutline(fPaint);
    //      PaintUtils.drawLine(c, p1.x, p1.y, p2.x, p2.y, fPaint);

    switch (fType) {
    case FANS: {
        float r382 = originY + height * 0.382f * (isRising ? 1f : -1f);
        float r500 = originY + height * 0.5f * (isRising ? 1f : -1f);
        float r618 = originY + height * 0.618f * (isRising ? 1f : -1f);
        fAppearance.applyOutline(fPaint);

        float rightMostX = Math.max(rightMost.x, c.getWidth() + 1f);

        PaintUtils.drawLine(c, leftMost.x, leftMost.y, rightMostX,
                Utils.getY(leftMost.x, leftMost.y, rightMost.x, r382, rightMostX), fPaint);
        PaintUtils.drawLine(c, leftMost.x, leftMost.y, rightMostX,
                Utils.getY(leftMost.x, leftMost.y, rightMost.x, r500, rightMostX), fPaint);
        PaintUtils.drawLine(c, leftMost.x, leftMost.y, rightMostX,
                Utils.getY(leftMost.x, leftMost.y, rightMost.x, r618, rightMostX), fPaint);
    }
        break;
    case RETRACEMENT: {
        Set<Entry<Float, String>> entrySet = RETRACEMENTS.entrySet();
        Iterator<Entry<Float, String>> iterator = entrySet.iterator();

        while (iterator.hasNext()) {
            Entry<Float, String> i = iterator.next();

            float y = originY + height * i.getKey() * (isRising ? 1f : -1f);

            fAppearance.applyOutline(fPaint);
            PaintUtils.drawLine(c, 0, y, c.getWidth(), y, fPaint);

            fAppearance.applyText(fPaint);
            c.drawText(i.getValue(), 1f, y - m, fPaint);
        }

    }
        break;
    case ARCS: {
        float length = (float) Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); // euclidean distance
        float r382 = length * 0.382f;
        float r500 = length * 0.5f;
        float r618 = length * 0.618f;

        fAppearance.applyOutline(fPaint);

        int a2 = isRising ? 180 : -180;

        fTempRectF.set(rightMost.x - r500, rightMost.y - r500, rightMost.x + r500, rightMost.y + r500);
        c.drawArc(fTempRectF, 0, a2, false, fPaint);

        fTempRectF.set(rightMost.x - r382, rightMost.y - r382, rightMost.x + r382, rightMost.y + r382);
        c.drawArc(fTempRectF, 0, a2, false, fPaint);

        fTempRectF.set(rightMost.x - r618, rightMost.y - r618, rightMost.x + r618, rightMost.y + r618);
        c.drawArc(fTempRectF, 0, a2, false, fPaint);
    }
        break;
    }
}

From source file:com.ddoskify.CameraOverlayActivity.java

/**
 * Initializes the UI and initiates the creation of a face detector.
 *///from w  w  w.  j av a 2 s . co  m
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);

    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay);
    mFaces = new ArrayList<FaceTracker>();

    final ImageButton button = (ImageButton) findViewById(R.id.flipButton);
    button.setOnClickListener(mFlipButtonListener);

    if (savedInstanceState != null) {
        mIsFrontFacing = savedInstanceState.getBoolean("IsFrontFacing");
    }

    mTakePictureButton = (Button) findViewById(R.id.takePictureButton);
    mTakePictureButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.e("CameraOverlay", "Button has been pressed");
            mCameraSource.takePicture(new CameraSource.ShutterCallback() {
                @Override
                public void onShutter() {
                    //                        mCameraSource.stop();
                    Snackbar.make(findViewById(android.R.id.content), "Picture Taken!", Snackbar.LENGTH_SHORT)
                            .setActionTextColor(Color.BLACK).show();
                }
            }, new CameraSource.PictureCallback() {
                public void onPictureTaken(byte[] data) {
                    int re = ActivityCompat.checkSelfPermission(getApplicationContext(),
                            Manifest.permission.WRITE_EXTERNAL_STORAGE);

                    if (!isStorageAllowed()) {
                        requestStoragePermission();
                    }

                    File pictureFile = getOutputMediaFile();
                    if (pictureFile == null) {
                        return;
                    }
                    try {

                        Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
                        Bitmap resizedBitmap = Bitmap.createBitmap(mGraphicOverlay.getWidth(),
                                mGraphicOverlay.getHeight(), picture.getConfig());
                        Canvas canvas = new Canvas(resizedBitmap);

                        Matrix matrix = new Matrix();

                        matrix.setScale((float) resizedBitmap.getWidth() / (float) picture.getWidth(),
                                (float) resizedBitmap.getHeight() / (float) picture.getHeight());

                        if (mIsFrontFacing) {
                            // mirror by inverting scale and translating
                            matrix.preScale(-1, 1);
                            matrix.postTranslate(canvas.getWidth(), 0);
                        }
                        Paint paint = new Paint();
                        canvas.drawBitmap(picture, matrix, paint);

                        mGraphicOverlay.draw(canvas); // make those accessible

                        FileOutputStream fos = new FileOutputStream(pictureFile);
                        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
                        fos.close();

                        Intent intent = new Intent(getApplicationContext(), PhotoReviewActivity.class);
                        intent.putExtra(BITMAP_MESSAGE, pictureFile.toString());
                        startActivity(intent);
                        Log.d("CameraOverlay", "Starting PhotoReviewActivity " + pictureFile.toString());

                    } catch (FileNotFoundException e) {
                        Log.e("CameraOverlay", e.toString());
                    } catch (IOException e) {
                        Log.e("CameraOverlay", e.toString());
                    }
                }
            });
        }
    });

    // Check for the camera permission before accessing the camera.  If the
    // permission is not granted yet, request permission.
    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) {
        createCameraSource();
    } else {
        requestCameraPermission();
    }
}

From source file:com.skumar.flexibleciruclarseekbar.CircularSeekBar.java

/**
 * Method to draw the dots over needle scale
 *
 * @param canvas drawable element/*  ww  w  .  j  a  v  a2s  .  c  o  m*/
 */
private void drawDotMarker(Canvas canvas) {
    float cx = canvas.getWidth() / 2;
    float cy = canvas.getHeight() / 2;
    float scaleMarkSize = getResources().getDisplayMetrics().density * 10;
    int radius = mArcRadius + mNeedleDistance + mIncreaseCenterNeedle + 15;

    for (float progress = 0; progress <= mMax; progress += 0.2f) {
        float progressSweep = progress / mMax * mSweepAngle;
        int thumbAngle = (int) (mStartAngle + progressSweep + mRotation);

        float stopX = (float) (cx + (radius + scaleMarkSize) * Math.sin(Math.toRadians(thumbAngle)));
        float stopY = (float) (cy - (radius + scaleMarkSize) * Math.cos(Math.toRadians(thumbAngle)));

        canvas.drawCircle(stopX, stopY, mDotSize, mNeedleScalePaint);
    }
}

From source file:com.skumar.flexibleciruclarseekbar.CircularSeekBar.java

/**
 * Method to draw the needle scale/*from  w w  w  .  java 2  s  .c om*/
 *
 * @param canvas drawable element
 */
private void drawNeedleMarkings(Canvas canvas) {
    float cx = canvas.getWidth() / 2;
    float cy = canvas.getHeight() / 2;
    float scaleMarkSize = getResources().getDisplayMetrics().density * mNeedleDP;
    int radius = mArcRadius + mNeedleDistance;

    for (float progress = mProgressIncrement; progress < mMax; progress += mProgressIncrement) {
        float progressSweep = progress / mMax * mSweepAngle;
        int thumbAngle = (int) (mStartAngle + progressSweep + mRotation);
        float startX = (float) (cx + radius * Math.sin(Math.toRadians(thumbAngle)));
        float startY = (float) (cy - radius * Math.cos(Math.toRadians(thumbAngle)));

        float stopX = (float) (cx + (radius + scaleMarkSize) * Math.sin(Math.toRadians(thumbAngle)));
        float stopY = (float) (cy - (radius + scaleMarkSize) * Math.cos(Math.toRadians(thumbAngle)));

        if (progress == mMax / 2 && isIncreaseCenter) {
            stopX = (float) (cx
                    + (radius + scaleMarkSize + mIncreaseCenterNeedle) * Math.sin(Math.toRadians(thumbAngle)));
            stopY = (float) (cy
                    - (radius + scaleMarkSize + mIncreaseCenterNeedle) * Math.cos(Math.toRadians(thumbAngle)));
        }
        if (progress >= mMinimumNeedleScale && progress <= mMaximumNeedleScale && mDrawNeedleScaleUp) {
            stopX = (float) (cx
                    + (radius + scaleMarkSize + mIncreaseCenterNeedle) * Math.sin(Math.toRadians(thumbAngle)));
            stopY = (float) (cy
                    - (radius + scaleMarkSize + mIncreaseCenterNeedle) * Math.cos(Math.toRadians(thumbAngle)));
        }
        canvas.drawLine(startX, startY, stopX, stopY, mNeedleScalePaint);
    }
}

From source file:rosmi.acagild.alarmclock.ringing.ShareFragment.java

private static void drawStamp(Context context, Bitmap bitmap, String question) {
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(bitmap, 0, 0, null);

    float opacity = 0.7f;
    int horizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
            context.getResources().getDisplayMetrics());
    int verticalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
            context.getResources().getDisplayMetrics());
    int textSize = 16; // defined in SP
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16,
            context.getResources().getDisplayMetrics());

    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setBackgroundResource(R.drawable.rounded_corners);
    layout.getBackground().setAlpha((int) (opacity * 255));
    layout.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);

    ImageView logo = new ImageView(context);
    logo.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.ic_launcher));
    layout.addView(logo);// w  w w.  j a  v  a2  s . c  o m

    TextView textView = new TextView(context);
    textView.setVisibility(View.VISIBLE);
    if (question != null) {
        textView.setText(question);
    } else {
        textView.setText("Mimicker");
    }
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
    textView.setPadding(horizontalPadding, 0, 0, 0);

    LinearLayout.LayoutParams centerInParent = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    centerInParent.gravity = Gravity.CENTER_VERTICAL;
    layout.addView(textView, centerInParent);

    layout.measure(canvas.getWidth(), height);
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());

    canvas.translate(horizontalPadding, (float) (canvas.getHeight() * 0.8 - height));
    float scale = Math.min(1.0f, canvas.getWidth() / 1080f);
    canvas.scale(scale, scale);
    layout.draw(canvas);
}

From source file:com.microsoft.mimickeralarm.ringing.ShareFragment.java

private static void drawStamp(Context context, Bitmap bitmap, String question) {
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(bitmap, 0, 0, null);

    float opacity = 0.7f;
    int horizontalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
            context.getResources().getDisplayMetrics());
    int verticalPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
            context.getResources().getDisplayMetrics());
    int textSize = 16; // defined in SP
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16,
            context.getResources().getDisplayMetrics());

    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setBackgroundResource(R.drawable.rounded_corners);
    layout.getBackground().setAlpha((int) (opacity * 255));
    layout.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);

    ImageView logo = new ImageView(context);
    logo.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.ic_launcher_no_bg));
    layout.addView(logo);/*from  w  w w .j a v  a2s. c  o m*/

    TextView textView = new TextView(context);
    textView.setVisibility(View.VISIBLE);
    if (question != null) {
        textView.setText(question);
    } else {
        textView.setText("Mimicker");
    }
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
    textView.setPadding(horizontalPadding, 0, 0, 0);

    LinearLayout.LayoutParams centerInParent = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    centerInParent.gravity = Gravity.CENTER_VERTICAL;
    layout.addView(textView, centerInParent);

    layout.measure(canvas.getWidth(), height);
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());

    canvas.translate(horizontalPadding, (float) (canvas.getHeight() * 0.8 - height));
    float scale = Math.min(1.0f, canvas.getWidth() / 1080f);
    canvas.scale(scale, scale);
    layout.draw(canvas);
}

From source file:com.concavenp.artistrymuse.BaseAppCompatActivity.java

private BitmapImageViewTarget createBitmapImageViewTarget(final ImageView imageView) {

    return new BitmapImageViewTarget(imageView) {
        @Override//from   w  w  w . ja  va  2 s . co  m
        protected void setResource(Bitmap bitmap) {

            int bitmapWidth = bitmap.getWidth();
            int bitmapHeight = bitmap.getHeight();
            int borderWidthHalf = 10;
            int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);
            int newBitmapSquare = bitmapSquareWidth + borderWidthHalf;

            Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquare, newBitmapSquare,
                    Bitmap.Config.ARGB_8888);

            // Initialize a new Canvas to draw empty bitmap
            Canvas canvas = new Canvas(roundedBitmap);

            // Calculation to draw bitmap at the circular bitmap center position
            int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
            int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;

            canvas.drawBitmap(bitmap, x, y, null);

            // Initializing a new Paint instance to draw circular border
            Paint borderPaint = new Paint();
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setStrokeWidth(borderWidthHalf * 2);
            borderPaint.setColor(ResourcesCompat.getColor(getResources(), R.color.myApp_accent_700, null));

            canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquare / 2, borderPaint);

            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),
                    roundedBitmap);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);

        }
    };
}

From source file:com.nextgis.maplibui.overlay.EditLayerOverlay.java

protected void drawCross(Canvas canvas) {
    if (mMode != MODE_EDIT) {
        return;//from w w  w .  ja va 2  s. co m
    }
    mCanvasCenterX = canvas.getWidth() / 2;
    mCanvasCenterY = canvas.getHeight() / 2;

    canvas.drawLine(mCanvasCenterX - mTolerancePX, mCanvasCenterY, mCanvasCenterX + mTolerancePX,
            mCanvasCenterY, mPaint);
    canvas.drawLine(mCanvasCenterX, mCanvasCenterY - mTolerancePX, mCanvasCenterX,
            mCanvasCenterY + mTolerancePX, mPaint);
}

From source file:com.microsoft.mimickeralarm.mimics.MimicTongueTwisterFragment.java

private void createSharableBitmap() {
    Bitmap sharableBitmap = Bitmap.createBitmap(getView().getWidth(), getView().getHeight(),
            Bitmap.Config.ARGB_8888);/*from w  w  w .  j ava  2s. c o  m*/
    Canvas canvas = new Canvas(sharableBitmap);
    canvas.drawColor(ContextCompat.getColor(getContext(), R.color.white));

    // Load the view for the sharable. This will be drawn to the bitmap
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_sharable_tongue_twister, null);

    TextView textView = (TextView) layout.findViewById(R.id.twister_sharable_tongue_twister);
    textView.setText(mQuestion);

    textView = (TextView) layout.findViewById(R.id.twister_sharable_i_said);
    textView.setText(mUnderstoodText);

    textView = (TextView) layout.findViewById(R.id.mimic_twister_share_success);
    textView.setText(mSuccessMessage);

    // Perform the layout using the dimension of the bitmap
    int widthSpec = View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY);
    layout.measure(widthSpec, heightSpec);
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());

    // Draw the generated view to canvas
    layout.draw(canvas);

    String title = getString(R.string.app_short_name) + ": " + getString(R.string.mimic_twister_name);
    mSharableUri = ShareFragment.saveShareableBitmap(getActivity(), sharableBitmap, title);
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * /*from   w w  w. j a v  a 2  s  .c o m*/
 * @param bitmap
 * @return
 */
public static Bitmap createRoundImageWithBorder(Context context, Bitmap bitmap) {
    //
    int bitmapWidth = bitmap.getWidth();
    //
    int bitmapHeight = bitmap.getHeight();
    // pixel
    int borderWidthHalf = 20;

    //??
    int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);

    //?
    int newBitmapSquareWidth = bitmapSquareWidth + borderWidthHalf;

    Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquareWidth, newBitmapSquareWidth,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(roundedBitmap);
    int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
    int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;

    //???,?X,Y?2 ??
    canvas.drawBitmap(bitmap, x / 2, y / 2, null);
    Paint borderPaint = new Paint();
    borderPaint.setStyle(Paint.Style.STROKE);
    borderPaint.setStrokeWidth(borderWidthHalf);
    borderPaint.setColor(Color.WHITE);

    //
    canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquareWidth / 2, borderPaint);

    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(),
            roundedBitmap);
    roundedBitmapDrawable.setGravity(Gravity.CENTER);
    roundedBitmapDrawable.setCircular(true);
    return getBitmapFromDrawable(roundedBitmapDrawable);
}