Java Code Examples for android.graphics.Canvas#drawColor()

The following examples show how to use android.graphics.Canvas#drawColor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadProgressButton.java    From Bailan with Apache License 2.0 6 votes vote down vote up
private void drawProgressRectWithClip(Canvas canvas) {
    mPaint.setColor(mProgressBarColor);
    mPaint.setStyle(Paint.Style.FILL);
    //根据进度比率计算出当前的进度值对应的宽度
    int progress = (int) (mValidWidth * (getProgress() * 1.0f / getMax()));
    canvas.save();
    canvas.translate(getPaddingLeft(), getPaddingTop());
    //裁剪圆角矩形路径
    drawRoundRectPath();
    canvas.clipPath(mRoundRectPath);//裁剪之后此时画布就变成了裁剪之后的圆角矩形
    //裁剪进度路径
    drawProgressPath(progress);
    canvas.clipPath(mProgressPath, Region.Op.INTERSECT);
    canvas.drawColor(mProgressBarColor);
    canvas.restore();
}
 
Example 2
Source File: MultiLevelPie.java    From numAndroidCharts with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

    canvas.drawColor(Color.TRANSPARENT);
    piePaint.reset();
    piePaint.setAntiAlias(true);



    midX = getWidth() / 2;
    midY = getHeight() / 2;

    if (midX < midY) {
        radius = midX;
    } else {
        radius = midY;
    }

    innerRadius = radius / 2;

    for (int i = 0; i < this.data.size(); i++){

        Float[] val = this.data.get(i).getY_List();
        drawChart(val, canvas, radius/(i+1));
    }
}
 
Example 3
Source File: IndicatorView.java    From POCenter with MIT License 6 votes vote down vote up
protected void onDraw(Canvas canvas) {
	if (count <= 1) {
		this.setVisibility(View.GONE);
		return;
	}
	float height = getHeight();
	float radius = height * DESIGN_INDICATOR_RADIUS / DESIGN_BOTTOM_HEIGHT;
	float distance = height * DESIGN_INDICATOR_DISTANCE / DESIGN_BOTTOM_HEIGHT;
	float windowWidth = radius * 2 * count + distance * (count - 1);
	float left = (getWidth() - windowWidth) / 2;
	float cy = height / 2;

	canvas.drawColor(0xffffffff);
	Paint paint = new Paint();
	paint.setAntiAlias(true);
	for (int i = 0; i < count; i++) {
		if (i == current) {
			paint.setColor(0xff5d71a0);
		} else {
			paint.setColor(0xffafb1b7);
		}
		float cx = left + (radius * 2 + distance) * i;
		canvas.drawCircle(cx, cy, radius, paint);
	}
}
 
Example 4
Source File: Chart.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
/**
 * Returns the bitmap that represents the chart.
 *
 * @return
 */
public Bitmap getChartBitmap() {
    // Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
    // Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    // Get the view's background
    Drawable bgDrawable = getBackground();
    if (bgDrawable != null)
        // has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else
        // does not have background drawable, then draw white background on
        // the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    draw(canvas);
    // return the bitmap
    return returnedBitmap;
}
 
Example 5
Source File: BitmapUtils.java    From mine-android-repository with Apache License 2.0 6 votes vote down vote up
public static Bitmap addPadding(Bitmap bmp, int color) {

        if (bmp == null) {
            return null;
        }

        int biggerParam = Math.max(bmp.getWidth(), bmp.getHeight());
        Bitmap bitmap = Bitmap.createBitmap(biggerParam, biggerParam, bmp.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(color);

        int top = bmp.getHeight() > bmp.getWidth() ? 0 : (bmp.getWidth() - bmp.getHeight())/2;
        int left = bmp.getWidth() > bmp.getHeight() ? 0 : (bmp.getHeight() - bmp.getWidth())/2;

        canvas.drawBitmap(bmp, left, top, null);
        return bitmap;
    }
 
Example 6
Source File: DanMuSurfaceView.java    From LLApp with Apache License 2.0 6 votes vote down vote up
public void lockDraw() {
    if (!isSurfaceCreated) {
        return;
    }
    Canvas canvas = mSurfaceHolder.lockCanvas();
    if (canvas == null) {
        return;
    }

    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

    if (danMuController != null) {
        danMuController.draw(canvas);
    }

    if (isSurfaceCreated) {
        mSurfaceHolder.unlockCanvasAndPost(canvas);
    }
}
 
Example 7
Source File: SelectorBorderDrawable.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
	if (selected) {
		canvas.drawColor(0x44ffffff);
		Rect bounds = getBounds();
		int thickness = (int) (THICKNESS_DP * density);
		canvas.drawRect(bounds.top, bounds.left, bounds.right, bounds.top + thickness, paint);
		canvas.drawRect(bounds.bottom - thickness, bounds.left, bounds.right, bounds.bottom, paint);
		canvas.drawRect(bounds.top, bounds.left, bounds.left + thickness, bounds.bottom, paint);
		canvas.drawRect(bounds.top, bounds.right - thickness, bounds.right, bounds.bottom, paint);
	}
}
 
Example 8
Source File: SwipeBackLayout.java    From hipda with GNU General Public License v2.0 5 votes vote down vote up
private void drawScrim(Canvas canvas, View child) {
    final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
    final int alpha = (int) (baseAlpha * mScrimOpacity);
    final int color = alpha << 24 | (mScrimColor & 0xffffff);

    if ((mTrackingEdge & EDGE_LEFT) != 0) {
        canvas.clipRect(0, 0, child.getLeft(), getHeight());
    } else if ((mTrackingEdge & EDGE_RIGHT) != 0) {
        canvas.clipRect(child.getRight(), 0, getRight(), getHeight());
    } else if ((mTrackingEdge & EDGE_BOTTOM) != 0) {
        canvas.clipRect(child.getLeft(), child.getBottom(), getRight(), getHeight());
    }
    canvas.drawColor(color);
}
 
Example 9
Source File: VisualizerView.java    From android-dev-challenge with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mBytes == null) {
        return;
    }

    // Get the current angle all of the shapes are at
    double currentAngleRadians = calcCurrentAngle();

    // Draw the background
    canvas.drawColor(backgroundColor);

    // Draw each shape
    if (showBass) {
        mBassCircle.draw(canvas, bass, currentAngleRadians);
    }
    if (showMid) {
        mMidSquare.draw(canvas, mid, currentAngleRadians);
    }
    if (showTreble) {
        mTrebleTriangle.draw(canvas, treble, currentAngleRadians);
    }

    // Invalidate the view to immediately redraw
    invalidate();
}
 
Example 10
Source File: SwipeBackLayout.java    From imsdk-android with MIT License 5 votes vote down vote up
private void drawScrim(Canvas canvas, View child) {
    final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
    final int alpha = (int) (baseAlpha * mScrimOpacity);
    final int color = alpha << 24 | (mScrimColor & 0xffffff);

    if ((mTrackingEdge & EDGE_LEFT) != 0) {
        canvas.clipRect(0, 0, child.getLeft(), getHeight());
    } else if ((mTrackingEdge & EDGE_RIGHT) != 0) {
        canvas.clipRect(child.getRight(), 0, getRight(), getHeight());
    } else if ((mTrackingEdge & EDGE_BOTTOM) != 0) {
        canvas.clipRect(child.getLeft(), child.getBottom(), getRight(), getHeight());
    }
    canvas.drawColor(color);
}
 
Example 11
Source File: LocalVideoView.java    From sealrtc-android with MIT License 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
    if (rectF.width() > 0) {
        canvas.drawRect(rectF, paint);
        dismissRectDelayed(DISPLAY_TIME_MILLIS);
    }
}
 
Example 12
Source File: DrawingBoardView.java    From PhotoEdit with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas)
{
	super.onDraw(canvas);
	canvas.drawColor(Color.WHITE);
	if(backgroundBitmap != null && !backgroundBitmap.isRecycled()){
		canvas.drawBitmap(backgroundBitmap, 0, 0, null);
	}

	if(paintBitmap != null && !paintBitmap.isRecycled()){
		canvas.drawBitmap(paintBitmap, 0, 0, null);
	}

}
 
Example 13
Source File: ColorView.java    From ColorPickerDialog with Apache License 2.0 5 votes vote down vote up
@Override
public void render(Canvas canvas) {
    if (Color.alpha(color) < 255) {
        int outline = Math.round(outlineSize) * 4;
        for (int x = 0; x < canvas.getWidth(); x += outline) {
            for (int y = x % (outline * 2) == 0 ? 0 : outline; y < canvas.getWidth(); y += (outline * 2)) {
                canvas.drawRect(x, y, x + outline, y + outline, tilePaint);
            }
        }
    }

    canvas.drawColor(color);
}
 
Example 14
Source File: PageLoader.java    From MyBookshelf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 横翻模式绘制背景
 */
private synchronized void drawBackground(Bitmap bitmap, TxtChapter txtChapter, TxtPage txtPage) {
    if (bitmap == null) return;
    Canvas canvas = new Canvas(bitmap);
    if (!readBookControl.bgIsColor() && !readBookControl.bgBitmapIsNull()) {
        Rect mDestRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(readBookControl.getBgBitmap(), null, mDestRect, null);
    } else {
        canvas.drawColor(readBookControl.getBgColor());
    }
    drawBackground(canvas, txtChapter, txtPage);
}
 
Example 15
Source File: CameraBridgeViewBase.java    From FaceDetectDemo with Apache License 2.0 4 votes vote down vote up
/**
 * This method shall be called by the subclasses when they have valid
 * object and want it to be delivered to external client (via callback) and
 * then displayed on the screen.
 * @param frame - the current frame to be delivered
 */
protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
    Mat modified;

    if (mListener != null) {
        modified = mListener.onCameraFrame(frame);
    } else {
        modified = frame.rgba();
    }

    boolean bmpValid = true;
    if (modified != null) {
        try {
            Utils.matToBitmap(modified, mCacheBitmap);
        } catch(Exception e) {
            Log.e(TAG, "Mat type: " + modified);
            Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight());
            Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
            bmpValid = false;
        }
    }

    if (bmpValid && mCacheBitmap != null) {
        Canvas canvas = getHolder().lockCanvas();
        if (canvas != null) {
            canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
            if (BuildConfig.DEBUG)
                Log.d(TAG, "mStretch value: " + mScale);

            if (mScale != 0) {
                canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
                     new Rect((int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2),
                     (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2),
                     (int)((canvas.getWidth() - mScale*mCacheBitmap.getWidth()) / 2 + mScale*mCacheBitmap.getWidth()),
                     (int)((canvas.getHeight() - mScale*mCacheBitmap.getHeight()) / 2 + mScale*mCacheBitmap.getHeight())), null);
            } else {
                 canvas.drawBitmap(mCacheBitmap, new Rect(0,0,mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
                     new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,
                     (canvas.getHeight() - mCacheBitmap.getHeight()) / 2,
                     (canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(),
                     (canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null);
            }

            if (mFpsMeter != null) {
                mFpsMeter.measure();
                mFpsMeter.draw(canvas, 20, 30);
            }
            getHolder().unlockCanvasAndPost(canvas);
        }
    }
}
 
Example 16
Source File: MinuteChartView.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(mBackgroundColor);
        if (mWidth == 0 || mHeight == 0 || mPoints == null || mPoints.size() == 0) {
            return;
        }
        drawGird(canvas);
        if (mPoints.size() > 0) {
            IMinuteLine lastPoint = mPoints.get(0);
            float lastX = getX(0);
            for (int i = 0; i < mPoints.size(); i++) {
                IMinuteLine curPoint = mPoints.get(i);
                float curX = getX(i);
                canvas.drawLine(lastX, getY(lastPoint.getLast()), curX, getY(curPoint.getLast()), mPricePaint);
//                canvas.drawLine(lastX, getY(lastPoint.getAvgPrice()), curX, getY(curPoint.getAvgPrice()), mAvgPaint);
                //成交量
                Paint volumePaint = ((i == 0 && curPoint.getLast() <= mValueStart) || curPoint.getLast() <= lastPoint.getLast()) ? mVolumePaintGreen : mVolumePaintRed;
                canvas.drawLine(curX, getVolumeY(0), curX, getVolumeY(curPoint.getVolume()), volumePaint);
                lastPoint = curPoint;
                lastX = curX;
            }
        }
        drawText(canvas);
        //画指示线
        if (isLongPress) {
            IMinuteLine point = mPoints.get(selectedIndex);
            float x = getX(selectedIndex);
            canvas.drawLine(x, 0, x, mHeight + mVolumeHeight, mTextPaint);
            canvas.drawLine(0, getY(point.getLast()), mWidth, getY(point.getLast()), mTextPaint);
            //画指示线的时间
            String text = DateUtil.shortTimeFormat.format(point.getDate());
            x = x - mTextPaint.measureText(text) / 2;
            if (x < 0) {
                x = 0;
            }
            if (x > mWidth - mTextPaint.measureText(text)) {
                x = mWidth - mTextPaint.measureText(text);
            }
            Paint.FontMetrics fm = mTextPaint.getFontMetrics();
            float textHeight = fm.descent - fm.ascent;
            float baseLine = (textHeight - fm.bottom - fm.top) / 2;
            //下方时间
            mTextPaint.setColor(ContextCompat.getColor(getContext(), R.color.chart_time));//长按线颜色
            canvas.drawRect(x, mHeight + mVolumeHeight - baseLine + textHeight, x + mTextPaint.measureText(text), mVolumeHeight + mHeight + baseLine, mBackgroundPaint);
            canvas.drawText(text, x, mHeight + mVolumeHeight + baseLine, mTextPaint);

            float r = textHeight / 2;
            float y = getY(point.getLast());
            //左方值
            text = floatToString(point.getLast());
            canvas.drawRect(0, y - r, mTextPaint.measureText(text), y + r, mBackgroundPaint);
            canvas.drawText(text, 0, fixTextY(y), mTextPaint);

            //右方值
            text = floatToString((point.getLast() - mValueStart) * 100f / mValueStart) + "%";
            canvas.drawRect(mWidth - mTextPaint.measureText(text), y - r, mWidth, y + r, mBackgroundPaint);
            canvas.drawText(text, mWidth - mTextPaint.measureText(text), fixTextY(y), mTextPaint);

            drawSelector(selectedIndex, point, canvas);
        }
        drawValue(canvas, isLongPress ? selectedIndex : mPoints.size() - 1);
    }
 
Example 17
Source File: ColorStateListDrawable.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
    canvas.drawColor(list.getColorForState(getState(), list.getDefaultColor()));
}
 
Example 18
Source File: ColorView.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(mColor);
}
 
Example 19
Source File: CommonUtil.java    From Pic2Ascii with MIT License 3 votes vote down vote up
public static Bitmap textAsBitmap(StringBuilder text, Context context) {
        TextPaint textPaint = new TextPaint();
        textPaint.setColor(Color.GRAY);
        textPaint.setAntiAlias(true);
        textPaint.setTypeface(Typeface.MONOSPACE);
        textPaint.setTextSize(12);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         //

        StaticLayout layout = new StaticLayout(text, textPaint, width,

                Layout.Alignment.ALIGN_CENTER, 1f, 0.0f, true);

        Bitmap bitmap = Bitmap.createBitmap(layout.getWidth() + 20,

                layout.getHeight() + 20, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);

        canvas.translate(10, 10);

        canvas.drawColor(Color.WHITE);

//        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);//绘制透明色

        layout.draw(canvas);

        return bitmap;

    }
 
Example 20
Source File: paletteUtils.java    From Color-picker-library with GNU General Public License v3.0 2 votes vote down vote up
private static RoundedBitmapDrawable createRoundedBitmapDrawableWithBorder(Activity activity, Bitmap bitmap, int tagColor, int viewColor, Resources mResources) {

        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        int borderWidthHalf = 10; // In pixels

        // Calculate the bitmap radius
        int bitmapRadius = Math.min(bitmapWidth, bitmapHeight) / 2;

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

        int bitmapDim = bitmapSquareWidth + borderWidthHalf;

        // Initializing a new empty bitmap.
        Bitmap roundedBitmap = Bitmap.createBitmap(bitmapDim, bitmapDim, Bitmap.Config.ARGB_8888);

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

        // Draw a solid color to canvas
        canvas.drawColor(tagColor);

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

        // Draw the bitmap to canvas
        // Bitmap will draw its center to circular bitmap center by keeping border spaces
        canvas.drawBitmap(bitmap, x, y, null);

        // Initializing a new Paint instance to draw circular border
        final Paint borderPaint = new Paint();
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setAntiAlias(true);
        borderPaint.setStrokeWidth(borderWidthHalf);

        // Set paint color according to view color to make it always visible
        double darkness = 1 - (0.299 * Color.red(viewColor) + 0.587 * Color.green(viewColor) + 0.114 * Color.blue(viewColor)) / 255;

        if (darkness < 0.5) {

            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {

                    borderPaint.setColor(colorUtils.opaqueColor(Color.DKGRAY));

                }
            });

        } else {

            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    borderPaint.setColor(colorUtils.opaqueColor(Color.LTGRAY));


                }
            });
        }

        // drawCircle(float cx, float cy, float radius, Paint paint)
        // Draw the specified circle using the specified paint.
        canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, bitmapDim / 2, borderPaint);

        // Create a new RoundedBitmapDrawable
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mResources, roundedBitmap);

        // Set the corner radius of the bitmap drawable
        roundedBitmapDrawable.setCornerRadius(bitmapRadius);

        roundedBitmapDrawable.setCircular(true);

        roundedBitmapDrawable.setAntiAlias(true);

        // Return the RoundedBitmapDrawable
        return roundedBitmapDrawable;
    }