moxide moxide - 10 days ago 4
Android Question

How do I make a Canvas in Android?

I don't understand how to make a canvas in android. I've looked at a lot of tutorials, and I think I followed them correctly, but I clearly didn't as it still doesn't work. Here is the code I have for my main activity:

package com.example.dan.shootyguy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void neato(View view){
Intent intent = new Intent(MainActivity.this, DrawThing.class);
}
}


I think this part works, but it isn't changing how the app looks so I can't be sure.

Then, I have another class where I have the canvas and onDraw:

package com.example.dan.shootyguy;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class DrawThing extends View{

Paint paint = new Paint(Color.RED);

public DrawThing(Context context) {
super(context);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawOval(25f,25f,25f,25f,paint);
}

}


This should be making a red circle, but it doesn't do anything. Can anyone offer any help? Thanks.

Answer

View(DrawThing ) con't call by intent Try this...

public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
         DrawThing draw=new DrawThing(this);
            setContentView(draw);

        }


    }