动态创建/绘制图像以放入Android视图 [英] Dynamically create / draw images to put in android view

查看:51
本文介绍了动态创建/绘制图像以放入Android视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否以正确"的方式进行操作,因此我也愿意接受其他选择.这是我要完成的工作:

I'm not sure I'm doing this the "right" way, so I'm open to other options as well. Here's what I'm trying to accomplish:

我想要一个包含图形的视图.该图应由应用程序本身动态创建.图形应该是可缩放的,并且开始时可能比屏幕大(大约800x600)

I want a view which contains a graph. The graph should be dynamically created by the app itself. The graph should be zoom-able, and will probably start out larger than the screen (800x600 or so)

我正计划从简单开始,只是一个散点图.最终,我想要一个散点图,其中有一条拟合线和一条误差轴,并且在缩放图形时,其误差轴在屏幕上停留在屏幕上……因此,这可能意味着将三张图像与缩放功能捆绑在一起.

I'm planning on starting out simple, just a scatter plot. Eventually, I want a scatter plot with a fit line and error bars with axis that stay on the screen while the graph is zoomed ... so that probably means three images overlaid with zoom functions tied together.

我已经构建了一个视图,该视图可以绘制,可以使用集中的捏缩放和拖动,可以自动缩放图像,可以动态切换图像并且可以拍摄比屏幕大的图像.将图像绑在一起应该不是问题.

I've already built a view that can take a drawable, can use focused pinch-zoom and drag, can auto-scale images, can switch images dynamically, and takes images larger than the screen. Tying the images together shouldn't be an issue.

但是,我不知道如何动态绘制简单的图像.

I can't, however, figure out how to dynamically draw simple images.

例如:我是否得到一个BitMap对象并逐像素绘制?我想使用某些ShapeDrawables,但似乎它们只能在画布上绘制形状...然后如何在我的视图中获得所有这些形状的位图?或者,每次移动或缩放时,是否都必须在视图的"onDraw"例程中动态重绘要描绘的所有图像?

For instance: Do I get a BitMap object and draw on it pixel by pixel? I wanted to work with some of the ShapeDrawables, but it seems they can only draw a shape onto a canvas ... how then do I get a bitmap of all those shapes into my view? Or alternately, do I have to dynamically redraw /all/ of the image I want to portray in the "onDraw" routine of my view every time it moves or zooms?

我认为完美"的解决方案是使用ShapeDrawable(或类似的东西来绘制线条并标记它们),并通过视图的onDraw方法绘制轴……将它们保持在当前水平并保持在正确的水平...然后叠加可以缩放和移动的数据点/拟合曲线/等的预生成图像.在图形图像上将白色设置为alpha的情况下应该可以实现.

I think the "perfect" solution would be to use the ShapeDrawable (or something like it to draw lines and label them) to draw the axis with the onDraw method of the view ... keep them current and at the right level ... then overlay a pre-produced image of the data points / fit curve / etc that can be zoomed and moved. That should be possible with white set to an alpha on the graph image.

PS:在视图上时,图形图像实际上不应/change/.它只是在缩放和拖动.轴可能实际上会随着运动而变化.因此,在进入视图之前(或紧随其后)预先生成图形是最佳的.但是我也注意到缩放对于矢量图像非常有效……听起来也很合适(而不是位图?).

PS: The graph image shouldn't actually /change/ while on the view. It's just zooming and being dragged. The axis will probably actually change with movement. So pre-producing the graph before (or immediately upon) entering the view would be optimal. But I've also noticed that scaling works really well with vector images ... which also sounds appropriate (rather than a bitmap?).

因此,我正在寻找一些一般性指导.尝试阅读BitMap,ShapeDrawable,Drawable等类,似乎找不到合适的类.那使我认为我在树错了树皮,并且有更多经验的人可以为我指明正确的方向.希望我不会浪费时间来构建昨天放在一起的可缩放视图:).

So I'm looking for some general guidance. Tried reading up on the BitMap, ShapeDrawable, Drawable, etc classes and just can't seem to find the right fit. That makes me think I'm barking up the wrong tree and someone with some more experience can point me in the right direction. Hopefully I didn't waste my time building the zoom-able view I put together yesterday :).

推荐答案

首先,如果您从中学到一些东西,那么永远不会浪费时间来编写代码.:-)

First off, it is never a waste of time writing code if you learned something from it. :-)

不幸的是,在Android中仍然不支持绘制矢量图像.因此,位图就是您所得到的.

There is unfortunately still no support for drawing vector images in Android. So bitmap is what you get.

我认为您缺少的一点是,您可以随时在位图上绘制画布.您不必等待onDraw给您一个.

I think the bit you are missing is that you can create a Canvas any time you want to draw on a bitmap. You don't have to wait for onDraw to give you one.

因此,在某个时候(从onCreate,数据更改时等等),请创建您想要的任意大小的位图.

So at some point (from onCreate, when data changes etc), create your own Bitmap of whatever size you want.

这是一些伪代码(未经测试)

Here is some psuedo code (not tested)

Bitmap mGraph;

void init() {
    // look at Bitmap.Config to determine config type
    mGraph = new Bitmap(width, height, config);
    Canvas c = new Canvas(mybits);
    // use Canvas draw routines to draw your graph
}

// Then in onDraw you can draw to the on screen Canvas from your bitmap.
protected void onDraw(Canvas canvas) {
    Rect dstRect = new Rect(0,0,viewWidth, viewHeight);
    Rect sourceRect = new Rect();
    // do something creative here to pick the source rect from your graph bitmap
    // based on zoom and pan 
    sourceRect.set(10,10,100,100);

    // draw to the screen
    canvas.drawBitmap(mGraph, sourceRect, dstRect, graphPaint);
}

希望能有所帮助.

这篇关于动态创建/绘制图像以放入Android视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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