新到Android - 绘制视图在运行时 [英] New to Android - Drawing a view at runtime

查看:111
本文介绍了新到Android - 绘制视图在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我刚开始使用开发的机器人。我期待端口我的iPhone应用程序之一,但我是那种在为如何绘制在运行时的视图(在XML声明不是一个视图)的损失。基本上,我想画一个简单的矩形,但能够进行延伸后操纵其框架。

I'm just getting started with developing for Android. I'm looking to port one of my iPhone applications, but I'm kind of at a loss for how to draw a view at runtime (a view not declared in the XML). Basically, I want to draw a simple rectangle, but then be able to manipulate its frame after being drawn.

抱歉,如果这是真的,真的很简单的问题,但我似乎无法找到一些相当于iPhone的SDK在这里。

Sorry if this is a really, really simple question, but I can't seem to find some equivalent to the iPhone SDK here.

在此先感谢!

推荐答案

这听起来像你想尝试2D图形 - 对于这一点,你应该使用画布。您可以通过无效()的方法,它告诉Android的重画整个事情触发控制画布绘制自定义的的OnDraw()方法。你提到不想使用XML文件,但是这是摆在画布最简单的方法 - 你不必来定义其内容的XML文件中,而只是告诉布局文件它的存在。一个强大而简单的方式把一个Canvas在你的应用程序是一个自定义视图。例如,在XML文件中包含< your.package.CustomView机器人:... /> 元素。然后声明 CustomView扩展视图类。任何一种绘制你想要做的,放在的OnDraw()方法。

It sounds like you want to experiment with 2D graphics - for that, you should use a Canvas. You can control the drawing of the Canvas through the invalidate() method, which tells Android to redraw the whole thing triggering your customised onDraw() method. You mention not wanting to use the XML file, but that is the simplest way to put in a Canvas - you don't have to define its contents in the XML file, but simply tell the layout file it's there. A powerful but simple way to put a Canvas in your application is to customise a View. For example, include in your XML file a <your.package.CustomView android:.../> element. Then declare the CustomView extends View class. Any kind of drawing you want to do, put in the onDraw() method.

例如,绘制一个矩形,做这样的事情。

For example, to draw a rectangle, do something like this.

//First you define a colour for the outline of your rectangle
rectanglePaint = new Paint();
rectanglePaint.setARGB(255, 255, 0, 0);
rectanglePaint.setStrokeWidth(2);
rectanglePaint.setStyle(Style.STROKE);

//Then create yourself a Rectangle
Rect rectangle = new Rect(left, top, right, bottom) //in pixels

//And here's a sample onDraw()
@Override
public void onDraw(Canvas canvas){
    rectangle.offset(2, 2);
    canvas.drawRect(rectangle, rectanglePaint);
}

每次无效()从程序被调用时,视图将被重画的矩形移动2px的向下和向右。注:重绘只发生在主线程等待。换句话说,如果你有一个循环调用无效几次,查看实际上不会画,直到循环结束。您实现这个愿望,但增加了更多的复杂性。有关如何这样做了一个例子,看看从谷歌LunarLander例如游戏 - 这是一个简单的游戏演示的自定义视图,2个线程,以及如何实现持续的动画

Every time invalidate() is called from your program, the view will be redrawn and the rectangle moved 2px down and to the right. Note: the redrawing only happens with the main thread is 'waiting'. In other words, if you have a loop calling invalidate several times, the View won't actually be drawn until the loop finishes. You can get around this, but that adds more complication. For an example of how that's done, look at the LunarLander example game from Google - it's a simple game demonstrating a custom View, 2 threads, and how to implement continuous animation.

这篇关于新到Android - 绘制视图在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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