Thursday, June 12, 2014

[Java Android] Create Circle and Line using Canvas on Eclipse with ADT (just for self documentation)

here it is,

I just create new Android app and go straigth to MainActivity.java without setting up the interface, so res/value/strings.xml  and res/layout/* is left as is

and my activity is mainly in MainActivity :)



package com.nugnux.gambar;
 
import android.app.Activity;
//import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
//import android.os.Build;
import android.widget.Toast;
 
public class MainActivity extends Activity {
     
    private String tulisan="";
    private final int interval = 3000; // 1 Second
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable(){
        public void run() {
            Toast.makeText(MainActivity.this, "Tadaa...", Toast.LENGTH_SHORT).show();
        }
    }; 
    DrawView drawView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tulisan = Long.toString(System.currentTimeMillis());
        Toast.makeText(MainActivity.this, "Heheh..."+tulisan, Toast.LENGTH_SHORT).show();
         
        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
         
        //handler.postAtTime(runnable, System.currentTimeMillis()+interval);
        handler.postDelayed(runnable, interval);
        //handler.postDelayed(r, delayMillis)
         
    }
    class DrawView extends View {
        Paint paint = new Paint();
 
        public DrawView(Context context) {
            super(context);
            paint.setColor(Color.BLUE);
        }
        @Override
        public void onDraw(Canvas canvas) {
             super.onDraw(canvas);
                canvas.drawLine(10, 20, 30, 40, paint);
                canvas.drawLine(20, 10, 50, 20, paint);
                canvas.drawCircle(100, 100, 10, paint);
        }
}
     
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
 
        public PlaceholderFragment() {
        }
 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }
}
 
...still figuring how to create moveTo lineTo since it'll come handy for drawing curve (sine, cosine, some exotic function) and animating it :)