Android Simple Development Tutorial 13: Option Menu Brush Sample

Source: Internet
Author: User
Tags linecap resource xmlns

The guide bee two-dimensional drawing brush (Pen) sample contains four examples, Lines, dashes, LineJoin, and LineCap. You intend to use Option menu (Main menu) to select different examples.

First, make some changes to Guidebeegraphics2dview and graphics2dactivity, starting with this example, Guidebeegraphics2dview need to dynamically draw different graphics (either through menus or thread). Add the following two methods to the Guidebeegraphics2dview:

Final Runnable Updatecanvas = new Runnable () {
public void Run () {
Invalidate ();
}
};
public void Refreshcanvas () {
Post (Updatecanvas);
}

Post can be used in the call UI thread method in non-UI thread. This only triggers the screen redraw event to refresh the screen display.

In graphics2dactivity add a variable protected guidebeegraphics2dview graphic2dview; To obtain the corresponding Guidebeegraphics2dview instance.

graphic2dview= (Guidebeegraphics2dview) Findviewbyid (R.id.graphics2dview);

Using option menu, although you can also use code to create a menu, a more general approach is to use the menus resource. Create the menu subdirectory under Res, and then under Res/menu, create the four sample options that Menu_option_line.xml uses to display the pen:

<?xml version= "1.0″encoding=" utf-8″?>
<menu
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item
Android:id= "@+id/mnulines"
Android:title= "Lines"
android:icon= "@drawable/icon1″>
</item>
<item
Android:id= "@+id/mnudashes"
android:title= "dashes"
android:icon= "@drawable/icon2″>
</item>
<item
Android:id= "@+id/mnulinecap"
Android:title= "LineCap"
android:icon= "@drawable/icon3″>
</item>
<item
Android:id= "@+id/mnulinejoin"
Android:title= "LineJoin"
android:icon= "@drawable/icon4″>
</item>
</menu>

<menu></menu> Define menu,<item></item> define menu items,<item></item> can be nested to define submenus. Menus can define attributes such as IDs, Icon,text, and so on. can also support radio, multiple selection, at this time need to rely on <GROUP></GROUP>, use Group to define a menu item as a group, you can use Setgroupvisible () to show hidden entire menu group, enable or disable the entire menu group setgroupenabled () and so on. Most critical, there is a group to achieve the menu of radio and multiple selection function:

<?xml version= "1.0″encoding=" utf-8″?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@+id/item1″
android:icon= "@drawable/item1″
android:title= "@string/item1″/>
<!–menu group–>
<group android:id= "@+id/group1″>
<item android:id= "@+id/groupitem1″
android:title= "@string/groupitem1″/>
<item android:id= "@+id/groupitem2″
android:title= "@string/groupitem2″/>
</group>
</menu>

Once you have defined the menu resources, you can use Menuinflater.inflate () to expand the menu, which generally requires the oncreateoptionsmenu () of the activity to expand the menu:

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (r.menu.menu_option_line, menu);
return true;
}

Finally, respond to menu events:

@Override
public boolean onoptionsitemselected (MenuItem item) {
Menuoption = Item.getitemid ();
DrawImage ();
return true;
}

Item.getitemid (); Returns the ID of the menu (defined in the menu resource).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.