Android:创建形状按钮

发布于 2024-12-10 01:50:46 字数 117 浏览 0 评论 0原文

如何创建这样的自定义按钮?

在此处输入图像描述

它应该只是可点击区域而不是真正的按钮。

How can I create a custom button like this?

enter image description here

It should be just clickable area not a real button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心凉怎暖 2024-12-17 01:50:46

我在我的应用程序上使用了一堆不规则形状的按钮,为了更改按钮的“热区”或“可点击区域”,我只使用 Bitmap.getPixel() 方法来检查 alpha在所使用的图像上。如果该方法返回0,则不执行点击事件。

例子:
(1) 按照您喜欢的方式照常创建按钮。
(2) 定义一个位图 并为其分配与按钮相同的可绘制图像。
(3) 获取触摸或单击操作的 X 和 Y 坐标。
(4) 将坐标传递给 .getPixel(x,y) 方法。

示例代码:

// ** Declare your Bitmap somewhere **
final Bitmap TheBitmap;       
TheBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.TheImage);

// ** My onTouch code **
public boolean onTouch(View v, MotionEvent event) {

        int eventPadTouch = event.getAction();

        switch (eventPadTouch) {

            case MotionEvent.ACTION_DOWN:
                if (iX>=0 & iY>=0 & iX<TheBitmap.getWidth() & iY<TheBitmap.getHeight()) { // ** Makes sure that X and Y are not less than 0, and no more than the height and width of the image.                
                    if (TheBitmap.getPixel(iX,iY)!=0) {
                        // * A non-alpha area was clicked, do something 
                    }               
                }
                return true;                
        }           
        return false;
}

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:

// ** Declare your Bitmap somewhere **
final Bitmap TheBitmap;       
TheBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.TheImage);

// ** My onTouch code **
public boolean onTouch(View v, MotionEvent event) {

        int eventPadTouch = event.getAction();

        switch (eventPadTouch) {

            case MotionEvent.ACTION_DOWN:
                if (iX>=0 & iY>=0 & iX<TheBitmap.getWidth() & iY<TheBitmap.getHeight()) { // ** Makes sure that X and Y are not less than 0, and no more than the height and width of the image.                
                    if (TheBitmap.getPixel(iX,iY)!=0) {
                        // * A non-alpha area was clicked, do something 
                    }               
                }
                return true;                
        }           
        return false;
}

The event.getX() and event.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.

还在原地等你 2024-12-17 01:50:46

使用您自己的画布或使用像这样的 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

旧城空念 2024-12-17 01:50:46

只需创建一个类似的图像,您就可以使用不带文本的 ImageViewButton ,并实现 OnClickListener。它就是有效的!

Simply just create an image like that and you can use either ImageView or Button w/o text, and implement an OnClickListener. It just works!

奶气 2024-12-17 01:50:46

将其另存为 png 并将其放入您的可绘制文件夹中。然后在你的 xml 中使用类似这样的东西,

<Button
android:height="wrap_content"
android:width="wrap_content"
android:background="@drawable/your_png"
/>

我并不 100% 肯定切角会正常工作。消失的角落区域最终可能是可点击的。如果是这种情况,并且您不希望出现这种情况,那么您必须在某处将图片切成两半,创建两个按钮,您可以将它们设置为彼此相邻以组成该形状,并使用相同的单击侦听器两个都。从用户的角度来看,它仍然看起来像一个按钮。

save that as a png and and put it in your drawables folder. Then in your xml use something like this

<Button
android:height="wrap_content"
android:width="wrap_content"
android:background="@drawable/your_png"
/>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文