在画布上绘制对象/图像 [英] draw object/image on canvas

查看:41
本文介绍了在画布上绘制对象/图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有另一种在 android 中的画布上绘制对象的方法?

Is there another way to draw an object on a canvas in android?

draw() 中的这段代码不起作用:

This code inside draw() doesn't work:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);

实际上,它正在处理我的第一个代码,但是当我将它转移到另一个名为 MarkOverlay 的类时,它不再工作了.

Well actually, it's working on my 1st code but when I've transfered this to another class called MarkOverlay, it's not working anymore.

  markerOverlay = new MarkerOverlay(getApplicationContext(), p);
                      listOfOverlays.add(markerOverlay);  

我应该向 MarkerOverlay 传递什么参数才能使此代码工作?错误出现在 getResources() 中.

What parameter should I pass to MarkerOverlay to make this code work? The error is somewhere in getResources().

仅供参考,canvas.drawOval 工作正常,但我真的想绘制图像而不是椭圆形.:)

FYI, canvas.drawOval is perfectly working but I really want to draw an Image not an Oval. :)

推荐答案

我更喜欢这样做,因为它只生成一次图像:

I prefer to do this as it only generates the image once:

public class CustomView extends View {

    private Drawable mCustomImage;

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCustomImage = context.getResources().getDrawable(R.drawable.my_image);
    }

    ...

    protected void onDraw(Canvas canvas) {
        Rect imageBounds = canvas.getClipBounds();  // Adjust this for where you want it

        mCustomImage.setBounds(imageBounds);
        mCustomImage.draw(canvas);
    }
}

这篇关于在画布上绘制对象/图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆