带下波浪线的textView

编程入门 行业动态 更新时间:2024-10-22 02:39:54

带下<a href=https://www.elefans.com/category/jswz/34/1769735.html style=波浪线的textView"/>

带下波浪线的textView

前言

先上图

 再上代码  

package com.example.myapplication3;import android.annotation.SuppressLint;
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.util.AttributeSet;import android.view.View;import androidx.annotation.Nullable;import java.util.HashMap;
import java.util.Map;public class WaveLineTextView extends View {int waveLineColor;String allText;int textColor;int textSize;private Paint mTextPaint;private Paint mWaveLinePaint;int waveLineWidth;int waveWidth; //波長int waveAmplitude; //振幅int lineDistanceToText = 10; //文字和波浪线距离int textdistanceToText = 10; //文字和波浪线距离float textHeight; //文字高度Map<Integer, String> textMap = new HashMap<>();String[] TextsNeedToDrawWaveLine;private int left;private int right;boolean isAllWithWaveLine;public boolean isAllWithWaveLine() {return isAllWithWaveLine;}public void setAllWithWaveLine(boolean allWithWaveLine) {isAllWithWaveLine = allWithWaveLine;}public void setWaveLineColor(int waveLineColor) {this.waveLineColor = waveLineColor;mWaveLinePaint.setColor(waveLineColor);invalidate();}public void setAllText(String allText) {this.allText = allText;invalidate();}public void setTextSize(int textSize) {this.textSize = textSize;mTextPaint.setTextSize(textSize);invalidate();}public void setTextColor(int textColor) {this.textColor = textColor;mTextPaint.setColor(textColor);invalidate();}public void setTextsNeedToDrawWaveLine(String[] textsNeedToDrawWaveLine) {TextsNeedToDrawWaveLine = textsNeedToDrawWaveLine;invalidate();}public WaveLineTextView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TextViewWithWaveLine);textColor = attributes.getColor(R.styleable.TextViewWithWaveLine_text_color, Color.BLACK);textSize = attributes.getColor(R.styleable.TextViewWithWaveLine_text_size, 40);waveLineColor = attributes.getColor(R.styleable.TextViewWithWaveLine_wave_line_color, Color.RED);waveLineWidth = attributes.getInt(R.styleable.TextViewWithWaveLine_wave_line_width, 3);waveWidth = attributes.getInt(R.styleable.TextViewWithWaveLine_wave_width, 20);waveAmplitude = attributes.getInt(R.styleable.TextViewWithWaveLine_wave_amplitude, 4);initPaint();}private void initPaint() {mTextPaint = new Paint();mTextPaint.setColor(textColor);mTextPaint.setAntiAlias(true);mTextPaint.setTextSize(textSize);mWaveLinePaint = new Paint();mWaveLinePaint.setStyle(Paint.Style.STROKE);mWaveLinePaint.setColor(waveLineColor);mWaveLinePaint.setAntiAlias(true);mWaveLinePaint.setStrokeWidth(waveLineWidth);textHeight = Math.abs(mTextPaint.descent()) + Math.abs(mTextPaint.ascent());}@SuppressLint("DrawAllocation")@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);drawText(canvas);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);this.left = left;this.right = right;}private void drawText(Canvas canvas) {for (int i = 0; i < TextsNeedToDrawWaveLine.length; i++) {String text = TextsNeedToDrawWaveLine[i];int i1 = allText.indexOf(text);if (i1 != -1) {textMap.put(i1, text);}}int paddingLeft = getPaddingLeft();int paddingRight = getPaddingRight();float width = right - left - (paddingLeft + paddingRight);//文字绘制区的宽度float charStartX = paddingLeft;float textStartY = textHeight;float charWidths = 0; //单行已绘制的字母长度和for (int i = 0; i < allText.length(); i++) {String singleChar = String.valueOf(allText.charAt(i));float charWidth = mTextPaint.measureText(singleChar); //单个字母长度if (charStartX + charWidths + charWidth > width + paddingLeft) { //文字需要换行if (isAllWithWaveLine && charStartX + charWidths <= width + paddingLeft) { //整行加上波浪线drawWaveLine(canvas, waveAmplitude, textStartY + lineDistanceToText + lineDistanceToText, charStartX, charStartX + charWidths, waveWidth);charStartX = paddingLeft;charWidths = 0;textStartY += textHeight + textdistanceToText;canvas.drawText(singleChar, charStartX, textStartY, mTextPaint);} else {charStartX = paddingLeft;textStartY += textHeight + textdistanceToText;charWidths = 0;canvas.drawText(singleChar, charStartX, textStartY, mTextPaint);if (textMap.containsKey(i)) {drawWaveLine(canvas, textMap.get(i), mTextPaint, width, charStartX, textStartY);}}} else {canvas.drawText(singleChar, charStartX + charWidths, textStartY, mTextPaint);if (isAllWithWaveLine) {if (i == allText.length() - 1) { //最后一句不换行drawWaveLine(canvas, waveAmplitude, textStartY + lineDistanceToText + lineDistanceToText, charStartX, charStartX + charWidths, waveWidth);}} else {if (textMap.containsKey(i)) {drawWaveLine(canvas, textMap.get(i), mTextPaint, width, charStartX + charWidths, textStartY);}}}charWidths = charWidths + charWidth;}}private float drawWaveLine(Canvas canvas, String text, Paint paint, float width, float charStartX, float textStartY) {float textWidth = paint.measureText(text);float lineLength = charStartX + textWidth;int paddingLeft = getPaddingLeft();if ((lineLength) <= width + paddingLeft) {//波浪线文字不需要換行drawWaveLine(canvas, waveAmplitude, textStartY + lineDistanceToText + lineDistanceToText, charStartX, lineLength, waveWidth);} else {float chartWidthS = 0;for (int i3 = 0; i3 < text.length(); i3++) {String value = String.valueOf(text.charAt(i3));float charWidth = paint.measureText(value);if (charStartX + chartWidthS <= width + paddingLeft && (charStartX + chartWidthS + charWidth) > width + paddingLeft) { //一個單詞 下一個字母需要換行drawWaveLine(canvas, waveAmplitude, textStartY + lineDistanceToText + lineDistanceToText, charStartX, charStartX + chartWidthS, waveWidth);String substring = text.substring(i3);//获取换行的字符串float needToDrawWidth = paint.measureText(substring);//获取换行的波浪线宽度charStartX = paddingLeft;drawWaveLine(canvas, waveAmplitude, textStartY + textHeight + lineDistanceToText + lineDistanceToText + textdistanceToText, charStartX, charStartX + needToDrawWidth, waveWidth);return charStartX;}chartWidthS = chartWidthS + charWidth;}}return charStartX;}private void drawWaveLine(Canvas canvas, int amplitude, float height, float index, float needToDrawWidth, int waveWidth) {Path mPath = new Path();float intx = index;while (intx <= needToDrawWidth) {float endY = (float) (Math.sin((float) intx / (float) waveWidth * 2f * Math.PI)* (float) amplitude + height - amplitude);if (intx == index) {mPath.moveTo(intx, endY);} else {mPath.lineTo(intx, endY);}intx++;}canvas.drawPath(mPath, mWaveLinePaint);}
}
  <com.example.myapplication3.WaveLineTextViewandroid:id="@+id/wv2"android:layout_width="match_parent"android:layout_height="200dp"android:layout_marginTop="20px"android:layout_marginBottom="20px"android:padding="20dp" />
        final WaveLineTextView wv = findViewById(R.id.wv);wv.setAllText("If you place a bid on an item,you enter a contractual agreement to buy it if you win the auction. All auctions have minimum starting bids, and some have a reserve price-a secret minimum amount the seller is willing to accept for the item. If the bidding doesn't reach the reserve price, the seller doesn't have to partwith the item. In addition to auctions, you can find tons of fixed-price items on eBay that make shopping there just like shopping at any other online marketplace. You see what you like, you buy it, you pay for it and you wait for it to arrive at your door.");String[] TextsNeedToDrawWaveLine={"contractual","price-a","doesn't"};//全部需要绘制下划线的字符串wv.setTextsNeedToDrawWaveLine(TextsNeedToDrawWaveLine);wv.setAllWithWaveLine(false);//是否全部需要绘制下划线

更多推荐

带下波浪线的textView

本文发布于:2023-06-20 23:52:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/808827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:波浪   带下   textView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!