Android开发之不规则图形按钮响应

这是我在网上找了不少资料后,总结下来的简单不规则图形按钮制作方法。

如果我们想在安卓开发时设计漂亮的按钮,很可能会用到图片按钮,但是图片按钮响应区域是长方形的,比如下图



如果里面的月亮和云彩是用图片按钮制作,我们点击月亮中间空白部分,月亮按钮会响应,但是我们希望只有黄色部分是可以响应的。

简单思路:把月亮中间空白部分设置为透明,通过判断点击点颜色是否透明的方法,考虑是否做出响应。

设计思路:

在ontouch中可以获得触摸点的view和motionevent,我们用motionevent的getx()和gety()获得点击点相对点击对象View的x和y值,用getpixel(x,y)方法获得点击点对应的颜色(bitmap类中才有获得当前点击点的颜色方法getPixel(x,y),所以要把按钮背景转换成bitmap)。

对应完整代码如下:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener{
    Drawable drawable;
    Bitmap bitmap;
    int pixel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);
        //隐藏导航栏
        ActionBar actionBar=getSupportActionBar();
        assert actionBar!=null;
        actionBar.hide();

        findViewById(id.iv_moon).setOnTouchListener(this);
        findViewById(id.iv_tower).setOnTouchListener(this);
        findViewById(id.iv_wind1).setOnTouchListener(this);
        findViewById(id.iv_wind2).setOnTouchListener(this);
        findViewById(id.iv_wind3).setOnTouchListener(this);
        findViewById(id.iv_wind4).setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            int x=(int)event.getX();  //这个坐标是点击点在View v里的坐标。
            int y=(int)event.getY();
            switch (v.getId()){
                case R.id.iv_moon:
                    drawable=getDrawable(R.drawable.moon);
                    bitmap = ((BitmapDrawable)drawable).getBitmap();
                    pixel=bitmap.getPixel(x,y);
                    if(pixel!=Color.TRANSPARENT)
                        Toast.makeText(MainActivity.this,"你点击了月亮",Toast.LENGTH_SHORT).show();
                    break;
                case id.iv_tower:
                    drawable=getDrawable(R.drawable.tower);
                    bitmap = ((BitmapDrawable)drawable).getBitmap();
                    pixel=bitmap.getPixel(x,y);
                    if(pixel!=Color.TRANSPARENT)
                    {
                        Intent intent=new Intent(MainActivity.this,PlayActivity.class);
                        startActivity(intent);
                    }
                    break;
                case id.iv_wind1:
                    drawable=getDrawable(R.drawable.wind1);
                    bitmap = ((BitmapDrawable)drawable).getBitmap();
                    pixel=bitmap.getPixel(x,y);
                    if(pixel!=Color.TRANSPARENT){
                        Intent intent=new Intent(MainActivity.this,SettingActivity.class);
                        startActivity(intent);
                    }
                    break;
                case id.iv_wind2:
                    drawable=getDrawable(R.drawable.wind2);
                    bitmap = ((BitmapDrawable)drawable).getBitmap();
                    pixel=bitmap.getPixel(x,y);
                    if(pixel!=Color.TRANSPARENT)
                        Toast.makeText(MainActivity.this,"你点击了云2",Toast.LENGTH_SHORT).show();
                    break;
                case id.iv_wind3:
                    drawable=getDrawable(R.drawable.wind3);
                    bitmap = ((BitmapDrawable)drawable).getBitmap();
                    pixel=bitmap.getPixel(x,y);
                    if(pixel!=Color.TRANSPARENT)
                        Toast.makeText(MainActivity.this,"你点击了云3",Toast.LENGTH_SHORT).show();
                    break;
                case id.iv_wind4:
                    drawable=getDrawable(R.drawable.wind4);
                    bitmap = ((BitmapDrawable)drawable).getBitmap();
                    pixel=bitmap.getPixel(x,y);
                    if(pixel!=Color.TRANSPARENT)
                        Toast.makeText(MainActivity.this,"你点击了云4",Toast.LENGTH_SHORT).show();
                    break;
            }}
        return false;
    }
}

 

布局文件如下:

 
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#252B75"
    tools:context="com.tianxi.prisonerofhell.activitys.MainActivity">
    <ImageView
        android:id="@+id/iv_moon"
        android:src="@drawable/moon"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv_wind1"
        android:src="@drawable/wind1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="0dp"/>
    <ImageView
        android:id="@+id/iv_wind2"
        android:src="@drawable/wind2"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/iv_sea4"
        android:src="@drawable/sea4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="0dp"
        android:layout_marginBottom="20dp"
        />
    <ImageView
        android:id="@+id/iv_sea3"
        android:src="@drawable/sea3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="0dp"
        android:layout_marginBottom="15dp"
        />
    <ImageView
        android:id="@+id/iv_sea2"
        android:src="@drawable/sea2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="25dp"
        android:layout_marginBottom="0dp"

        />
    <ImageView
        android:id="@+id/iv_tower"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/tower" />
    <ImageView
        android:id="@+id/iv_wind3"
        android:src="@drawable/wind3"
        android:layout_gravity="right"
        android:layout_marginTop="50dp"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"/>
    <ImageView
        android:id="@+id/iv_wind4"
        android:src="@drawable/wind4"
        android:layout_gravity="right"
        android:layout_marginTop="10dp"
        android:layout_marginRight="0dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"/>
    <ImageView
        android:id="@+id/iv_sea1"
        android:src="@drawable/sea1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
       android:adjustViewBounds="true"
        />
</FrameLayout>


注意事项:

1.图片格式要用png,因为这种格式有透明度。

2.Imageview不能设置maxheight和maxwidth进行缩放,这会导致点击区域和响应区域不一致问题,原因我还不懂,希望有大佬能指出。

3.我只是刚学Android的新手,本文章可能存在很多问题,欢迎一起讨论更好的解决思路和方法。

这是上面的两个代码和素材下载(代码可以直接在上面复制呀!)点击打开链接

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 要在 Android 上创建图形按钮,可以使用以下步骤: 1. 在 res/drawable 文件夹中创建一个 XML 文件,来定义你想要的按钮形状。例如,你可以创建一个名为 "button_shape.xml" 的文件,并在其中添加以下代码: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp" /> <solid android:color="@color/button_color" /> </shape> ``` 这将创建一个圆角矩形按钮,颜色为 "@color/button_color"。 2. 在布局文件中,使用 Button 元素来创建一个按钮,并为其设置样式为你刚才定义的按钮形状: ``` <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_shape" android:text="My Button" /> ``` 3. 在代码中,可以像处理普通按钮一样处理这个图形按钮: ``` Button myButton = findViewById(R.id.my_button); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理按钮点击事件 } }); ``` 这样,一个简单的图形按钮就完成了。你可以根据需要自定义按钮形状和颜色,以适应你的应用程序风格。 ### 回答2: Android 图形按钮是一种在Android应用程序中常见的UI元素。它是一种用于用户交互的控件,通常用于触发某个操作或跳转到其他界面。 Android图形按钮通常包含一个图标或者是一个图标和文本组合而成。这个图标可以是一张图片或者是由字体图标库提供的矢量图标。图形按钮的外观可以通过设置按钮的背景色、边框样式、文字颜色等属性进行自定义。 在Android开发中,我们通常使用Button控件来创建图形按钮。在XML布局文件中,我们可以使用`Button`标签来定义一个图形按钮,并通过设置`android:background`属性来指定按钮的背景样式。同时,我们还可以使用`android:drawableLeft`、`android:drawableRight`等属性来设置按钮中图标的位置。在Java代码中,我们可以通过`setOnClickListener`方法来为按钮绑定点击事件的处理逻辑。 使用图形按钮可以为用户提供直观的操作体验,通过不同的图标或颜色搭配,可以增加按钮的辨识度,并且可以提高用户对界面的可理解性和使用便捷性。在设计图形按钮时,需要考虑按钮的大小、样式和布局,以及按钮与其他UI元素的协调性,从而实现更好的用户交互体验。 综上所述,Android图形按钮是一种常见的用户交互控件,通过图标和文字组合形成,通过设置属性和绑定点击事件的方式实现功能。它可以提供直观的操作体验,增加界面可理解性和使用便捷性,是Android应用程序中不可或缺的一部分。 ### 回答3: Android 图形按钮是一种用于用户界面的交互元素,它可以呈现各种形状和样式的图形Android 提供了多种方式来创建图形按钮,其中最常用的方式是使用Button控件和ImageView控件。 Button控件是Android中最基本的按钮控件之一,它可以显示文本、图标或二者的组合。可以通过设置Button的背景、文本、文本颜色和点击事件来自定义按钮的外观和行为。 ImageView控件是一种显示图像的控件,它可以加载本地或网络上的图像资源。将图像设置为按钮的背景,可以实现图形按钮的效果。可以使用setImageResource()方法或setImageBitmap()方法来设置ImageView的图像资源。 通过设置图形按钮的点击事件,可以为按钮添加事件监听器。当用户点击按钮时,可以执行相应的操作,例如跳转到另一个界面、提交表单或执行其他逻辑。 除了使用基本的Button和ImageView控件,还可以使用自定义控件或第三方库来创建更复杂的图形按钮。自定义控件可以继承Button或ImageView来实现特定的按钮效果,例如圆形按钮、带有动画效果的按钮等。第三方库提供了丰富的图形按钮样式和动画效果,可以通过导入库并设置相应的属性来使用。 总结来说,Android 图形按钮是一种用于用户界面的交互元素,可以通过Button和ImageView控件、自定义控件或第三方库来创建。通过设置背景、文本、文本颜色和点击事件等属性,可以自定义图形按钮的外观和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值