zoukankan      html  css  js  c++  java
  • 三种自定义圆形按钮的方法

    占坑

    1、自定义的view,在onDraw方法里用canvas绘制一个圆。

    2、用ImageButton,然后背景传入一个圆形的图片。

    3、用shape编写形状,button里指定shape。

    只有方法3能有点击的阴影效果,方法1和2看不出点击效果

    一:自定义view

    画了圆之外,其实整个控件还是矩形的,必须让背景透明

    public class CircleView extends Button {
    
        int mColor = Color.GRAY;
    
        Paint paint;
    
        private void init(){
    
            paint = new Paint();
            paint.setColor(mColor);
    
        }
    
        public CircleView(Context context) {
            super(context);
            init();
    
        }
    
        public CircleView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            int width = getWidth();
            int height = getHeight();
            int radius = Math.min(width,height)/2;
            canvas.drawCircle(width/2,height/2,radius,paint);
    
        }
    
    
    
    }
    

    二:放入圆形的背景图即可,这里不展开说了

    三:编写shape文件

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape
    
        xmlns:android= "http://schemas.android.com/apk/res/android"
    
        android:shape= "oval"椭圆
    
        android:useLevel= "false" >
    
        <solid android:color= "#CC0000" />颜色
    
    
    
        <size android:width= "20dp"
    
            android:height= "20dp" />
    
    </shape>
    

    整个布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <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.administrator.circletest.MainActivity">
    
        <com.example.administrator.circletest.CircleView
            android:background="#00000000"透明
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/view" />
    
        
    
    
        <ImageButton
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/back"
            android:id="@+id/imageButton"
            android:layout_alignTop="@+id/view"
            android:layout_centerHorizontal="true" />
    
    
        <Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/circle"
            android:layout_alignTop="@+id/imageButton"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    
    </RelativeLayout>
    

  • 相关阅读:
    Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入(1)
    Linux系统级别能够打开的文件句柄的数file-max命令
    记:cloudstack--gluster主存储上的一个文件损坏导致SSVM启动失败
    Linux 之 inotify+rsync 备份文件系统
    为什么KVM计算机点无故重启?
    vim批量在文件每行添加内容以及查询cloudstack模板是否是增量
    记-cloudstack 更改二级存储
    apache 自定义404错误页面
    URL路由
    前端图片优化
  • 原文地址:https://www.cnblogs.com/wzben/p/6115692.html
Copyright © 2011-2022 走看看