14

I was trying to get hold of 2D graphics in Android. As a example i want to implement a custom drawable and show it in my Activity

I have defined a customized drawable by extending from Android drawable as mentioned below

 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; } } 

I am trying to get this displayed in my activity, as shown below

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

But when i run the app i see no rectangle on the activity, can anyone help me out? Where am i going wrong?

2 Answers 2

13

Your problem is in the getOpacity() method. 100 is not a valid value. You should use a PixelFormat value. Also, you should create your RectF and Paint in the constructor and then just adjust the values in draw() so you don't create so many objects that need garbage collected. Like this:

public class Square extends Drawable { private final Paint mPaint; private final RectF mRect; public Square() { mPaint = new Paint(); mRect = new RectF(); } @Override public void draw(Canvas canvas) { // Set the correct values in the Paint mPaint.setARGB(255, 255, 0, 0); mPaint.setStrokeWidth(2); mPaint.setStyle(Style.FILL); // Adjust the rect mRect.left = 15.0f; mRect.top = 50.0f; mRect.right = 55.0f; mRect.bottom = 75.0f; // Draw it canvas.drawRoundRect(mRect, 0.5f, 0.5f, mPaint); } @Override public int getOpacity() { return PixelFormat.OPAQUE; } @Override public void setAlpha(int arg0) { } @Override public void setColorFilter(ColorFilter arg0) { } } 
Sign up to request clarification or add additional context in comments.

6 Comments

I did the changes mentioned by casey, but still i am not able to see any valid stuff drawn on my view.
Nother problem is that it seems that you never add the ImageView to your LinearLayout. You need to add layObj.addView(imageView); to the end of onCreate()
I have added the imageView to linear layout, it is basically a problem with set bounds In xml , i hardcoded the width and height of the image view, now it is showing up, so some problem with my setBounds is it?
Is it possible to embed an image with in the rounded rectangle or any primitive shape i draw, say polygon or a path? If so can you please share the code?
If you override the draw() method you can draw whatever you want to the Canvas.
|
0

You may have to implement other overrides like getIntrinsicWidth and getIntrinsicHeight. One way to tell is that you set your layout_width and layout_height to some constant (layout_width="42dip" layout_height="42dip" in XML or setting your layoutParams to some value if you are using Java layouts). Some View types handle not having getIntrinsic* implemented than others, so try them! This includes a straight View.

You can try returning -1 if there's no specific width or height.

Hard to tell if the issue ever got resolved, but I got here via Google trying to help myself remember the details of making a custom Drawable, plus I want to help people avoid this: http://xkcd.com/979/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.