- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
String dis = "";
String itemName = pdtList.get(position).getItemName();
if (itemName.contains("MG") || itemName.contains("GM") || itemName.contains("ML") || itemName.contains("MCG")) {
String[] itemNameLength = itemName.split("\\s+");
for (int i = 0; i < itemNameLength.length; i++) {
if (itemNameLength[i].contains("MG") || itemNameLength[i].contains("GM") || itemNameLength[i].contains("ML") || itemNameLength[i].contains("MCG")) {
if(itemName.contains(" ")){
int i1 = itemName.indexOf(" "); // 4
String word = itemName.substring(0, i1);
dis = word + "\n" + itemNameLength[i];
System.out.println(" itemName looping value is ---- " + dis);
break;
}
}
}
} else {
char first = itemName.charAt(0);
dis = first + "";
}
System.out.println(" itemName value ---- " + dis);
System.out.println(" itemName value size---- " + dis.length());
TextDrawable drawable = TextDrawable.builder()
.beginConfig()
.withBorder(4,"#19a4cf") /* thickness in px */
.fontSize((dis.contains("MG") || dis.contains("GM") || dis.contains("ML") || dis.contains("MCG") ? 30 : 75))
.textColor(getResources().getColor(R.color.text))
.endConfig()
.buildRoundRect(dis, getResources().getColor(R.color.tabname), 10);
holder.image_view.setImageDrawable(drawable);
将多行文本值设置为 imageview 不起作用。上面是我的代码,文本值不是在多行中设置,而是在单行中设置。
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.RoundRectShape;
public class TextDrawable extends ShapeDrawable {
private static final float SHADE_FACTOR = 0.9f;
private final Paint textPaint;
private final Paint borderPaint;
private final String text;
private final int color;
private final int borderColor;
private final RectShape shape;
private final int height;
private final int width;
private final int fontSize;
private final float radius;
private final int borderThickness;
private TextDrawable(Builder builder) {
super(builder.shape);
// shape properties
shape = builder.shape;
height = builder.height;
width = builder.width;
radius = builder.radius;
// text and color
text = builder.toUpperCase ? builder.text.toUpperCase() : builder.text;
color = builder.color;
borderColor = builder.borderColor;
// text paint settings
fontSize = builder.fontSize;
textPaint = new Paint();
textPaint.setColor(builder.textColor);
textPaint.setAntiAlias(true);
textPaint.setFakeBoldText(builder.isBold);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTypeface(builder.font);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setStrokeWidth(builder.borderThickness);
// border paint settings
borderThickness = builder.borderThickness;
borderPaint = new Paint();
//borderPaint.setColor(getDarkerShade(color));
borderPaint.setColor(borderColor);
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderThickness);
//borderPaint.set
// drawable paint color
Paint paint = getPaint();
paint.setColor(color);
}
public static IShapeBuilder builder() {
return new Builder();
}
private int getDarkerShade(int color) {
return Color.rgb((int) (SHADE_FACTOR * Color.red(color)),
(int) (SHADE_FACTOR * Color.green(color)),
(int) (SHADE_FACTOR * Color.blue(color)));
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
Rect r = getBounds();
// draw border
if (borderThickness > 0) {
drawBorder(canvas);
}
int count = canvas.save();
canvas.translate(r.left, r.top);
// draw text
int width = this.width < 0 ? r.width() : this.width;
int height = this.height < 0 ? r.height() : this.height;
int fontSize = this.fontSize < 0 ? (Math.min(width, height) / 2) : this.fontSize;
textPaint.setTextSize(fontSize);
canvas.drawText(text, width / 2, height / 2 - ((textPaint.descent() + textPaint.ascent()) / 2), textPaint);
canvas.restoreToCount(count);
}
private void drawBorder(Canvas canvas) {
RectF rect = new RectF(getBounds());
rect.inset(borderThickness / 2, borderThickness / 2);
//rect.inset(borderThickness, borderThickness);
if (shape instanceof OvalShape) {
canvas.drawOval(rect, borderPaint);
} else if (shape instanceof RoundRectShape) {
canvas.drawRoundRect(rect, radius, radius, borderPaint);
} else {
canvas.drawRect(rect, borderPaint);
}
}
@Override
public void setAlpha(int alpha) {
textPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
textPaint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public int getIntrinsicWidth() {
return width;
}
@Override
public int getIntrinsicHeight() {
return height;
}
public interface IConfigBuilder {
public IConfigBuilder width(int width);
public IConfigBuilder height(int height);
public IConfigBuilder textColor(int color);
public IConfigBuilder withBorder(int thickness, int borderColor);
public IConfigBuilder withBorder(int thickness, String borderColor);
public IConfigBuilder useFont(Typeface font);
public IConfigBuilder fontSize(int size);
public IConfigBuilder bold();
public IConfigBuilder toUpperCase();
public IShapeBuilder endConfig();
}
public static interface IBuilder {
public TextDrawable build(String text, int color);
}
public static interface IShapeBuilder {
public IConfigBuilder beginConfig();
public IBuilder rect();
public IBuilder round();
public IBuilder roundRect(int radius);
public TextDrawable buildRect(String text, int color);
public TextDrawable buildRoundRect(String text, int color, int radius);
public TextDrawable buildRound(String text, int color);
}
public static class Builder implements IConfigBuilder, IShapeBuilder, IBuilder {
public int textColor;
public float radius;
private String text;
private int color;
private int borderColor;
private int borderThickness;
private int width;
private int height;
private Typeface font;
private RectShape shape;
private int fontSize;
private boolean isBold;
private boolean toUpperCase;
private Builder() {
text = "";
color = Color.GRAY;
borderColor = Color.RED;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
public IConfigBuilder width(int width) {
this.width = width;
return this;
}
public IConfigBuilder height(int height) {
this.height = height;
return this;
}
public IConfigBuilder textColor(int color) {
this.textColor = color;
return this;
}
public IConfigBuilder withBorder(int thickness, int borderColor) {
this.borderThickness = thickness;
this.borderColor = borderColor;
return this;
}
public IConfigBuilder withBorder(int thickness, String borderColor) {
this.borderThickness = thickness;
this.borderColor = Color.parseColor(borderColor);
return this;
}
public IConfigBuilder useFont(Typeface font) {
this.font = font;
return this;
}
public IConfigBuilder fontSize(int size) {
this.fontSize = size;
return this;
}
public IConfigBuilder bold() {
this.isBold = true;
return this;
}
public IConfigBuilder toUpperCase() {
this.toUpperCase = true;
return this;
}
@Override
public IConfigBuilder beginConfig() {
return this;
}
@Override
public IShapeBuilder endConfig() {
return this;
}
@Override
public IBuilder rect() {
this.shape = new RectShape();
return this;
}
@Override
public IBuilder round() {
this.shape = new OvalShape();
return this;
}
@Override
public IBuilder roundRect(int radius) {
this.radius = radius;
float[] radii = {radius, radius, radius, radius, radius, radius, radius, radius};
this.shape = new RoundRectShape(radii, null, null);
return this;
}
@Override
public TextDrawable buildRect(String text, int color) {
rect();
return build(text, color);
}
@Override
public TextDrawable buildRoundRect(String text, int color, int radius) {
roundRect(radius);
return build(text, color);
}
@Override
public TextDrawable buildRound(String text, int color) {
round();
return build(text, color);
}
@Override
public TextDrawable build(String text, int color) {
this.color = color;
this.text = text;
return new TextDrawable(this);
}
}
}
这是textdrawable 类。请帮助我解决这个问题。提前致谢。
最佳答案
我自己解决了这个问题。您需要添加此逻辑来设置多行文本。在draw()中需要更新如下。
int count = canvas.save();
canvas.translate(r.left, r.top);
int width = this.width < 0 ? r.width() : this.width;
int height = this.height < 0 ? r.height() : this.height;
int fontSize = this.fontSize < 0 ? (Math.min(width, height) / 2) : this.fontSize;
textPaint.setTextSize(fontSize);
String[] lines = text.split("\n");
int yoff = 0;
for (int i = 0; i < lines.length; ++i) {
canvas.drawText(lines[i], width / 2, (height / 2 - ((textPaint.descent() + textPaint.ascent()) / 2)) + yoff, textPaint);
textPaint.getTextBounds(lines[i], 0, lines[i].length(), r);
yoff += r.height();
}
canvas.restoreToCount(count);
关于java - 将多行文本值设置为 imageview 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38238108/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!