Android Menu半透明效果的开发实例
程序员文章站
2024-03-06 14:25:26
不知道大家是否用过天天动听,对于它界面上的半透明menu效果,笔者感觉非常漂亮。下面是天天动听半透明menu的截图,欣赏下吧:
&...
不知道大家是否用过天天动听,对于它界面上的半透明menu效果,笔者感觉非常漂亮。下面是天天动听半透明menu的截图,欣赏下吧:
感觉还不错吧?那么如何实现这种半透明menu效果呢?本文就重点讨论并给出这种menu的具体代码实现过程。
首先分析下实现这种半透明menu所需做的工作,并进行合理分解:
1. 利用shaper设置一个半透明圆角背景。
2. 定义menu布局,主要就gridview,把图标都放在这个gridview。
3. menu事件, 通过popupwindow或者alertdialog或者透明activity显示到页面即可。
4. 按钮的监听事件,实例中没加。需要的话自己在adapter里加。
比较简单,不多说了。
半透明圆角背景xml:
xml/html代码
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle"> <solid android:color="#b4000000" /> <stroke android:width="2.0dip" android:color="#b4ffffff" android:dashwidth="3.0dip" android:dashgap="0.0dip" /> <padding android:left="7.0dip" android:top="7.0dip" android:right="7.0dip" android:bottom="7.0dip" /> <corners android:radius="8.0dip" /> </shape>
menu布局:
xml/html代码
<?xml version="1.0" encoding="utf-8"?> <linearlayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="fill_parent"> <gridview android:gravity="center" android:layout_gravity="center" android:id="@+id/menugridchange" android:background="@drawable/menu_bg_frame" android:padding="5.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:horizontalspacing="10.0dip" android:verticalspacing="3.0dip" android:stretchmode="columnwidth" android:columnwidth="60.0dip" android:numcolumns="auto_fit"/> </linearlayout>
主要类:
java代码
package com.yfz; import android.app.activity; import android.app.alertdialog; import android.app.alertdialog.builder; import android.content.context; import android.graphics.drawable.bitmapdrawable; import android.os.bundle; import android.util.log; import android.view.contextmenu; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.view.contextmenu.contextmenuinfo; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; import android.widget.linearlayout; import android.widget.popupwindow; import android.widget.textview; import android.widget.linearlayout.layoutparams; public class menutest extends activity { private string tag = this.getclass().getsimplename(); private int[] resarray = new int[] { r.drawable.icon_menu_addto, r.drawable.icon_menu_audioinfo, r.drawable.icon_menu_findlrc, r.drawable.icon_menu_scan }; private string[] title = new string[]{ "添加歌曲", "歌曲信息", "查找歌词", "搜索歌词" }; private static boolean show_flag = false; private popupwindow pw = null; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } @override public boolean oncreateoptionsmenu(menu menu) { log.e(tag, "------ oncreateoptionsmenu ------"); //用alertdialog弹出menu // view view = layoutinflater.from(this).inflate(r.layout.menu, null); // gridview grid1 = (gridview)view.findviewbyid(r.id.menugridchange); // grid1.setadapter(new imageadapter(this)); // builder build = new alertdialog.builder(this); // build.setview(view); // build.show(); layoutinflater inflater = (layoutinflater)this.getsystemservice(context.layout_inflater_service); view view = inflater.inflate(r.layout.menu, null); gridview grid1 = (gridview)view.findviewbyid(r.id.menugridchange); grid1.setadapter(new imageadapter(this)); //用popupwindow弹出menu pw = new popupwindow(view,layoutparams.fill_parent, layoutparams.wrap_content); //nnd, 第一个参数, 必须找个view pw.showatlocation(findviewbyid(r.id.tv), gravity.center, 0, 300); return true; } @override public boolean onoptionsitemselected(menuitem item) { return super.onoptionsitemselected(item); } public class imageadapter extends baseadapter { private context context; public imageadapter(context context) { this.context = context; } @override public int getcount() { return resarray.length; } @override public object getitem(int arg0) { return resarray[arg0]; } @override public long getitemid(int arg0) { return arg0; } @override public view getview(int arg0, view arg1, viewgroup arg2) { linearlayout linear = new linearlayout(context); linearlayout.layoutparams params = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); linear.setorientation(linearlayout.vertical); imageview iv = new imageview(context); iv.setimagebitmap(((bitmapdrawable)context.getresources().getdrawable(resarray[arg0])).getbitmap()); linearlayout.layoutparams params2 = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); params2.gravity=gravity.center; linear.addview(iv, params2); textview tv = new textview(context); tv.settext(title[arg0]); linearlayout.layoutparams params3 = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); params3.gravity=gravity.center; linear.addview(tv, params3); return linear; } } }
到此,大家是不是觉得半透明menu效果也是比较好实现的呢?可以根据自己的需要对此实例进行修改以求更美观好用。
以上就是对android menu 半透明效果的实现,后续继续补充相关资料谢谢大家对本站的支持!
推荐阅读