How Do Solve Shape round rect too large to be rend

2019-06-28 03:11发布

问题:

I create A shape for Text View Background

<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" />

and my textview is :

 <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/rel1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        android:background="@drawable/rounded_corners"
        android:gravity="right"
        android:lineSpacingExtra="6dp"
        android:supportsRtl="true"
        android:text="@string/hello_world"
        android:textColor="#FFFFFF" />

when text is short like this

but when text is too large not showing background and eclipse logcat show

Shape round rect too large to be rendered into a texture (424x5884, max=2048x2048)

how to solve it? thank you

回答1:

My Solution is to draw onto the canvas. See below.

If you need to do gradents etc then look at Shader's, https://developer.android.com/reference/android/graphics/LinearGradient.html Should do what you need it too.

/**
 * 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);
}
}


回答2:

Edit: The easiest solution is to get rid of the rounded corners. If you remove the rounded corners and use a simple rectangle, the hardware renderer will no longer create a single large texture for the background layer, and won't run into the texture size limit any more.


One simple workaround should be to revert to software rendering for that view:

View view = findViewById(R.id.textView1);
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

...but we've run into a similar problem here and we got the same result you did, the view (and its children) didn't render.

You can also set the layer type of a view from XML:

<TextView android:layerType="software" />

Setting the layerType to "none" instead of software seems to cause the view to draw, but it drew without the rounded corners in a quick test we just tried.

Another approach might be to use a different method of rendering the rounded rectangle, e.g.

  • clipping and drawing the path yourself in onDraw
  • using a PaintDrawable (which supports rounded corners, but must be set from code)
  • breaking the rectangle into three slices -- a top (with rounded corners), middle (just a solid color), and bottom (with rounded corners)


回答3:

You could also try to make your background .9.png