Android: How to use the onDraw method in a class that extends Activity?

As a newbie, I created a simple counter application using a simple xml layout and a class called "Counter" that outputs (extends) from the Activity class.

Now I want to upload a bitmap (png file) to place next to the counter. I read onDraw (), but the View extension is required for this class. I tried to create an object of this class to use it instead, but to no avail. I am a little puzzled by this concept of how to make it easy. If anyone could explain, I would appreciate it.

+5
source share
4 answers

A simple example using the onDraw function - this requires the class to extend the 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,x, radius, paint); } } } 
+3
source

I think that you cannot use onDraw in the class, expanding the action due to the use of Canvas, you need to create a Custom component that extends some kind, and there you perform the actions, and what you need to do is =>

1. Extend an existing class or subclass of a class with its own class.

2. Return some of the methods from the superclass. Superclass methods for overriding begin with "on", for example, onDraw (), onMeasure (), onKeyDown (). This is similar to the β€œevents” in an Activity that you redefine for the life cycle and other functional hooks.

3. Use the new extension class. After completing your new class, extensions can be used instead of the view on which it was based.

0
source

Could you just place the ImageView next to your counter? You can do this 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 his speech:

 @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, 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 all questions, you can refer to Android Training

0
source

For this purpose you do not need to use View.onDraw() . Just use layouts and place any drawn image next to your counter, say a text image, using the drawableLeft attribute.

txtview_counter.drawableLeft="@drawable/xxx"

0
source

All Articles