當前位置: 首頁>>代碼示例>>Java>>正文


Java Canvas類代碼示例

本文整理匯總了Java中android.graphics.Canvas的典型用法代碼示例。如果您正苦於以下問題:Java Canvas類的具體用法?Java Canvas怎麽用?Java Canvas使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Canvas類屬於android.graphics包,在下文中一共展示了Canvas類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: createBitmapFromView

import android.graphics.Canvas; //導入依賴的package包/類
/**
 * 從一個view創建Bitmap。
 * 注意點:繪製之前要清掉 View 的焦點,因為焦點可能會改變一個 View 的 UI 狀態。
 * 來源:https://github.com/tyrantgit/ExplosionField
 *
 * @param view  傳入一個 View,會獲取這個 View 的內容創建 Bitmap。
 * @param scale 縮放比例,對創建的 Bitmap 進行縮放,數值支持從 0 到 1。
 */
public static Bitmap createBitmapFromView(View view, float scale) {
    if (view instanceof ImageView) {
        Drawable drawable = ((ImageView) view).getDrawable();
        if (drawable != null && drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
    }
    view.clearFocus();
    Bitmap bitmap = createBitmapSafely((int) (view.getWidth() * scale),
            (int) (view.getHeight() * scale), Bitmap.Config.ARGB_8888, 1);
    if (bitmap != null) {
        synchronized (sCanvas) {
            Canvas canvas = sCanvas;
            canvas.setBitmap(bitmap);
            canvas.save();
            canvas.drawColor(Color.WHITE); // 防止 View 上麵有些區域空白導致最終 Bitmap 上有些區域變黑
            canvas.scale(scale, scale);
            view.draw(canvas);
            canvas.restore();
            canvas.setBitmap(null);
        }
    }
    return bitmap;
}
 
開發者ID:QMUI,項目名稱:QMUI_Android,代碼行數:33,代碼來源:QMUIDrawableHelper.java

示例2: renderShape

import android.graphics.Canvas; //導入依賴的package包/類
@Override
public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler,
                        float posX, float posY, Paint renderPaint) {

    final float shapeHalf = dataSet.getScatterShapeSize() / 2f;

    renderPaint.setStyle(Paint.Style.STROKE);
    renderPaint.setStrokeWidth(Utils.convertDpToPixel(1f));

    c.drawLine(
            posX - shapeHalf,
            posY,
            posX + shapeHalf,
            posY,
            renderPaint);
    c.drawLine(
            posX,
            posY - shapeHalf,
            posX,
            posY + shapeHalf,
            renderPaint);

}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:24,代碼來源:CrossShapeRenderer.java

示例3: renderAxisLine

import android.graphics.Canvas; //導入依賴的package包/類
@Override
public void renderAxisLine(Canvas c) {

    if (!mXAxis.isDrawAxisLineEnabled() || !mXAxis.isEnabled())
        return;

    mAxisLinePaint.setColor(mXAxis.getAxisLineColor());
    mAxisLinePaint.setStrokeWidth(mXAxis.getAxisLineWidth());

    if (mXAxis.getPosition() == XAxisPosition.TOP
            || mXAxis.getPosition() == XAxisPosition.TOP_INSIDE
            || mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) {
        c.drawLine(mViewPortHandler.contentRight(),
                mViewPortHandler.contentTop(), mViewPortHandler.contentRight(),
                mViewPortHandler.contentBottom(), mAxisLinePaint);
    }

    if (mXAxis.getPosition() == XAxisPosition.BOTTOM
            || mXAxis.getPosition() == XAxisPosition.BOTTOM_INSIDE
            || mXAxis.getPosition() == XAxisPosition.BOTH_SIDED) {
        c.drawLine(mViewPortHandler.contentLeft(),
                mViewPortHandler.contentTop(), mViewPortHandler.contentLeft(),
                mViewPortHandler.contentBottom(), mAxisLinePaint);
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:26,代碼來源:XAxisRendererHorizontalBarChart.java

示例4: draw

import android.graphics.Canvas; //導入依賴的package包/類
@Override
public void draw(Canvas canvas, Paint paint) {
    float circleSpacing=4;
    float radius=(getWidth()-circleSpacing*4)/6;
    float x = getWidth()/ 2-(radius*2+circleSpacing);
    float y = getWidth()/ 2-(radius*2+circleSpacing);

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            canvas.save();
            float translateX=x+(radius*2)*j+circleSpacing*j;
            float translateY=y+(radius*2)*i+circleSpacing*i;
            canvas.translate(translateX, translateY);
            canvas.scale(scaleFloats[3 * i + j], scaleFloats[3 * i + j]);
            paint.setAlpha(alphas[3 * i + j]);
            canvas.drawCircle(0, 0, radius, paint);
            canvas.restore();
        }
    }
}
 
開發者ID:leobert-lan,項目名稱:UiLib,代碼行數:21,代碼來源:BallGridPulseIndicator.java

示例5: onDraw

import android.graphics.Canvas; //導入依賴的package包/類
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mViewPager == null) {
        return;
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

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

    final int paddingLeft = getPaddingLeft();
    final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count);
    final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset);
    final float right = left + pageWidth;
    final float top = getPaddingTop();
    final float bottom = getHeight() - getPaddingBottom();
    canvas.drawRect(left, top, right, bottom, mPaint);
}
 
開發者ID:fikyair,項目名稱:musicplayer,代碼行數:26,代碼來源:UnderlinePageIndicator.java

示例6: onDraw

import android.graphics.Canvas; //導入依賴的package包/類
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mPath.reset();
    //貝塞爾曲線
    mPath.moveTo(mStartXPoint, mStartYPoint);
    mPath.quadTo(mConXPoint, mConYPoint, mEndXPoint, mEndYPoint);
    canvas.drawPath(mPath, mPaint);

    //輔助線
    canvas.drawLine(mStartXPoint, mStartYPoint, mConXPoint, mConYPoint, mLinePaint);
    canvas.drawLine(mConXPoint, mConYPoint, mEndXPoint, mEndYPoint, mLinePaint);

    //文字
    canvas.drawPoint(mStartXPoint, mStartYPoint, mPaint);
    canvas.drawText("起始點", mStartXPoint, mStartYPoint + 30, mTextPaint);
    canvas.drawPoint(mEndXPoint, mEndYPoint, mPaint);
    canvas.drawText("結束點", mEndXPoint, mEndYPoint + 30, mTextPaint);
    canvas.drawPoint(mConXPoint, mConYPoint, mPaint);
    canvas.drawText("控製點", mConXPoint, mConYPoint - 30, mTextPaint);

}
 
開發者ID:Lichenwei-Dev,項目名稱:BezierView,代碼行數:23,代碼來源:BezierQuadView.java

示例7: circleBitmap

import android.graphics.Canvas; //導入依賴的package包/類
public static Bitmap circleBitmap(Bitmap source) {
    //獲取圖片的寬度
    int width = source.getWidth();
    //創建一個與source等寬的Bitmap對象
    Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
    //創建等大小的畫布
    Canvas canvas = new Canvas(bitmap);
    //繪製一個圓圈:將此圓圈理解為下層圖片
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    canvas.drawCircle(width / 2, width / 2, width / 2, paint);

    //設置圖片相交情況下的處理方式
    //setXfermode:設置當繪製的圖像出現相交情況時候的處理方式的,它包含的常用模式有哪幾種
    //PorterDuff.Mode.SRC_IN 取兩層圖像交集部門,隻顯示上層圖像,注意這裏是指取相交叉的部分,然後顯示上層圖像
    //PorterDuff.Mode.DST_IN 取兩層圖像交集部門,隻顯示下層圖像
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    //source:理解為上層圖片
    canvas.drawBitmap(source, 0, 0, paint);

    return bitmap;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:23,代碼來源:BitmapUtils.java

示例8: onDraw

import android.graphics.Canvas; //導入依賴的package包/類
@Override
protected void onDraw(Canvas canvas) {
    int w = getWidth();
    int h = getHeight();

    int cx = w / 2;
    int cy = (int) (h / 2f - getShadowDy());

    int radius = (int) (Math.min(w, h) / 2f - shadowSize);

    canvas.drawCircle(cx, cy, radius, bgPaint);

    if (icon != null) {
        final int targetW = getIconSize(2 * radius, icon.getIntrinsicWidth());
        final int targetH = getIconSize(2 * radius, icon.getIntrinsicHeight());
        icon.setBounds(cx - targetW / 2, cy - targetH / 2, cx + targetW / 2, cy + targetH / 2);
        icon.draw(canvas);
    }
}
 
開發者ID:roshakorost,項目名稱:Phial,代碼行數:20,代碼來源:PhialButton.java

示例9: onDraw

import android.graphics.Canvas; //導入依賴的package包/類
@Override
public void onDraw(Canvas canvas) {
    if (mModel == null) {
        return;
    }
    if (!mSetuped) {
        setup();
    }
    if (mOffscreenBitmap == null) {
        return;
    }

    int startIndex = mDrawnLineSize - 1;
    if (startIndex < 0) {
        startIndex = 0;
    }

    DrawRenderer.renderModel(mOffscreenCanvas, mModel, mPaint, startIndex);
    canvas.drawBitmap(mOffscreenBitmap, mMatrix, mPaint);

    mDrawnLineSize = mModel.getLineSize();
}
 
開發者ID:elimu-ai,項目名稱:egma-handwriting-numbers,代碼行數:23,代碼來源:DrawView.java

示例10: drawHorizontal

import android.graphics.Canvas; //導入依賴的package包/類
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
    final int childSize = parent.getChildCount();
    //childSize-1  最後一個item不顯示分割線
    for (int i = 0; i < childSize-1; i++) {
        final View child = parent.getChildAt(i);
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
        final int top = child.getBottom() + layoutParams.bottomMargin;
        final int bottom = top + mDividerHeight;
        if (mDivider != null) {
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(canvas);
        }
        if (mPaint != null) {
            canvas.drawRect(left, top, right, bottom, mPaint);
        }
    }
}
 
開發者ID:yangchong211,項目名稱:YCSlideView,代碼行數:20,代碼來源:RecycleViewItemLine.java

示例11: drawYLabels

import android.graphics.Canvas; //導入依賴的package包/類
/**
 * draws the y-labels on the specified x-position
 *
 * @param fixedPosition
 * @param positions
 */
@Override
protected void drawYLabels(Canvas c, float fixedPosition, float[] positions, float offset) {

    mAxisLabelPaint.setTypeface(mYAxis.getTypeface());
    mAxisLabelPaint.setTextSize(mYAxis.getTextSize());
    mAxisLabelPaint.setColor(mYAxis.getTextColor());

    final int from = mYAxis.isDrawBottomYLabelEntryEnabled() ? 0 : 1;
    final int to = mYAxis.isDrawTopYLabelEntryEnabled()
            ? mYAxis.mEntryCount
            : (mYAxis.mEntryCount - 1);

    for (int i = from; i < to; i++) {

        String text = mYAxis.getFormattedLabel(i);

        c.drawText(text, positions[i * 2], fixedPosition - offset, mAxisLabelPaint);
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:26,代碼來源:YAxisRendererHorizontalBarChart.java

示例12: drawableToBitmap

import android.graphics.Canvas; //導入依賴的package包/類
/**
 * 確保drawable是能夠拿到寬高的drawable
 *
 * @param drawable
 * @return
 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable == null
            && drawable.getIntrinsicWidth() > 0
            && drawable.getIntrinsicHeight() > 0) {
        return null;
    }
    Bitmap bitmap = Bitmap
            .createBitmap(
                    drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(),
                    drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                            : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight());
    Log.i("Utilities",
            "drawableToBitmap drawable.getIntrinsicWidth()=" + drawable.getIntrinsicWidth()
                    + ",drawable.getIntrinsicHeight()="
                    + drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}
 
開發者ID:halohoop,項目名稱:RollSquareView,代碼行數:29,代碼來源:Utils.java

示例13: addTextWatermark

import android.graphics.Canvas; //導入依賴的package包/類
/**
 * 添加文字水印
 *
 * @param src      源圖片
 * @param content  水印文本
 * @param textSize 水印字體大小
 * @param color    水印字體顏色
 * @param x        起始坐標x
 * @param y        起始坐標y
 * @param recycle  是否回收
 * @return 帶有文字水印的圖片
 */
public static Bitmap addTextWatermark(final Bitmap src,
                                      final String content,
                                      final float textSize,
                                      final int color,
                                      final float x,
                                      final float y,
                                      final boolean recycle) {
    if (isEmptyBitmap(src) || content == null) return null;
    Bitmap ret = src.copy(src.getConfig(), true);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Canvas canvas = new Canvas(ret);
    paint.setColor(color);
    paint.setTextSize(textSize);
    Rect bounds = new Rect();
    paint.getTextBounds(content, 0, content.length(), bounds);
    canvas.drawText(content, x, y + textSize, paint);
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
開發者ID:Wilshion,項目名稱:HeadlineNews,代碼行數:32,代碼來源:ImageUtils.java

示例14: drawSky

import android.graphics.Canvas; //導入依賴的package包/類
private void drawSky(Canvas canvas, int width, int height) {
        Matrix matrix = mMatrix;
        matrix.reset();

        int bWidth = mDrawableSky.width();//mSky.getWidth();
        int bHeight = mDrawableSky.height();//mSky.getHeight();
        float townScale = 1f * width / bWidth;
        float offsetx = 0;
        float offsety = height / 2 - bHeight / 2;

//        matrix.postScale(townScale, townScale);
//        matrix.postTranslate(offsetx, offsety);
//
//        canvas.drawBitmap(mSky, matrix, null);

        final int saveCount = canvas.getSaveCount();
        canvas.save();
        canvas.translate(offsetx, offsety);
        matrix.postScale(townScale, townScale);
        mDrawableSky.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
 
開發者ID:scwang90,項目名稱:SmartRefreshLayout,代碼行數:23,代碼來源:PhoenixHeader.java

示例15: drawable2Bitmap

import android.graphics.Canvas; //導入依賴的package包/類
private static Bitmap drawable2Bitmap(Drawable drawable) {
    if (drawable == null) {
        return null;
    }
    // 取 drawable 的長寬
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    // 取 drawable 的顏色格式
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;
    // 建立對應 bitmap
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    // 建立對應 bitmap 的畫布
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    // 把 drawable 內容畫到畫布中
    drawable.draw(canvas);
    return bitmap;
}
 
開發者ID:GcsSloop,項目名稱:diycode,代碼行數:20,代碼來源:ACache.java


注:本文中的android.graphics.Canvas類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。