Android app Draw Shapes

My first Android App Draw Shapes, which is actually my first assignment of android programming class, is now on Google Play. The code is shared at GitHub.

ActionBar

I want to show the user what they are drawing on the action bar. So I put a DrawingShapeView in action_bar layout, and set it up in my MainActivity class.

MainActivity.setupActionBar()link
1
2
3
4
5
6
7
8
View actionBarView = LayoutInflater.from(this).inflate(R.layout.action_bar, null);
((DrawingShapeView) actionBarView.findViewById(R.id.drawingShape)).setup(model, painter);
//...
ActionBar actionBar = getActionBar();
actionBar.setCustomView(actionBarView);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);

In setup() method of DrawingShapeView class I listen to the change of color/shape from model class and redraw it.

DrawingShapeView.setup()link
1
2
3
4
5
6
7
8
9
10
11
12
model.setOnColorChangeListener(new DrawModel.OnColorChangeListener() {
@Override
public void onColorChanged(int color) {
invalidate();
}
});
model.setOnShapeChangeListener(new DrawModel.OnShapeChangeListener() {
@Override
public void onShapeChanged(Shape shape) {
invalidate();
}
});

DrawerLayout

To use DrawerLayout you need to add Support Library to build.gradle of the app (not the top level one).

app/build.gradlelink
1
2
3
4
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:support-v4:20.0.0"
}

And make the root to be DrawerLayout.

activity_main.xmllink
1
2
3
4
5
6
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v4.widget.DrawerLayout>

The first child is the main view, others will be the drawers. I put 2 drawers, one on left and one on right.

activity_main.xmllink
1
2
3
4
5
6
7
8
9
10
11
12
// Main view
<com.evancoding.drawshapes.view.DrawView />
// Left drawer
<LinearLayout
android:id="@+id/toolsLayout"
android:layout_gravity="start">

</LinearLayout>
// Right drawer
<LinearLayout
android:id="@+id/buttonsLayout"
android:layout_gravity="end">

</LinearLayout>

You can then open/close the drawers in the activity class.

MainActivity.setupDrawer()link
1
2
3
4
5
private void setupDrawer() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
toolsLayout = findViewById(R.id.toolsLayout);
buttonsLayout = findViewById(R.id.buttonsLayout);
}
MainActivity.toggleDrawer()link
1
2
3
4
5
6
7
private void toggleDrawer(View drawer) {
if (drawerLayout.isDrawerOpen(drawer)) {
drawerLayout.closeDrawer(drawer);
} else {
drawerLayout.openDrawer(drawer);
}
}

StarDrawer

It took me a lot of time to figure out how to draw a Star. You can find it and all the other code on GitHub.