【问题标题】:Draw my drawable Image on canvas?在画布上绘制我的可绘制图像?
【发布时间】:2014-02-10 06:26:33
【问题描述】:

如何在画布上绘制我的可绘制图像?我从资源中获取我的图像并在画布上绘制该图像。有可能我们可以声明多个画布并将所有画布设置为一个吗? 我已经在画布上画了一张图片,想在上面画一个可绘制的图片吗?

private Bitmap drawtextonimage(Bitmap bitmap, String text, String caption) {
    caption = caption.replace("\n", " ");

声明 Canvas 和 Paint ?

    Canvas cs = new Canvas(bitmap);
    Paint tPaint = new Paint();
    Paint captionPaint = new Paint();

    if (text.equals("Good")) {
        tPaint.setColor(Color.GREEN);
    } else {
        tPaint.setColor(Color.RED);
    }

    tPaint.setStyle(Style.FILL);
    tPaint.setTextSize(30);

    //

    captionPaint.setColor(Color.CYAN);
    captionPaint.setAlpha(100);
    captionPaint.setTextSize(25);
    captionPaint.setTextScaleX((float) 0.7);
    captionPaint.setTextAlign(Align.CENTER);
    captionPaint.setTypeface(Typeface.SERIF);
    captionPaint.setShadowLayer(1f, 0, 1f, Color.WHITE);

    /*Canvas cs1 = new Canvas(bitmap);

        cs1.drawRect(0, bitmap.getHeight(), bitmap.getWidth(), 500, tPaint);

       // canvas.drawRect(33, 33, 77, 60, paint );
        Paint zPaint = new Paint();
         cs1.drawARGB((int) 0.5,54,54,200);*/ 

//         cs.drawText(text, 60, 60, tPaint);

此方法获取我的可绘制图像并在画布上绘制,但它不起作用?

     Resources res = getResources();
     Bitmap bitmapx = BitmapFactory.decodeResource(res, R.drawable.overlay_good_full);
     Bitmap bitmapxx = BitmapFactory.decodeResource(res, R.drawable.overlay_bad_full);
    if(text.equals("Good"))
    {


        cs.drawBitmap(bitmapx, 0, 0, new Paint());
        //cs.drawBitmap(bitmapx, 0, 0, tPaint);

    }
    else
    {
         // cs.drawBitmap(bitmapxx, 0, 0, tPaint);

    }

    cs.drawText(caption, (bitmap.getWidth() / 2), bitmap.getHeight()
            , captionPaint);




//  canvas.drawBitmap(image, 0, 0, null);


//  Log.i("Caption", caption);
    return bitmap;

}

【问题讨论】:

  • 不工作是什么意思?你有任何异常吗?如果它不起作用,你需要先检查你的 text.equals("Good") 条件是否起作用。
  • 试试 cs.setBitMap(bitmapx);相反...
  • 不,它没有显示可绘制的图像。
  • 尝试在drawBitmap方法中设置图片的宽高
  • 你需要在 drawBitmap() 中设置一些有效值而不是你的 0, 0。

标签: android bitmap android-canvas android-drawable


【解决方案1】:

以下代码将从资源中绘制一个 Drawable 到从位图创建的画布上(在本例中是来自相机预览的位图)。这已经过测试并适用于 API 22+(尚未在早期版本中测试过):

            Bitmap cameraBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length, opt);
            Canvas camImgCanvas = new Canvas(cameraBitmap);
            Drawable d = ContextCompat.getDrawable(getActivity(), R.drawable.myDrawable);
            //Centre the drawing
            int bitMapWidthCenter = cameraBitmap.getWidth()/2;
            int bitMapheightCenter = cameraBitmap.getHeight()/2;
            d.setBounds(bitMapWidthCenter, bitMapheightCenter, bitMapWidthCenter+d.getIntrinsicWidth(),
                    bitMapheightCenter+d.getIntrinsicHeight());
            //And draw it...
            d.draw(camImgCanvas);

【讨论】:

  • @LiNKeR - 这是相同的方法 - 这就是你要强调的吗?这里唯一的区别是画布是从相机预览中的位图创建的,并且边界是通过编程计算的。
猜你喜欢
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2011-01-11
  • 2013-07-20
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多