Estoy tratando de trazar una línea en un View
personalizado. Aquí he creado un simple Path
con solo un segmento, creé un PathShape
a partir de eso, y finalmente lo metí en un ShapeDrawable
con la intención de usarlo para dibujar en el Canvas
dentro de . Sin embargo, esto no funciona. Vea mi ejemplo, aquí.¿Cómo se supone que debes usar ShapeDrawable con PathShape para dibujar una línea en una vista personalizada?
package com.example.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.util.Log;
import android.view.View;
public class TestView extends View {
private Path mPath = null;
private Paint mPaint = null;
private PathShape mPathShape = null;
private ShapeDrawable mShapeDrawable = null;
public TestView(Context context) {
super(context);
}
private void init() {
int width = this.getWidth()/2;
int height = this.getHeight()/2;
Log.d("init", String.format("width: %d; height: %d", width, height));
this.mPath = new Path();
this.mPath.moveTo(0, 0);
this.mPath.lineTo(width, height);
this.mPaint = new Paint();
this.mPaint.setColor(Color.RED);
this.mPathShape = new PathShape(this.mPath, 1, 1);
this.mShapeDrawable = new ShapeDrawable(this.mPathShape);
this.mShapeDrawable.getPaint().set(this.mPaint);
this.mShapeDrawable.setBounds(0, 0, width, height);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
// Doing this here because in the constructor we don't have the width and height of the view, yet
this.init();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("onDraw", "Drawing");
// This works, but won't let me do what I'm really trying to do
canvas.drawLine(0.0f, 0.0f, this.getWidth()/2.0f, this.getHeight()/2.0f, this.mPaint);
// This should work, but does not
//this.mPathShape.draw(canvas, this.mPaint);
// This should work, but does not
//this.mShapeDrawable.draw(canvas);
}
}
Como se puede ver en mis comentarios en el método onDraw()
, ni eligiendo PathShape
ni el ShapeDrawable
para dibujar el Path
en el Canvas
realmente funciona. Nada se dibuja cuando lo intento. ¿Alguien tiene alguna idea de por qué?
El dispositivo en el que estoy probando esto ejecuta Android 4.1.1.
Simplemente curioso: ha intentado crear un nuevo objeto Paint dentro de onDraw. Estoy haciendo eso y teniendo éxito con bitmaps, pero no estaba seguro de si era necesario o no. – JavaCoderEx
No lo hice, ya que no quería crear objetos en onDraw (en realidad causa una advertencia de Lint). Sin embargo, lo intenté justo ahora y obtuve los mismos resultados. Sin éxito. – Daniel
Después de investigar un poco más, y la falta de respuesta aquí, creo que esto simplemente se ha roto y he enviado un informe de error. http://code.google.com/p/android/issues/detail?id=35229 – Daniel