Android的:在特定的位置绘制位图,拒绝绘制 [英] Android: Drawing Bitmap in Specific Location, Refusal to Draw

查看:109
本文介绍了Android的:在特定的位置绘制位图,拒绝绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我开始与我最新编写Java,所以我会为一个完整的解释爱。不只是溢料的code,但东西,让一些网站为什么以及在哪里它应该是

我目前的尝试的编写一个应用程序,我是怎么过有一些麻烦的画布和绘图位图,我想在上面。这是我的code绘制图像:

I'm currently attempting to write an Application, how ever I'm have some trouble with the Canvas and drawing the bitmap I would like onto it. This is my code to draw the image:

Canvas canvas = null;
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(image, 10, 800, null);

此方法(期待)抛出一个空指针异常。然而,定义画布为空是唯一的选择Eclipse的给我。当试图将其定义为

This method (expectantly) throws a Null Pointer Exception. However, Defining "canvas" as null is the only option Eclipse gives me. When attempting to define it as

Canvas canvas = new Canvas();

Android的只是拒绝绘制位图。我敢肯定,我的形象是位于RES /绘制/图像 如果有帮助,将图像保存为image.png

Android simply refuses to draw the bitmap. I'm sure my image is located at res/drawable/image If it helps, the image is saved as "image.png"

我将如何让Android显示/在我的显示图像/位图中的的具体位置的我已要求它来绘制图像(10x800)?

How would I make Android display/display my image/bitmap at the specific location I have asked it to draw the image (10x800)?

推荐答案

写作废话,解决了一个编译器错误不会改变的事实,这是胡说八道! :)

Writing nonsense that solves a compiler error doesn't change the fact that it's nonsense! :)

您必须得到一个引用Canvas对象的另一种方式。你可能想更具体的了解你所究竟想要做什么,这样我们可以建议你应该如何去做。 (例都在尝试,以及一些其他的意见,只是显示图像?你们是不是要创建一个自定义视图?你可能只是想考虑使用ImageView的)

You have to get a reference to a Canvas object another way. You might want to be more specific about what you are exactly trying to do, so that we can suggest how you should go about it. (Ex. are you attempting to just display an image along with some other Views? Are you trying to create a custom View? You might just want to consider using an ImageView)

编辑:

您应该阅读关于Android架构的developer.android.com。如果你只是想显示的图像,可能没有理由直接使用画布。不过,你可以通过扩展视图类中的自定义视图中绘制

You should read up about Android architecture at developer.android.com . If you are simply trying to show an image, there might not be a reason to use the canvas directly. Nonetheless, you can draw within a custom View by extending the View class

class myView extends View{

    Bitmap bm;

    loadBitmap()
    {
        bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
    }

    @Override
    public void draw(Canvas c)
    {
       c.drawBitmap(bm, XCORD, YCORD, null);
    }
}

如果您不需要自定义视图,只需使用ImageView的类

if you don't need a custom view, just use the ImageView class

class MyActivty extends Activity{
   @Override
   public void onCreate(Bundle b)
   {
       super.onCreate(b);
       ImageView iv = new ImageView(this);
       iv.setImageResource(R.drawable.pic);
       setContentView(iv);
   }
}

警告:我写的那些方法调用了我的头的顶部,他们可能会稍微偏离

Warning: I wrote those method calls off of the top of my head, they might be slightly off.

这篇关于Android的:在特定的位置绘制位图,拒绝绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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