变身一个retangler视图成圈 [英] Morph a retangler view into a circle

查看:224
本文介绍了变身一个retangler视图成圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动画/变形矩形视图/布局,因为所有的观点都是长方形。这种用户的情况下将是:用户长点击包含与背景的ImageView的或就在主视图的图,就可以产生一个新的观点是在屏幕的大小,则该视图将收缩成的一个圆一定的大小。可能的解决方法我发现是扩展视图类,因此创建我自己的看法,并实现自定义的方法,但我对如何动画缩小视图困惑。还认为必须跟随用户触摸位置不断,因为它动画/摇身一变,以混淆增加更多。

I am trying to animate/morph a rectangular view/layout, as all views are rectangular. The user case for this would be: The user long-clicks a view that contains an ImageView or just the main view with a background, a new view would be generated being the size of the screen, then this view would shrink into a circle of a certain size. Possible solution I have found is extending the view class, therefore creating my own view and implementing a custom method, but I am confused on how to animate the view to shrink. Also the view would have to follow the users touch location continuously as it animates/morphs, adding more to the confusion.

推荐答案

一个快,不那么彻底的解释,至少让你思考方向是正确的:

A quick, not-so-thorough explanation to at least get you thinking in the right direction:

我觉得大的事情要记住这里是什么解决方案,你发现,动画是怎么回事,除非你视图的的onDraw(帆布油画)内执行的转换完全是痛苦的缓慢方法。换句话说,避免任何需要的观点来衡量,并奠定了为动画的每一帧。通过这种方法,您只需更新视图执行帧过渡和呼叫无效()。请记住,你将失去大部分的与在其圆形的渲染视图内的任何交互的能力。

I think the big thing to keep in mind here is that whatever solution you find, the animation is going to be painfully slow unless you perform the transformations entirely within the view's onDraw(Canvas canvas) method. In other words, avoid anything that requires the view to measure and lay out for every frame of the animation. With this approach, you simply perform a frame transition and call invalidate() on the view to update. Keep in mind that you will lose most of your ability to interact with anything within the view in its circular rendering.

由于有内置的Andr​​oid没有形状改变的动画,您可能需要使用类似的属性动画自己滚吧。如果你要支持蜂窝(3.0)的设备之前,您可能需要在 NineOldAndroids 库带来,并使用其物业动画功能来代替。从本质上讲,你需要动画圆角半径的值。最好的情况下,你改变的方成圆形。否则,你需要做一些裁剪或尺寸的转换也是如此。

Since there are no shape-transforming animations built into Android, you will probably need to use something like Property Animation to roll it yourself. If you're supporting devices prior to Honeycomb (3.0), you will probably need to bring in the NineOldAndroids library and use its property animation capabilities instead. Essentially, you'll need to animate the values of the corner radius. Best case scenario, you're transforming a square into a circle. Otherwise, you need to do some cropping or dimension transformations as well.

要动画视图,您首先需要获得一个位图重新presenting最新渲染这一观点的。你可以这样做使用以下code:

To animate the view, you will first need to obtain a bitmap representing the latest render of that view. You can do that using the following code:

// Creates a bitmap in the size of the view
Bitmap bitmap = Bitmap.createBitmap(myView.getWidth(), myView.getHeight(), Bitmap.Config.RGB_565);
// Creates a new canvas that draws to the bitmap
Canvas canvas = new Canvas(bitmap);
// Draws the contents of the view to the bitmap via the canvas
view.draw(notifyCanvas);

您只能每动画执行此一次,它应该出现之前,为了执行动画。

You should only perform this once per animation, and it should occur just prior to performing the animation.

接下来,你很可能需要创建一个空的扩展查看只是抱着你要修改的视图的动画重新presentation。

Next, you most likely need to create an empty extended View simply to hold the animated representation of the view you want to modify.

实施的的onDraw(帆布油画)的转换逻辑(这是通过扩展上述观点做),你可以访问的画布对象。从本质上讲,Canvas对象是包含所有的画的呈现在屏幕上查看呼叫的最后一层。重载的onDraw 方法上的新的自定义视图,然后你可以使用 canvas.drawRoundedRect(...)画带圆角和油漆中我们在code上面创建的视图的位图的矩形。在您的自定义视图,你可以保持它存储 drawRoundedRect 使用半径的变量。当你的动画,你设置半径越来越大,直到你得到一个圆(当然,你只会得到一个圆,如果原来的形状是方形)。这确实得到pretty复杂,我肯定跳了几步,但是你可以通过检查出的 RoundedImageView 。有很多在库,你可以借好点子。

Implementing the transformation logic in onDraw(Canvas canvas) (which is done by extending the view as mentioned above), you have access to the Canvas object. Essentially, the Canvas object is the final layer that contains all the "draw" calls that render the view on the screen. Overloading the onDraw method on your new custom view, you can then use canvas.drawRoundedRect(...) to draw a rectangle with rounded corners and paint in the bitmap of the view we created in the code above. Within your custom view, you can keep a variable that stores the radius used by drawRoundedRect. As you animate, you set that radius to be larger and larger until you get a circle (granted, you'll only get a circle if the original shape is a square). This does get pretty complicated, and I've definitely skipped a few steps, but you can see a good example of a rounded view by checking out the RoundedImageView on GitHub. There are lots of good ideas in that library that you could borrow.

不齐心协力更加完整解释道歉。这是一个相当复杂的任务!

Apologies for not pulling together a more complete explanation. It's quite a complex task!

这篇关于变身一个retangler视图成圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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