Android:创建形状按钮
如何创建这样的自定义按钮?
它应该只是可点击区域而不是真正的按钮。
How can I create a custom button like this?
It should be just clickable area not a real button.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在我的应用程序上使用了一堆不规则形状的按钮,为了更改按钮的“热区”或“可点击区域”,我只使用
Bitmap.getPixel()
方法来检查 alpha在所使用的图像上。如果该方法返回0,则不执行点击事件。例子:
(1) 按照您喜欢的方式照常创建按钮。
(2) 定义一个
位图
并为其分配与按钮相同的可绘制图像。(3) 获取触摸或单击操作的 X 和 Y 坐标。
(4) 将坐标传递给
.getPixel(x,y)
方法。示例代码:
event.getX()
和event.getY()
只是为您提供触摸按钮位置的坐标。** 以上示例旨在指导您正确的方向。需要在代码中添加一些检查以确保不会发生错误。
I use a crapload of irregular shaped buttons on my app, and to change the "hot zone" or "clickable area" of the button, I just use the
Bitmap.getPixel()
method to check for alpha on the image used. If the method returns 0, then don't perform click event.Example:
(1) Create your button as usual, whichever way you would like.
(2) Define a
Bitmap
and assign to it the same image drawable used for the button.(3) Get the X and Y coordinates of the touch or click action.
(4) Pass the coordinates to the
.getPixel(x,y)
method.Sample Code:
The
event.getX()
andevent.getY()
simply give you the coordinates of where you touched the button.** The above sample is to guide you in the correct direction. There are some checks to add to the code to assure no errors occur.
使用您自己的画布或使用像这样的 Photoshop 制作图像,然后使用 Bitmap.createScaledBitmap 根据按钮的尺寸缩放它,因此您将得到这个按钮。
使用画布你必须编写更多代码只要这样做它就会工作得很好
ceate using your own canvas or make a image using photoshop like this and then using Bitmap.createScaledBitmap scale it according to dimension of your button and hence you will get this button.
using canvas you have to write more code just do this it will work fine
只需创建一个类似的图像,您就可以使用不带文本的
ImageView
或Button
,并实现OnClickListener
。它就是有效的!Simply just create an image like that and you can use either
ImageView
orButton
w/o text, and implement anOnClickListener
. It just works!将其另存为 png 并将其放入您的可绘制文件夹中。然后在你的 xml 中使用类似这样的东西,
我并不 100% 肯定切角会正常工作。消失的角落区域最终可能是可点击的。如果是这种情况,并且您不希望出现这种情况,那么您必须在某处将图片切成两半,创建两个按钮,您可以将它们设置为彼此相邻以组成该形状,并使用相同的单击侦听器两个都。从用户的角度来看,它仍然看起来像一个按钮。
save that as a png and and put it in your drawables folder. Then in your xml use something like this
I am not 100% positive that the corner cut out is going to work properly. That corner area that is gone may end up being clickable. If that is the case and if you don't want it to be then you'll have to slice your picture in half somewhere create two buttons that you can set next to each other to make up that shape and use the same click listener for both. From the users perspective it will still seem like one button.