简体   繁体   English

Android:如何绘制一条简单的线并将其显示在小部件中?

[英]android: how to draw a simple line and display it in a widget?

i want to visualize stuff with a widget by drawing some lines. 我想通过绘制一些线条来使小部件可视化。 lets just say i get two coordinates (x,y) every n seconds and then want to update the widget accordingly. 假设我每n秒获得两个坐标(x,y),然后要相应地更新小部件。 this works just fine by now, but now i want to actually DRAW a line between those two points. 到现在为止,这工作还不错,但是现在我想在这两个点之间画一条线。

i googled around and found many things, but nothing explaining the fundamentals of drawing on an image. 我四处搜寻,发现了很多东西,但没有任何东西可以解释绘制图像的基本原理。 since i have NO experience with graphics, i imagine these things to work somehow like graphics drawn with tikzpicture in LaTeX. 因为我没有图形方面的经验,所以我认为这些东西可以像在LaTeX中用tikzpicture绘制的图形一样工作。

can someone please explain to me the how this all works - or at least point me to an example explaining this from the start? 有人可以给我解释一下这一切是如何工作的吗?或者至少请我从一开始就提供一个示例来说明这一点? it would be highly appreciated! 将不胜感激!

thanks! 谢谢!

In the Android is usually all comes down to drawing on the canvas, eg as in the method View.onDraw(). 在Android中,通常一切都取决于在画布上进行绘制,例如在View.onDraw()方法中。

// prepare picture
if (mBackground != null) {
    mBackground.recycle();
}
mBackground = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas backgroundCanvas = new Canvas(mBackground);
backgroundCanvas.scale(width, height);
RectF rect = new RectF(0f, 0f, 1f, 1f);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.parseColor("#18a518")); // gray

// draw line
backgroundCanvas.drawLine(startX, startY, stopX, stopY, paint);

onDraw(): 的onDraw():

// draw picture on View canvas
@Override
public void onDraw(Canvas canvas) {
    canvas.drawBitmap(mBackground, 0, 0, null);
}

ADD

A primitive chain is as follows: 1 create your own View class (extend). 基本链如下:1创建您自己的View类(扩展)。 2 Define onDraw() method in your View. 2在视图中定义onDraw()方法。 3 Place your View in layout xml file (or add in runtime: Activity.addContentView() ). 3将View放置在布局xml文件中(或在运行时添加: Activity.addContentView() )。 4 If needed redraw, call invalidate(); // forces onDraw() to be called 4如果需要重绘,则调用invalidate(); // forces onDraw() to be called invalidate(); // forces onDraw() to be called . invalidate(); // forces onDraw() to be called
For detailed information see android examples by google. 有关详细信息,请参阅Google提供的android示例。

Then, if you want improve efficiency, learn SurfaceView. 然后,如果要提高效率,请学习SurfaceView。 But in surface we drawing on canvas too. 但是在表面上,我们也在画布上绘画。

http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html http://www.droidnova.com/playing-with-graphics-in-android-part-i,147.html

may help for a start. 可能对您有所帮助。 unfortunately, this still leaves me clueless how to actually DRAW on a widgets layout... 不幸的是,这仍然让我一无所知,如何在小部件布局上实际绘制...

... and to answer this by myself: android: how to add a customized image to widgets? ...并由我自己回答: android:如何向小部件添加自定义图像?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM