您好,登錄后才能下訂單哦!
這篇文章主要介紹Android怎樣實現左上角傾斜的標簽效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
實現的思路大致如下圖:
主頁面的布局結構如下:
<?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怎樣實現左上角傾斜的標簽效果”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。