CornerViewLayout & CornerView(标签)

右下角角标文本,斜体文本,标签文本,使用CornerViewLayout作为父布局,则在该父布局右下角绘制三角形角标以及旋转的文本,或单独使用CornerView作为一个子控件,放置在某个布局右下角实现三角形角标和旋转文本
2017-7-26更新
为使角标绘制在顶层,调整至dispatchDraw方法中绘制;同时调整了一下字号,当字体个数小于3时字体减小5,如果不减小的话有点偏大不大好看。

调整前效果图
调整后效果图

可自定义角标大小,文本大小自适应
xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.forens.xuan.forkit.CornerActivity">

    <com.forens.xuan.forkit.view.CornerViewLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="#f1f1f1"
        app:cornerColor="#a4a4a4"
        app:cornerSize="30dp"
        app:cornerText="已过期">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:src="@mipmap/ic_launcher" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="优惠券名称"
                    android:textColor="@android:color/black"
                    android:textSize="18sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="优惠券使用日期" />
            </LinearLayout>

        </LinearLayout>
    </com.forens.xuan.forkit.view.CornerViewLayout>

    <com.forens.xuan.forkit.view.CornerViewLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="#f1f1f1"
        app:cornerColor="#a4a4a4"
        app:cornerSize="50dp"
        app:cornerText="已过期">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:src="@mipmap/ic_launcher" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="优惠券名称"
                    android:textColor="@android:color/black"
                    android:textSize="18sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="优惠券使用日期" />
            </LinearLayout>

        </LinearLayout>
    </com.forens.xuan.forkit.view.CornerViewLayout>

    <com.forens.xuan.forkit.view.CornerViewLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:background="#f1f1f1"
        app:cornerColor="@color/colorAccent"
        app:cornerSize="50dp"
        app:cornerText="有效">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:src="@mipmap/ic_launcher" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="优惠券名称"
                    android:textColor="@android:color/black"
                    android:textSize="18sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="优惠券使用日期" />
            </LinearLayout>

        </LinearLayout>
    </com.forens.xuan.forkit.view.CornerViewLayout>

    <RelativeLayout
        android:background="#f1f1f1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:src="@mipmap/ic_launcher" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/image"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="优惠券名称"
                android:textColor="@android:color/black"
                android:textSize="18sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="优惠券使用日期" />
        </LinearLayout>

        <com.forens.xuan.forkit.view.CornerView
            android:layout_alignParentBottom="true"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            app:cornerColor="@color/colorAccent"
            app:cornerText="可用"/>
    </RelativeLayout>
</LinearLayout>

CornerViewLayout.java

package com.forens.xuan.forkit.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.forens.xuan.forkit.R;


/**
 * Project_Name:
 * Copyright:
 * Created:         dake.xuan on 2017/7/25 11:47
 * E-mail:          1129305620@qq.com
 * Desc:            (说明描述)
 */

public class CornerViewLayout extends FrameLayout{
    private String cornerContent;
    private int cornerColor;
    private float cornerSize;

    public CornerViewLayout(@NonNull Context context) {
        super(context);
    }

    public CornerViewLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initCornerViewLayout(context,attrs);
    }

    public CornerViewLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCornerViewLayout(context,attrs);
    }
    private void initCornerViewLayout(Context context,AttributeSet attrs){

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CornerView);
        cornerContent=a.getString(R.styleable.CornerView_cornerText);
        cornerColor=a.getColor(R.styleable.CornerView_cornerColor, Color.parseColor("#a4a4a4"));
        cornerSize=a.getDimensionPixelSize(R.styleable.CornerView_cornerSize,50);
        a.recycle();
        setWillNotDraw(false);
    }
     @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if(cornerContent==null)return;
        int height=getHeight();
        if(height<cornerSize){
            getLayoutParams().height= (int) cornerSize;
            getLayoutParams().width= (int) cornerSize;
        }


        //先填充右下角三角形颜色
        Path path1=new Path();
        path1.moveTo(getWidth(),getHeight()-cornerSize);
        path1.lineTo(getWidth()-cornerSize,getHeight());
        path1.lineTo(getWidth(),getHeight());
        Paint paint1=new Paint();
        paint1.setColor(cornerColor);
        canvas.drawPath(path1,paint1);

        //沿路径绘制文本
        Path path=new Path();
        path.moveTo(getWidth()-cornerSize/2,getHeight());
        path.lineTo(getWidth(),getHeight()-cornerSize/2);
        Paint textPaint=new Paint();
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.WHITE);
        double m=(Math.sqrt(2)*cornerSize)/2;
        float size= (float) (m/cornerContent.length());//根据实际路径长度求得字体大小
        textPaint.setTextSize(cornerContent.length()<3?size-5:size);

        canvas.drawTextOnPath(cornerContent,path,0, 0,textPaint);

    }
}

CornerView.java

package com.forens.xuan.forkit.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.forens.xuan.forkit.R;


/**
 * Project_Name:
 * Copyright:
 * Created:         dake.xuan on 2017/7/25 14:17
 * E-mail:          1129305620@qq.com
 * Desc:            (说明描述)
 */

public class CornerView extends View{
    private String cornerContent;
    private int cornerColor;
    public CornerView(Context context) {
        super(context);
    }

    public CornerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initCornerView(context,attrs);
    }

    public CornerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCornerView(context,attrs);
    }
    private void initCornerView(Context context,AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CornerView);
        cornerContent=a.getString(R.styleable.CornerView_cornerText);
        cornerColor=a.getColor(R.styleable.CornerView_cornerColor, Color.parseColor("#a4a4a4"));
        a.recycle();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //以宽度限定宽高
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(cornerContent==null)return;
        //先填充右下角三角形颜色
        Path path1=new Path();
        path1.moveTo(getWidth(),0);
        path1.lineTo(0,getHeight());
        path1.lineTo(getWidth(),getHeight());
        Paint paint1=new Paint();
        paint1.setColor(cornerColor);
        canvas.drawPath(path1,paint1);


        Paint textPaint=new Paint();
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setAntiAlias(true);
        textPaint.setColor(Color.WHITE);
        double m=(Math.sqrt(2)*getHeight())/2;
        float size= (float) (m/cornerContent.length());//根据实际路径长度求得字体大小
        textPaint.setTextSize(size);
        //沿路径绘制文本
        Path path=new Path();
        path.moveTo(getHeight()/2,getHeight());
        path.lineTo(getHeight(),getHeight()/2);
        canvas.drawTextOnPath(cornerContent,path,0, 0,textPaint);
    }
}

styleable

<declare-styleable name="CornerView">
        <attr name="cornerSize" format="dimension" />
        <attr name="showCorner" format="boolean"/>
        <attr name="cornerColor" format="color"/>
        <attr name="cornerText" format="string"/>
    </declare-styleable>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容

  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,684评论 22 664
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,266评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,982评论 4 60
  • 新西兰毛利人的医药(一):草药 新西兰的土著毛利人据信已经在本地居住超过800年的时间,根据毛利人自己的口传史则可...
    简明空阅读 5,513评论 2 2
  • 最近看了一部电视剧《煮妇神探》,由李小璐贾乃亮夫妻俩出演的苟吉祥毛儒毅因为两人的家庭背景、身份地位等相差极大的原因...
    肖小二啊阅读 228评论 0 0