Android实现左上角(其他边角)倾斜的标签(环绕效果)效果

所属分类: 软件编程 / Android 阅读数: 52
收藏 0 赞 0 分享

先上效果图吧

在这里插入图片描述

由于项目需要实现这种左上角倾斜环绕的标签效果,所以自己尝试着做一做,并记录下来。

实现的思路大致如下图:

在这里插入图片描述

主页面的布局结构如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#fff"
  android:layout_height="match_parent"
  tools:context=".MainActivity">
  <RelativeLayout
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:background="#fff"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" >
    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="5dp"
      android:background="#33B7F3"
      android:elevation="2dp"></RelativeLayout>
    <com.zc.labeldemo.LabelView
      android:id="@+id/labelView"
      android:layout_alignParentTop="true"
      android:layout_width="75dp"
      android:elevation="3dp"
      android:layout_height="75dp"/>
  </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

绘制倾斜标签的代码如下:

package com.zc.labeldemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
/**
 * @author wenchao
 * @version 1.0.1
 * @className LabelView
 * @date 2019/9/20
 * @description
 */
public class LabelView extends View {
  /**
   * 画笔
   */
  private Paint paint;
  /**
   * Path
   */
  private Path path;
  /**
   * View宽度
   */
  private float width;
  /**
   * View高度
   */
  private float height;
  /**
   * 标签背景宽度
   */
  private float labelWidth;
  /**
   * 标签折叠区域宽度
   */
  private float pointWidth;
  /**
   * 标签折叠区域高度
   */
  private float pointHeight;
  /**
   * 标签背景颜色
   */
  private int labelColor;
  /**
   * 标签折叠区域背景颜色
   */
  private int pointColor;
  /**
   * 中心点x坐标
   */
  private float centerX;
  /**
   * 中心点y坐标
   */
  private float centerY;
  /**
   * 标签文字内容
   */
  private String text;
  /**
   * 标签文字颜色
   */
  private int textColor;
  public LabelView(Context context) {
    super(context);
  }
  public LabelView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }
  private void init() {
    labelWidth = 120;
    pointWidth = 10;
    pointHeight = 17;
    paint = new Paint();
    path = new Path();
    paint.setAntiAlias(true);
    paint.setStrokeWidth(10);
    setBackgroundColor(Color.TRANSPARENT);
    labelColor = Color.parseColor("#EA6724");
    pointColor = Color.parseColor("#C43200");
    text = "测试内容";
    textColor = Color.WHITE;
  }
  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = w;
    height = h;
    centerX = w/2;
    centerY = h/2;
  }
  @Override
  public void draw(Canvas canvas) {
    super.draw(canvas);
    //画标签区域背景上边的折叠区域(小三角区域)
    path.reset();
    path.moveTo(width-pointWidth,0);
    path.lineTo(width,pointHeight);
    path.lineTo(width-pointWidth-26,pointHeight);
    path.close();
    paint.setColor(pointColor);
    canvas.drawPath(path,paint);
    //画标签背景区域下边的折叠区域
    path.reset();
    path.moveTo(0,height-pointWidth);
    path.lineTo(pointHeight,height);
    path.lineTo(pointHeight,height-pointWidth-26);
    path.close();
    paint.setColor(pointColor);
    canvas.drawPath(path,paint);
    //画标签背景区域
    path.reset();
    paint.setColor(labelColor);
    paint.setStyle(Paint.Style.FILL);
    path.moveTo(width-labelWidth-pointWidth,0);
    path.lineTo(width-pointWidth,0);
    path.lineTo(0,height-pointWidth);
    path.lineTo(0,height-labelWidth-pointWidth);
    canvas.drawPath(path,paint);
    //画文字 逆时针选择45度
    canvas.rotate(-45,centerX,centerY);
    //文字中心点横坐标
    float textX = width / 2;
    //文字中心点纵坐标
    float textY = (height-pointWidth-(labelWidth / 2f)) / 2f;
    paint.setColor(textColor);
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(38);
    //设置文字居中绘制
    paint.setTextAlign(Paint.Align.CENTER);
    canvas.drawText(text,textX,textY,paint);
  }
}

这个标签实现应该是比较简单的,而且多嵌套一层布局会消耗一定的资源,这里先简单记录一下实现的思路,后期有时间再做更改优化。下面再贴一张其他边角的效果图吧:

在这里插入图片描述

以上所述是小编给大家介绍的Android实现左上角(其他边角)倾斜的标签(环绕效果)效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

更多精彩内容其他人还在看

Android 自定义球型水波纹带圆弧进度效果(实例代码)

最近小编接到一个这样的需求,需要实现一个圆形水波纹,带进度,两层水波纹需要渐变显示,且外围有一个圆弧进度。今天小编给大家分享实例代码,感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Flutter 实现下拉刷新上拉加载的示例代码

这篇文章主要介绍了Flutter 实现下拉刷新上拉加载的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Windows实现Flutter环境搭建及配置这一篇就够了

这篇文章主要介绍了Windows实现Flutter环境搭建及配置这一篇就够了,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android利用碎片fragment实现底部标题栏(Github模板开源)

Fragment可以作为Activity的组成部分,一个Activity可以有多个Fragment,这篇文章主要介绍了Android利用碎片fragment实现底部标题栏(Github模板开源),需要的朋友可以参考下
收藏 0 赞 0 分享

android studio 的下拉菜单Spinner使用详解

这篇文章主要介绍了android studio 的下拉菜单Spinner使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解析Android 8.1平台SystemUI 导航栏加载流程

这篇文章主要介绍了Android 8.1平台SystemUI 导航栏加载流程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信录音功能

这篇文章主要为大家详细介绍了Android仿微信录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信键盘切换效果

这篇文章主要为大家详细介绍了Android仿微信键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android超清晰6.0权限申请AndPermission

这篇文章主要介绍了Android超清晰6.0权限申请AndPermission,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿微信录制语音功能

这篇文章主要介绍了Android仿微信录制语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多