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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Suraface v = new Suraface(this);
setContentView(v);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step(2)Suraface class-
public class Suraface extends SurfaceView implements
SurfaceHolder.Callback {
Bitmap mBitmap;
public SurafaceClass(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
}
}
0 Comment(s)