Android三角形自定义按钮

Android三角形自定义按钮,第1张

概述我想制作像thisquestion这样的2个对角三角形按钮.我怎样才能做到这一点?我应该制作一个带有矩形的可绘制xml并以某种方式旋转它吗?我是否应该在数学的帮助下制作图像并使其仅在三角形部分上可点击?解决方法:packagecom.example.buttonsView;importandroid.content.Context;

我想制作像this question这样的2个对角三角形按钮.

我怎样才能做到这一点?我应该制作一个带有矩形的可绘制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;    }}

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

输出:

总结

以上是内存溢出为你收集整理的Android三角形自定义按钮全部内容,希望文章能够帮你解决Android三角形自定义按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116156.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存