当前位置: 首页>>代码示例>>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;未经允许,请勿转载。