Android的三角形自定义按钮 [英] Android triangle custom buttons

查看:2676
本文介绍了Android的三角形自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打2角的三角形按钮,像这个问题

我怎样才能做到这一点?我应该用矩形绘制一个xml和旋转不知何故?我应该做的图像,使其可点击只与数学的帮助?

三角形零件
解决方案

 包com.example.buttonsView;进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.Path;
进口android.graphics.Path.FillType;
进口android.graphics.Point;
进口android.graphics.RectF;
进口android.graphics.Region;
进口android.util.AttributeSet;
进口android.view.MotionEvent;
进口android.view.View;公共类TwoButtons扩展视图{    私人路径路径;
    私人路径PATH1;
    专用区域区域;
    专用区域REGION1;
    私有静态诠释GAP = 10;    私人ButtonClickEvents buttonClickEvent;    公共接口ButtonClickEvents {
        公共无效redButtonClick();
        公共无效blueButtonClick();
    }    公共无效setOnButtonClickEvent(ButtonClickEvents buttonClickEvent){
        this.buttonClickEvent = buttonClickEvent;
    }    公共TwoButtons(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        在里面();
    }    私人无效的init(){    }
    @覆盖
    保护无效的onDraw(帆布油画){
        // TODO自动生成方法存根
        super.onDraw(画布);
        canvas.drawColor(Color.GRAY);
        涂料粉刷=新的油漆();
        paint.setColor(android.graphics.Color.BLACK);
        canvas.drawPaint(油漆);        paint.setStrokeWidth(0);        paint.setColor(android.graphics.Color.RED);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias​​(真);        a点=新的点(GAP,GAP);
        b点=新的点(GAP,的getHeight() - 2 * GAP);
        c点=新的点(的getWidth() - 2 * GAP,GAP);        路径=新路径();
        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(路径,油漆);        RectF rectF =新RectF();
        path.computeBounds(rectF,真);
        区域=新区域();
        region.setPath(路径,新的地区((INT)rectF.left,(INT)rectF.top,(INT)rectF.right,(INT)rectF.bottom));        paint.setColor(Color.BLUE);
        点A1 =新的点(的getWidth() - GAP,的getHeight() - GAP);
        点B1 =新的点(的getWidth() - GAP,2 * GAP);
        C1点=新的点(2 * GAP,的getHeight() - GAP);        PATH1 =新路径();
        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,油漆);        RectF rectF1 =新RectF();
        path1.computeBounds(rectF1,真);
        REGION1 =新区域();
        region1.setPath(PATH1,新的地区((INT)rectF1.left,(INT)rectF1.top,(INT)rectF1.right,(INT)rectF1.bottom));
    }    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){
        // TODO自动生成方法存根
        开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:            逐点=新点();
            point.x =(int)的event.getX();
            point.y =(int)的event.getY();            无效();
            如果(region.contains((INT)point.x,(INT)point.y))
            {
                如果(buttonClickEvent!= NULL)
                    buttonClickEvent.redButtonClick();
            }否则如果(region1.contains((INT)point.x,(INT)point.y))
            {
                如果(buttonClickEvent!= NULL)
                    buttonClickEvent.blueButtonClick();
            }        返回true;
        }        返回false;
    }}

activity_main.xml中

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=com.example.buttonsView.MainActivity>    < com.example.buttonsView.TwoButtons
        机器人:ID =@ + ID / twoButtons1
        机器人:layout_width =200dp
        机器人:layout_height =200dp
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真/>< / RelativeLayout的>

MainActivity.java

 包com.example.buttonsView;进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.Toast;。进口com.afbb preferencessample.R;
进口com.example.buttonsView.TwoButtons.ButtonClickEvents;公共类MainActivity延伸活动{    TwoButtons按钮;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        按钮=(TwoButtons)findViewById(R.id.twoButtons1);
        buttons.setOnButtonClickEvent(新ButtonClickEvents(){            @覆盖
            公共无效redButtonClick(){
                // TODO自动生成方法存根
                Toast.makeText(getApplicationContext(),红,
                        Toast.LENGTH_SHORT).show();            }            @覆盖
            公共无效blueButtonClick(){
                // TODO自动生成方法存根
                Toast.makeText(getApplicationContext(),蓝,
                        Toast.LENGTH_SHORT).show();            }
        });    }}

输出:

I want to make 2 diagonal triangle buttons like in this question.

How can I achieve this? Should I make a drawable xml with a rectangle and rotate it somehow? Should I make an image and make it clickable only on the triangle parts with the help of mathematics?

解决方案

    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;
    }

}

activity_main.xml

<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 :

这篇关于Android的三角形自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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