开发者

Drawing a java.awt.image.BufferedImage in an Android View

Is there any way to draw a java.awt.image.Buff开发者_如何学编程eredImage in an Android View ?

I have an existing plain Java/Swing code drawing stuff into a BufferedImage and using it to draw into a Swing component.

I would like to copy/paste this code into my Andoid project, and somehow draw into a View.


Ok so i think you are used to using graphics in java on your computer, in android its a little different, instead of having an instance of BufferedImage you have an instance of Drawable. Drawable can draw a BufferedImage for you, and heres how:

Drawable image = context.getResourses().getDrawable(R.drawable.my_super_cool_image);

this is how you setup a Drawable, ill walk you through it. Look at your project(hopefully you are useing Eclipse), then look for a folder called res in your path, then look for the 3 folders that start with drawable. put your image in all 3 of these folders. Once you have done that, then enter the code above but instead of putting my_super_cool_image put the name of the image you want to draw(p.s. all letters must be lowercase, and no spaces). Later on when you want to draw this image you should do the following:

public void onDraw(Canvas canvas)
{
    image.draw(canvas);
}

I know you are probably use to seeing this:

public void paint(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g;
    g2.drawImage(image,0,0,null);
}

Except when you use android you have to override the onDraw(Canvas canvas) meathod instead of paint().

I hope this helped!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜