Estaba tratando de obtener gráficos 2D en Android. Como ejemplo quiero implementar un dibujable costumbre, el documento presentado en mi actividadImplementación de un drawable personalizado en Android
he definido un dibujable personalizado mediante la extensión de dibujable Android como se menciona más adelante
class myDrawable extends Drawable {
private static final String TAG = myDrawable.class.getSimpleName();
private ColorFilter cf;
@Override
public void draw(Canvas canvas) {
//First you define a colour for the outline of your rectangle
Paint rectanglePaint = new Paint();
rectanglePaint.setARGB(255, 255, 0, 0);
rectanglePaint.setStrokeWidth(2);
rectanglePaint.setStyle(Style.FILL);
//Then create yourself a Rectangle
RectF rectangle = new RectF(15.0f, 50.0f, 55.0f, 75.0f); //in pixels
Log.d(TAG,"On Draw method");
// TODO Auto-generated method stub
Paint paintHandl = new Paint();
// paintHandl.setColor(0xaabbcc);
paintHandl.setARGB(125, 234, 213, 34);
RectF rectObj = new RectF(5,5,25,25);
canvas.drawRoundRect(rectangle, 0.5f, 0.5f, rectanglePaint);
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 100;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
this.cf = cf;
}
}
Estoy tratando de obtener esta muestra en mi actividad, como se muestra a continuación
public class custDrawable extends Activity {
/** Called when the activity is first created. */
LinearLayout layObj = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layObj = (LinearLayout) findViewById(R.id.parentLay);
ImageView imageView = (ImageView) findViewById(R.id.icon2);
myDrawable myDrawObj = new myDrawable();
imageView.setImageDrawable(myDrawObj);
imageView.invalidate();
// layObj.addView(myDrawObj, params);
}
}
Pero cuando corro la aplicación me veo ninguna rectángulo en la actividad, puede alguien ayudarme? ¿Dónde me estoy equivocando?
que hice los cambios mencionado por Casey, pero aún no puedo ver ningún material válido dibujado en mi opinión. – Girish
El otro problema es que parece que nunca se agrega 'ImageView' a su' LinearLayout'. Necesita agregar 'layObj.addView (imageView);' al final de 'onCreate()' – CaseyB
He agregado el imageView al diseño lineal, básicamente es un problema con establecer los límites En xml, he codificado el ancho y la altura de la vista de la imagen, ahora está apareciendo, ¿cierto problema con mis setBounds? – Girish