Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawable is not drawing on canvas

I have a custom view and a drawable to be drawn on it, but for some reason the drawable is not drawing itself on the view's canvas. Here's how the drawable is created:

int[] gradientColors=new int[] { 0xFFFF0000,0xFFFFFF00,0xFF00FF00,
        0xFF00FFFF,0xFF0000FF,0xFFFF00FF,0xFFFF0000 };
gradient=new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
        gradientColors);

And here's the onDraw function:

@Override protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    int width=canvas.getWidth();
    int height=canvas.getHeight();
    gradientBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
    canvas.setBitmap(gradientBitmap);
    gradient.setBounds(0,0,width,height);
    gradient.draw(canvas);
}
like image 365
user940016 Avatar asked Jun 02 '12 18:06

user940016


People also ask

What is the preferred image format for the Drawable?

Supported file types are PNG (preferred), JPG (acceptable), and GIF (discouraged).

How to add image in Drawable XML?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure.

How to define a Drawable?

drawable (comparative more drawable, superlative most drawable) Capable of being drawn (represented graphically). Capable of being drawn (pulled or extracted). Capable of being drawn (deduced or inferred).

How to add image Drawable android?

Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import. Figure 3: Select Import Drawables from the dropdown menu.


1 Answers

Try to look at this code it can be helpful for you to learn haw to handle drawing for custom views:

public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new SampleView(this));
        }

           private static class SampleView extends View {
                private Rect    mRect;
                private GradientDrawable mDrawable;

                public SampleView(Context context) {
                    super(context);
                    setFocusable(true);

                    mRect = new Rect(0, 0, 220, 120);

    /*              GradientDrawable.Orientation  BL_TR  draw the gradient from the bottom-left to the top-right   
                      BOTTOM_TOP  draw the gradient from the bottom to the top   
                      BR_TL  draw the gradient from the bottom-right to the top-left   
                      LEFT_RIGHT  draw the gradient from the left to the right   
                      RIGHT_LEFT  draw the gradient from the right to the left   
                      TL_BR  draw the gradient from the top-left to the bottom-right   
                      TOP_BOTTOM  draw the gradient from the top to the bottom   
                      TR_BL  draw the gradient from the top-right to the bottom-left   
    */

                    mDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                                                     new int[] { 0xFFFF0000, 0xFF00FF00,
                                                         0xFF0000FF });
                    mDrawable.setShape(GradientDrawable.RECTANGLE);
                    mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60));
                }

                static void setCornerRadius(GradientDrawable drawable, float r0,
                                           float r1, float r2, float r3) {
    /*              setCornerRadii
                    Specify radii for each of the 4 corners. For each corner, 
                    the array contains 2 values, [X_radius, Y_radius]. 
                    The corners are ordered top-left, top-right, bottom-right, 
                    bottom-left 
    */
                    drawable.setCornerRadii(new float[] { r0, r0, r1, r1,
                                                          r2, r2, r3, r3 });
                }

                @Override protected void onDraw(Canvas canvas) {

                    mDrawable.setBounds(mRect);

                    float r = 35;

                    canvas.save();
                    canvas.translate(10, 10);
                    mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
                    setCornerRadius(mDrawable, r, r, 0, 0);
                    mDrawable.draw(canvas);
                    canvas.restore();

                    canvas.translate(0, mRect.height() + 10);
                    canvas.save();
                    canvas.translate(10, 10);
                    mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
                    setCornerRadius(mDrawable, 0, 0, r, r);
                    mDrawable.draw(canvas);
                    canvas.restore();

                    canvas.translate(0, mRect.height() + 10);
                    canvas.save();
                    canvas.translate(10, 10);
                    mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);
                    setCornerRadius(mDrawable, 0, r, r, 0);
                    mDrawable.draw(canvas);
                    canvas.restore();


                }
            }
        }

UPDATE

public class MyView extends View {
    private static int measuredWidth = 300;
    private static int measuredHeight = 300;
    private Rect    mRect;
    private GradientDrawable mDrawable;

    public MyView(Context context) {
        super(context);
        initializeView();


    }

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initializeView();
    }

    private void initializeView() {

        mDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] { 0xFFFF0000,0xFFFFFF00,0xFF00FF00,
                0xFF00FFFF,0xFF0000FF,0xFFFF00FF,0xFFFF0000 });
        mDrawable.setShape(GradientDrawable.RECTANGLE);
        mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60));
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeView();
    }

    @Override protected void onDraw(Canvas canvas) {
        mRect = new Rect(0, 0, measuredWidth, measuredHeight);
        mDrawable.setBounds(mRect);
        canvas.save();
        canvas.translate(10, 10);
        mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
        //setCornerRadius(mDrawable, r, r, 0, 0);
        mDrawable.draw(canvas);
        canvas.restore();

    }

I tried this for you it's based on your code and works except that i fixed the height and width:

like image 107
K_Anas Avatar answered Oct 21 '22 12:10

K_Anas