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

前端 未结 4 1939
离开以前
离开以前 2021-01-11 17:31

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.

4条回答
  •  别那么骄傲
    2021-01-11 17:52

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

提交回复
热议问题