zoukankan      html  css  js  c++  java
  • Android实现边缘凹凸的View

    转载

    最近做项目的时候遇到一个卡劵的效果,由于自己觉得用图片来做的话可以会出现适配效果不好,再加上自己自定义view方面的知识比较薄弱,所以想试试用自定义View来实现。但是由于自己知识点薄弱,一开始居然想着用画矩形来设置边缘实现,后面一个哥们指导了我,在这里感谢他。

    实现分析

    上面的图片其实和普通的Linearlayout,RelativeLayout一样,只是上下两边多了类似于半圆锯齿的形状。那么只需要处理不同地方。可以在上下两条线上画一个个白色的小圆来实现这种效果。

    假如我们上下线的半圆以及半圆与半圆之间的间距是固定的,那么不同尺寸的屏幕肯定会画出不同数量的半圆,那么我们只需要根据控件的宽度来获取能画的半圆数。

    大家观察图片,很容易发现,圆的数量总是圆间距数量-1,也就是,假设圆的数量是circleNum,那么圆间距就是circleNum+1。

    所以我们可以根据这个计算出circleNum.

    circleNum = (int) ((w-gap)/(2*radius+gap));

    这里gap就是圆间距,radius是圆半径,w是view的宽。

    具体的实现

    下面看代码:

    上面定义了圆的半径和圆间距,同时初始化了这些值并且获取了需要画的圆数量。

    接下来只需要一个一个将圆画出来就可以了。

    简单的根据circleNum的数量进行了圆的绘制。

    这里remain/2是因为,可以一些情况,计算出来的可以画的数量不是刚好整除的。这样就会出现右边最后一个间距会比其它的间距都要宽。

    所以我们在绘制第一个的时候加上了余下的间距的一半,即使是不整除的情况。至少也能保证第一个和最后一个间距宽度一致。

    这样就实现了,看看布局文件:

    效果图:

    作者通过非常巧妙和简单的方式实现了边缘特殊图像的绘制,相信大家可以轻松的学习到相关知识,不过这里指定了特定的颜色(白色),如果想要直接像橡皮擦一样擦成透明,可以使用xfermode来做处理。

    源代码下载

    源代码

    参考链接

    一起来学习android自定义控件—边缘凹凸的View

    更新

    利用边缘凹凸的View来实现比较好看的editText

    效果如下

    这里写图片描述

    布局文件

    <?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="0dp"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="@color/backcircle"
        tools:context="com.zj.second.MainActivity">
    
    
        <com.zj.second.CouponDisplayView
            android:layout_marginRight="16dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:padding="0dp">
    
            <EditText
                android:padding="20dp"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="姓 名"
                android:background="@null"
                android:textSize="16sp"/>
    
            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="#FF909090" />
    
            <EditText
                android:padding="20dp"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="电 话"
                android:background="@null"
                android:textSize="16sp"/>
    
            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="#FF909090" />
    
            <EditText
                android:padding="20dp"
                android:textStyle="bold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="地 址"
                android:background="@null"
                android:textSize="16sp"/>
    
            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="#FF909090" />
    
        <EditText
    
            android:gravity="top|left"
            android:padding="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="案情描述"
            android:minLines="8"
            android:background="@null"
    
            />
        </com.zj.second.CouponDisplayView>
    
        <ImageView
    
            android:id="@+id/imageView_send"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:padding="0dp"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="168dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:src="@drawable/inline_reply_dialog_send"
    
            android:layout_alignParentRight="true"
    
            />
    
    </RelativeLayout>

    参考链接

    文本框 - Material Design 中文版 - 极客学院Wiki

  • 相关阅读:
    Educational Codeforces Round 3 B
    Educational Codeforces Round 3 A
    BestCoder Round #66 1002
    Codeforces Round #335 (Div. 2) B
    BestCoder Round #66 1001
    Codeforces Round #335 (Div. 2) C
    Linux加载DTS设备节点的过程(以高通8974平台为例)
    高通APQ8074 spi 接口配置
    摩托罗拉SE4500 德州仪器TI Omap37xx/AM3715/DM3730/AM3530 wince6.0/Windows Mobile 6.5平台 二维软解调试记录及相关解释
    摩托罗拉SE4500 三星 S3C6410 Wince6.0平台软解码调试记录以及驱动相关问题解释
  • 原文地址:https://www.cnblogs.com/jjx2013/p/6223648.html
Copyright © 2011-2022 走看看