Android 自定义三角形按钮

2024-05-01

我想制作 2 个对角三角形按钮,如下所示这个问题 https://stackoverflow.com/questions/23315400/android-diagonal-triangle-buttons.

我怎样才能实现这个目标?我应该用矩形制作一个可绘制的 xml 并以某种方式旋转它吗?我应该制作一个图像并在数学的帮助下使其只能在三角形部分上点击吗?


    package com.example.buttonsView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.FillType;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class TwoButtons extends View {

    private Path path;
    private Path path1;
    private Region region;
    private Region region1;
    private static int GAP=10;

    private ButtonClickEvents buttonClickEvent;

    public interface ButtonClickEvents{
        public void redButtonClick();
        public void blueButtonClick();
    }

    public void setOnButtonClickEvent(ButtonClickEvents buttonClickEvent) {
        this.buttonClickEvent=buttonClickEvent;
    }

    public TwoButtons(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }


    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        canvas.drawColor(Color.GRAY);
        Paint paint = new Paint();
        paint.setColor(android.graphics.Color.BLACK);
        canvas.drawPaint(paint);

        paint.setStrokeWidth(0);

        paint.setColor(android.graphics.Color.RED);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);

        Point a = new Point(GAP, GAP);
        Point b = new Point(GAP, getHeight()-2*GAP);
        Point c = new Point(getWidth()-2*GAP, GAP);

        path = new Path();
        path.setFillType(FillType.EVEN_ODD);
        path.moveTo(a.x, a.y);
        path.lineTo(b.x, b.y);
        path.lineTo(c.x, c.y);
        path.close();

        canvas.drawPath(path, paint);

        RectF rectF = new RectF();
        path.computeBounds(rectF, true);
        region = new Region();
        region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));

        paint.setColor(Color.BLUE);
        Point a1 = new Point(getWidth()-GAP, getHeight()-GAP);
        Point b1 = new Point(getWidth()-GAP, 2*GAP);
        Point c1 = new Point(2*GAP, getHeight()-GAP);

        path1 = new Path();
        path1.setFillType(FillType.EVEN_ODD);
        path1.moveTo(a1.x, a1.y);
        path1.lineTo(b1.x, b1.y);
        path1.lineTo(c1.x, c1.y);

        path1.close();
        canvas.drawPath(path1, paint);

        RectF rectF1 = new RectF();
        path1.computeBounds(rectF1, true);
        region1 = new Region();
        region1.setPath(path1, new Region((int) rectF1.left, (int) rectF1.top, (int) rectF1.right, (int) rectF1.bottom));


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            Point point = new Point();
            point.x = (int) event.getX();
            point.y = (int) event.getY();

            invalidate();


            if(region.contains((int)point.x,(int) point.y))
            {
                if(buttonClickEvent!=null)
                    buttonClickEvent.redButtonClick();
            }else if(region1.contains((int)point.x,(int) point.y))
            {
                if(buttonClickEvent!=null)
                    buttonClickEvent.blueButtonClick();
            }

        return true;
        }

        return false;
    }

}

活动主文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.buttonsView.MainActivity" >

    <com.example.buttonsView.TwoButtons
        android:id="@+id/twoButtons1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

MainActivity.java

package com.example.buttonsView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.afbb.preferencessample.R;
import com.example.buttonsView.TwoButtons.ButtonClickEvents;

public class MainActivity extends Activity {

    TwoButtons buttons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttons = (TwoButtons) findViewById(R.id.twoButtons1);
        buttons.setOnButtonClickEvent(new ButtonClickEvents() {

            @Override
            public void redButtonClick() {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "red",
                        Toast.LENGTH_SHORT).show();

            }

            @Override
            public void blueButtonClick() {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "blue",
                        Toast.LENGTH_SHORT).show();

            }
        });

    }

}

output :

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 自定义三角形按钮 的相关文章

  • 在名称中使用时间戳时,Android Studio 在构建后无法启动应用程序

    我遇到了 gradle 和 Android Studio 的问题 该问题仅在 Android Studio 中构建时出现 BuildServer 和 Commandline 工作正常 applicationVariants all vari
  • Android向后兼容技术

    我现在在开发基于最新 API 15 ICS 的 15 项活动 Android 应用程序方面取得了进展 现在我发现应用程序的主要功能主义者即使支持 android v4 也不向后兼容 例如 1 fragment事务动画 2 将StringSe
  • 如何将 Android Instrumentation 测试推送到模拟器/设备?

    我正在尝试使用 Ubuntu 9 04 中的命令行 shell 在 Android 模拟器上运行 Webkit 布局测试 adb s emulator 5554 shell am instrument w com android dumpr
  • Android ListView 自定义适配器 ImageButton

    这可能不是正确的方法 如果有更好的方法请告诉我 我创建了一个自定义适配器类 在我的 getView 方法中我膨胀了我想要使用的视图 public View getView int position View convertView View
  • 如何忽略 LeakCanary 中的某些类?

    有人能给我一个如何忽略 LeakCanary 中的某些类的有效示例吗 我正在查看这个示例 以忽略 LeakCanary 中第三方库中的某些类 但我不知道将其放在应用程序中的何处 我把它放在我的应用程序类中 但这些变量和方法有错误 isInA
  • 需要在 Android 中伪造正在扫描的 NFC 标签

    好的 我有一个应用程序 此应用程序仅在扫描 NFC 标签 任何标签 时才会完成任务 唯一的问题是 我没有任何 nfc 标签 无论如何 我正试图消除对卡的需要 所以我需要的是一种 伪造 使其看起来 就像已扫描 nfc 标签的方法 我可以编写应
  • 在后台服务中持续获取位置更新

    我正在开发需要在后台服务中持续获取位置更新的应用程序 我已经使用了它正在使用的后台粘性服务 但是 即使我添加了启动广播并在那里启动了服务 启动完成后服务也没有启动 服务启动并立即被杀死 另外 这不适用于奥利奥 服务在应用程序关闭几分钟后停止
  • 在 Android 中始终以横向模式打开相机

    在我的 Android 应用程序中 单击按钮后我希望相机以横向模式打开 即使我将手机旋转为纵向模式 相机也应始终处于横向模式或纵向模式 使用此代码在横向模式下打开相机 Intent cameraIntent new Intent Media
  • Android httpclient文件上传数据损坏和超时问题

    我在 Android 中上传图像时遇到问题 我正在使用 apache httpmime 4 1 lib 代码是这样的 MultipartEntity reqEntity new MultipartEntity HttpMultipartMo
  • Android Drawable 绘图性能?

    在我看来 我有一个简单的 ARGB 可绘制对象 大约需要 2 毫秒才能绘制 但我可以在 0 5 毫秒内绘制与位图相同的文件 只是一些快速代码 我真的不能认为它是一个选项 优化可绘制对象的绘制速度的最佳方法是什么 这取决于可绘制的数量以及每个
  • 按名称获取 ArrayList

    这是正确的获取方式吗ArrayList
  • 在 android 版本 7.0 上膨胀类 android.widget.DatePicker 时出错

    我想显示弹出日期选择器并且我使用此代码 Calendar mcurrentDate Calendar getInstance int mYear mcurrentDate get Calendar YEAR int mMonth mcurr
  • 无法登录 Google Play 游戏服务

    我在开发者控制台上使用包名称和正确的签名证书设置了我的游戏 并为其创建了排行榜 但没有创建任何成就 然后 我从以下位置下载了示例 Type A Number Challenge 和 BaseGameUtils https developer
  • SDK尚未初始化,请务必先调用FacebookSdk.sdkInitialize()

    我在实现 Facebook SDK 时遇到此错误 并且我tried https stackoverflow com questions 15490399 error inflating class com facebook widget l
  • 如何在 onDraw() 方法中定义与像素无关的高度

    我扩展了 View 来构建自定义小部件 我想用独立的像素单位定义小部件的高度 我认为可以通过将像素密度乘以所需的高度来完成 但我不知道该怎么做 到目前为止我所拥有的 最小化 public class Timeline extends Vie
  • 应用程序中空指针异常[重复]

    这个问题在这里已经有答案了 我正在尝试在我的应用程序中实施应用程序内计费 我写了这段代码 public class Settings extends PreferenceFragment ServiceConnection mService
  • 改造方法调用可能会产生“java.lang.NullPointerException”

    使用 Retrofit 2 3 0 我在 Android Studio 中收到以下消息 有关如何删除此 IDE 错误消息的任何建议 谢谢 来自Response文档 http square github io retrofit 2 x ret
  • 使用 C# 动态创建按钮并按预定义的顺序放置它们

    NET 4 5 C 创建 Windows 窗体 我想动态创建和添加按钮并为其分配单击事件 但希望它们以特定的方式动态放置 就像图像一样 我的问题是如何以上述方式动态放置按钮 即 4x4 格式 一行 4 个按钮 4 列 但行数不受限制 是否可
  • 在android中使用BaseActivity的不同活动中的通用标头

    我想编写一次代码并在不同的活动中使用 我创建了一个Base Activity class为了那个原因 此外 不同活动中所有布局的标题都是相同的 我在以下人员的帮助下做到了这一点
  • Android Espresso - 如果未选中,请单击复选框

    I have onView withId R id check box perform click 但我只想在尚未选中该复选框时执行此操作 我怎样才能在浓缩咖啡中做到这一点 我还想根据其之前的状态来切换复选框 开关 起初 我尝试用此方法打开

随机推荐