如何解决形状圆形矩形太大而无法在android TextBox中渲染为纹理的问题

如何解决形状圆形矩形太大而无法在android TextBox中渲染为纹理的问题,android,background,textbox,Android,Background,Textbox,我为文本视图背景创建了一个形状 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#800e1520" android:endColor="#801e252f" android:angle="45"/> <padding android:left="

我为文本视图背景创建了一个形状

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#800e1520"
    android:endColor="#801e252f"
    android:angle="45"/>
<padding android:left="7dp"
    android:top="7dp"
    android:right="7dp"
    android:bottom="7dp" />
<corners android:radius="8dp" />
如何解决?
谢谢

编辑:最简单的解决方案是去掉圆角。如果删除圆角并使用简单的矩形,硬件渲染器将不再为背景层创建单个大纹理,并且不再遇到纹理大小限制


一个简单的解决方法是恢复该视图的软件渲染:

View view = findViewById(R.id.textView1);
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
…但是我们在这里遇到了类似的问题,我们得到了与您相同的结果,视图(及其子视图)没有渲染

还可以设置视图的图层类型:


将layerType设置为“none”而不是软件似乎会导致视图绘制,但在我们刚刚尝试的快速测试中,它绘制时没有圆角

另一种方法可能是使用不同的方法渲染圆角矩形,例如

  • onDraw
  • 使用(支持圆角,但必须从代码中设置)
  • 将矩形分成三个部分——顶部(带圆角)、中部(仅为纯色)和底部(带圆角)

你也可以尝试制作背景

我的解决方案是在画布上画画。见下文

如果您需要进行评分等,请查看
着色器
, 你也应该做你需要的事

/**
 * Created by chris on 04/11/2013
 */
public class WidgetLinearLayout extends LinearLayout {

//Dither and smooth :)
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
private final RectF mBound = new RectF();
private final float radius;

public WidgetLinearLayout(Context context) {
    this(context, null);
}

public WidgetLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    this(context, attrs);
}

public WidgetLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundDrawable(null);
    mPaint.setColor(getResources().getColor(R.color.white));
    mPaint.setStyle(Paint.Style.FILL);
    radius = getResources().getDimension(R.dimen.widget_corner_radius);
    setWillNotDraw(false);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    mBound.set(l, t, r, b);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRoundRect(mBound, radius, radius, mPaint);
}
}

感谢您的回复,但不起作用,请清除所有内容,minapi级别为11,但您可以用一个示例解释如何使用ondraw。对不起,我的英语不好。该调用的最低API级别为11,但在此之前不存在有问题的硬件加速,因此在旧版本上不需要它。如果您也针对较旧的版本,那么XML方法将工作得更好。然而,这种解决方法似乎并不能真正解决问题。我建议使用“切片”方法:用三个视图构建背景,中间部分可以是一个普通的矩形,为了避免纹理限制。@MostafaRostami,我更新了我的答案,指出您也可以通过删除圆角来解决问题。是的,这是我的最后一个选项。我删除圆角并使用一个简单的矩形。感谢您的回复,我接受您的答案,感谢Scustom onDraw是更好的方法,在画布上绘制时,系统可以处理如何执行此操作efficiently@MostafaRostami使用.9.png有什么问题。当我有一个高达屏幕5倍的文本视图时,当我使用.9.png时,我遇到了同样的问题。
View view = findViewById(R.id.textView1);
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
<TextView android:layerType="software" />
/**
 * Created by chris on 04/11/2013
 */
public class WidgetLinearLayout extends LinearLayout {

//Dither and smooth :)
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
private final RectF mBound = new RectF();
private final float radius;

public WidgetLinearLayout(Context context) {
    this(context, null);
}

public WidgetLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    this(context, attrs);
}

public WidgetLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    setBackgroundDrawable(null);
    mPaint.setColor(getResources().getColor(R.color.white));
    mPaint.setStyle(Paint.Style.FILL);
    radius = getResources().getDimension(R.dimen.widget_corner_radius);
    setWillNotDraw(false);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    mBound.set(l, t, r, b);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawRoundRect(mBound, radius, radius, mPaint);
}
}