如何创建Android上的放大镜 [英] How to create magnifier on android

查看:169
本文介绍了如何创建Android上的放大镜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好!

我想实现一个放大镜。
下看起来更大的应该做的内容。


Hello everybody!
I'm trying to implement an magnifier. It should make content under it looks bigger.

几个小时后,我只有一个解决方案。

After some hours I have only one solution.

在我的XML文件,声明是这样的(伪code):

In my xml file, declare something like this (pseudo code):

<ZoomView>
    some other content views

    <View 
        android:id="@+id/magnifier
    />
</ZoomView>

在此code ZOOMVIEW是一个自定义视图延伸RelativeLayout的。查看用放大镜id是一个实际的放大镜,将放大的内容。

In this code ZoomView is a custom view extends RelativeLayout. View with magnifier id is an actual magnifier that will zoom content.

这是它的样子:




那么,在code发生。

So what happens in code.

在ZOOMVIEW的方法的onDraw我做这个事情。

In onDraw method of ZoomView I do this things.

@Override
protected void onDraw(android.graphics.Canvas canvas)
{
    Bitmap  bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas bitmapCanvas = new Canvas(bitmap);
    super.onDraw(bitmapCanvas);

    // some code that detects magnifier view position and size, 
    // than gets 1 area image, scale it to make in 2x bigger.
    // And then replace 2 area with this scaled image.

    // draw result on canvas
    canvas.drawBitmap(bitmap, new Matrix(), null);
}

这code工作正常,但实在是太慢了。谁能给我一个其他的解决方案或如何优化这个吗?

This code works fine, but it is too slow. Can anyone give me an other solution or how can I optimize this one?

很抱歉,如果事情是错的,这是我的第一个问题。

Sorry if something is wrong, this is my first question.

推荐答案

您在正确的轨道上,但我认为业绩可能采取某些处理出来的的onDraw方法加以改进。如果原始尺寸的图像/内容是静态的,你可以很容易地创建内容的onCreate在放大版本(或者更好的是,在AsyncTask的),然后在放大镜感动了所有你需要做的就是计算一下区域的内容应显示的放大版本,而不是试图生成它每一次。

You are on the right track but I think performance could be improved by taking some of the processing out of the onDraw method. If the original size image/content is static you could easily create the magnified version of the content during onCreate (or better still, in an AsyncTask) and then when the magnifier is moved all you have to do is calculate what area of the content should be displayed of the magnified version, rather than trying to generate it every time.

如果所述内容是动态的,而不是仍然可以通过具有所述内容的2恒定副本,一个在正常大小和一个放大的和两者都在不断保持最新执行类似的处理。

If the content is dynamic instead you could still perform similar processing by having 2 constant copies of the content, one at normal size and one magnified and both are constantly kept up to date.

您的解决方案是,否则在正确的轨道上。我希望这有助于。

Your solution is otherwise on the right track. I hope this helps.

这篇关于如何创建Android上的放大镜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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