Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Draw on a Canvas in Android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 160
    Comment on it

    How to make Draw on a Canvas in Android. If you want to create Draw on a Canvas function check below example. In the below example I have created a Suraface class. Canvas works like as an interface. I have also used onDraw() method for drawing event.

    Step(1)-MainActivity -

    public class MainActivity extends Activity {

    1. @Override
    2. protected void onCreate(Bundle savedInstanceState) {
    3. super.onCreate(savedInstanceState);
    4. Suraface v = new Suraface(this);
    5. setContentView(v);
    6.  
    7. }
    8.  
    9. @Override
    10. public boolean onCreateOptionsMenu(Menu menu) {
    11. getMenuInflater().inflate(R.menu.main, menu);
    12. return true;
    13. }
    14. }

    Step(2)Suraface class-

    1. public class Suraface extends SurfaceView implements
    2. SurfaceHolder.Callback {
    3. Bitmap mBitmap;
    4.  
    5. public SurafaceClass(Context context) {
    6. super(context);
    7. mBitmap = BitmapFactory.decodeResource(getResources(),
    8. R.drawable.ic_launcher);
    9. }
    10.  
    11. @Override
    12. public void surfaceChanged(SurfaceHolder holder, int format, int width,
    13. int height) {
    14.  
    15. }
    16.  
    17. @Override
    18. public void surfaceCreated(SurfaceHolder holder) {
    19.  
    20. }
    21.  
    22. @Override
    23. public void surfaceDestroyed(SurfaceHolder holder) {
    24.  
    25. }
    26.  
    27. @Override
    28. protected void onDraw(Canvas canvas) {
    29. canvas.drawColor(Color.BLACK);
    30. }
    31.  
    32. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: