Android: How to use the onDraw method in a class extending Activity?
Asked Answered
N

4

10

As a beginner, I've been building a simple counter application using a simple layout xml and a class called 'Counter', which derives (extends) from the class Activity.

Now, I want to load a bitmap (png file) to place next to the counter. I've been reading up on onDraw(), but it requires the class to extend 'View'. I've been trying to create an object of this class to use this instead, to no avail. I'm a bit stumped about this concept, how to do it easily. If anyone could explain, I'd appreciate it.

Nineteenth answered 19/1, 2014 at 19:39 Comment(0)
D
9

Simple example using onDraw function-it requires class to extend view

Context to get the current activity context

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(new myview(this));


}

class myview extends View
{

    public myview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        int x=80;
        int y=80;
        int radius=40;
        Paint paint=new Paint();
        // Use Color.parseColor to define HTML colors
        paint.setColor(Color.parseColor("#CD5C5C"));
        canvas.drawCircle(x, y, radius, paint);
    }

}

}
Debug answered 26/6, 2014 at 15:2 Comment(2)
Great, succinct answer. Should be marked as answer. Thanks for showing View code and also the setContentView() call in the Activity. That helped me get what I needed.Biomass
This is only working example for android canvas . I still need redraw Where i can put this willNotDraw(false);Linnlinnaeus
S
0

I think that you cant use onDraw in class extending an Activity because of using Canvas, you need to create a Custom component extending some View and there handle you action,and what do you need to do that=>

1.Extend an existing View class or subclass with your own class.

2.Override some of the methods from the superclass. The superclass methods to override start with ‘on’, for example,onDraw(), onMeasure(), onKeyDown(). This is similar to the‘on’ events in Activity that you override for lifecycle and other functionality hooks.

3.Use your new extension class. Once completed, your new extension class can be used in place of the view upon which it was based.

Semen answered 19/1, 2014 at 19:55 Comment(0)
H
0

Couldn't you just place an ImageView next to your counter? You could do that in your xml layout file:

<ImageView
    android:id="@id+/my_image"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:src="@drawable/name_of_my_image" />

Or in your acrivity:

@Override
protected void onCreate(Bundle bundle) {
    // ...
    ImageView image = (ImageView) findViewById(R.id.my_image);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
    image.setImageBitmap(bitmap);
}

If you want to use a custom view and override the onDraw() method here's what you need to do:

class MyCustomView extends View {
    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // your custom drawing
        canvas.drawRect(0, 0, 50, 50, new Paint());
    }
}

For any doubt, you can refer to Android Training

Huddleston answered 19/1, 2014 at 20:49 Comment(0)
R
0

You don't need to use View.onDraw() for this purpose. Just use layouts and place any drawable image next to your counter, let's say a textview, using the drawableLeft attribute.

txtview_counter.drawableLeft="@drawable/xxx"

Ramires answered 3/8, 2016 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.