[6.2.0]调整小智语音消息盒子样式
This commit is contained in:
@@ -20,6 +20,7 @@ import com.mogo.eagle.core.function.call.msgbox.CallerMsgBoxManager
|
||||
import com.mogo.eagle.core.function.hmi.R
|
||||
import com.mogo.eagle.core.utilcode.util.TimeUtils
|
||||
import com.mogo.eagle.core.utilcode.util.TimeUtils.getHourMinFormat
|
||||
import com.mogo.eagle.core.widget.AlignTextView
|
||||
import com.mogo.eagle.core.widget.RoundCanClickConstraintLayout
|
||||
|
||||
/**
|
||||
@@ -209,7 +210,7 @@ class PassengerMsgBoxBubbleAdapter(private val activity: Activity): RecyclerView
|
||||
|
||||
//小智语音消息
|
||||
class BubbleVoiceHolder(itemView: View): RecyclerView.ViewHolder(itemView){
|
||||
var tvPassengerVoiceContent: TextView = itemView.findViewById(R.id.tvPassengerVoiceContent)
|
||||
var tvPassengerVoiceContent: AlignTextView = itemView.findViewById(R.id.tvPassengerVoiceContent)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,13 +12,16 @@
|
||||
android:paddingEnd="@dimen/dp_20"
|
||||
>
|
||||
|
||||
<TextView
|
||||
<com.mogo.eagle.core.widget.AlignTextView
|
||||
android:id="@+id/tvPassengerVoiceContent"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="@dimen/sp_32"
|
||||
android:textColor="#243959"
|
||||
android:layout_margin="@dimen/dp_54"
|
||||
android:layout_marginTop="@dimen/dp_54"
|
||||
android:layout_marginBottom="@dimen/dp_54"
|
||||
android:layout_marginStart="@dimen/dp_49"
|
||||
android:layout_marginEnd="@dimen/dp_49"
|
||||
android:textStyle="bold"
|
||||
android:gravity="start"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.mogo.eagle.core.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.text.Layout;
|
||||
import android.text.StaticLayout;
|
||||
import android.text.TextPaint;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.AppCompatTextView;
|
||||
|
||||
public class AlignTextView extends AppCompatTextView {
|
||||
private boolean alignOnlyOneLine;
|
||||
|
||||
public AlignTextView(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
public AlignTextView(Context context, @Nullable AttributeSet attrs) {
|
||||
this(context, attrs, 0);
|
||||
}
|
||||
|
||||
public AlignTextView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlignTextView);
|
||||
alignOnlyOneLine = typedArray.getBoolean(R.styleable.AlignTextView_alignOnlyOneLine, false);
|
||||
typedArray.recycle();
|
||||
}
|
||||
|
||||
protected void onDraw(Canvas canvas) {
|
||||
|
||||
TextPaint paint = getPaint();
|
||||
paint.setColor(getCurrentTextColor());
|
||||
paint.drawableState = getDrawableState();
|
||||
|
||||
CharSequence content = getText();
|
||||
if (!(content instanceof String)) {
|
||||
super.onDraw(canvas);
|
||||
return;
|
||||
}
|
||||
String text = (String) content;
|
||||
Layout layout = getLayout();
|
||||
|
||||
for (int i = 0; i < layout.getLineCount(); ++i) {
|
||||
int lineBaseline = layout.getLineBaseline(i) + getPaddingTop();
|
||||
int lineStart = layout.getLineStart(i);
|
||||
int lineEnd = layout.getLineEnd(i);
|
||||
if (alignOnlyOneLine && layout.getLineCount() == 1) {//只有一行
|
||||
String line = text.substring(lineStart, lineEnd);
|
||||
float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, paint);
|
||||
this.drawScaledText(canvas, line, lineBaseline, width, paint);
|
||||
} else if (i == layout.getLineCount() - 1) {//最后一行
|
||||
canvas.drawText(text.substring(lineStart), getPaddingLeft(), lineBaseline, paint);
|
||||
break;
|
||||
} else {//中间行
|
||||
String line = text.substring(lineStart, lineEnd);
|
||||
float width = StaticLayout.getDesiredWidth(text, lineStart, lineEnd, paint);
|
||||
this.drawScaledText(canvas, line, lineBaseline, width, paint);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void drawScaledText(Canvas canvas, String line, float baseLineY, float lineWidth, TextPaint paint) {
|
||||
|
||||
if (line.length() < 1) {
|
||||
return;
|
||||
}
|
||||
float x = getPaddingLeft();
|
||||
boolean forceNextLine = line.charAt(line.length() - 1) == 10;
|
||||
int length = line.length() - 1;
|
||||
if (forceNextLine || length == 0) {
|
||||
canvas.drawText(line, x, baseLineY, paint);
|
||||
return;
|
||||
}
|
||||
|
||||
float d = (getMeasuredWidth() - lineWidth - getPaddingLeft() - getPaddingRight()) / length;
|
||||
|
||||
for (int i = 0; i < line.length(); ++i) {
|
||||
String c = String.valueOf(line.charAt(i));
|
||||
float cw = StaticLayout.getDesiredWidth(c, paint);
|
||||
canvas.drawText(c, x, baseLineY, paint);
|
||||
x += cw + d;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,4 +28,9 @@
|
||||
</attr>
|
||||
|
||||
</declare-styleable>
|
||||
|
||||
<declare-styleable name="AlignTextView">
|
||||
<attr name="alignOnlyOneLine" format="boolean"/>
|
||||
</declare-styleable>
|
||||
|
||||
</resources>
|
||||
Reference in New Issue
Block a user