Android模块化编程——炫酷小巧多功能Button2
2016-07-08 10:17
316 查看
抱歉,之前这网络错误,发了两篇相同的文章,这次补上。
上文《炫酷小巧多功能button》今天附上另一种实现方法,是使用xml实现,布局方式不同。效果类似。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/876e0ae1fcc3c02dd1778f9ceb6ddc65)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/353c0f1ea0021943cd97c6b0a2fa2011)
直接上代码:PathMenuMainActivity
动画文件,这次不用nineoldandroids-2.4.0.jar了。
布局文件写了所有按钮,主按钮控制显隐。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/a9dd15f2b4388895b3da2cd5160ae5a4)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/3932c819b43f9fbdf8ea50728f04c381)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/7a4bcb8980cf429bef7406041641484c)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/aa4cfc8247e6244381420a4471cc9fb4)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/50990aa04fedb4ac1cf4ac51fada6182)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/f9650fb2bd5a47067c6936afddcc90d9)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/a01d40944692643c69c29f05404dcb2b)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/6fd6e54ad506a80a66e0f882325eb48b)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/9876f7e132297fe4ea0e98fbd9388b45)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/29c59920293b5d5534cea92dba349d1f)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/12/40a82968bb24f457eb498e0c2f958126)
上文《炫酷小巧多功能button》今天附上另一种实现方法,是使用xml实现,布局方式不同。效果类似。
直接上代码:PathMenuMainActivity
package com.example.button; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.RelativeLayout; public class PathMenuMainActivity extends Activity { /** Called when the activity is first created. */ private boolean areButtonsShowing; private RelativeLayout composerButtonsWrapper; private ImageView composerButtonsShowHideButtonIcon; private RelativeLayout composerButtonsShowHideButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_imitate_path2_main); MyAnimations.initOffset(PathMenuMainActivity.this); composerButtonsWrapper = (RelativeLayout) findViewById(R.id.composer_buttons_wrapper); composerButtonsShowHideButton = (RelativeLayout) findViewById(R.id.composer_buttons_show_hide_button); composerButtonsShowHideButtonIcon = (ImageView) findViewById(R.id.composer_buttons_show_hide_button_icon); composerButtonsShowHideButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!areButtonsShowing) { MyAnimations.startAnimationsIn(composerButtonsWrapper, 300); composerButtonsShowHideButtonIcon .startAnimation(MyAnimations.getRotateAnimation(0, -270, 300)); } else { MyAnimations .startAnimationsOut(composerButtonsWrapper, 300); composerButtonsShowHideButtonIcon .startAnimation(MyAnimations.getRotateAnimation( -270, 0, 300)); } areButtonsShowing = !areButtonsShowing; } }); for (int i = 0; i < composerButtonsWrapper.getChildCount(); i++) { composerButtonsWrapper.getChildAt(i).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { } }); } composerButtonsShowHideButton.startAnimation(MyAnimations .getRotateAnimation(0, 360, 200)); } }
动画文件,这次不用nineoldandroids-2.4.0.jar了。
package com.example.button; import android.content.Context; import android.view.ViewGroup; import android.view.ViewGroup.MarginLayoutParams; import android.view.animation.Animation; import android.view.animation.AnticipateInterpolator; import android.view.animation.OvershootInterpolator; import android.view.animation.RotateAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageButton; public class MyAnimations { private static int xOffset = 15; private static int yOffset = -13; public static void initOffset(Context context) {// 鐢卞竷灞?枃浠? xOffset = (int) (10.667 * context.getResources().getDisplayMetrics().density); yOffset = -(int) (8.667 * context.getResources().getDisplayMetrics().density); } public static Animation getRotateAnimation(float fromDegrees, float toDegrees, int durationMillis) { RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotate.setDuration(durationMillis); rotate.setFillAfter(true); return rotate; } public static void startAnimationsIn(ViewGroup viewgroup, int durationMillis) { for (int i = 0; i < viewgroup.getChildCount(); i++) { ImageButton inoutimagebutton = (ImageButton) viewgroup .getChildAt(i); inoutimagebutton.setVisibility(0); MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton .getLayoutParams(); Animation animation = new TranslateAnimation(mlp.rightMargin - xOffset, 0F, yOffset + mlp.bottomMargin, 0F); animation.setFillAfter(true); animation.setDuration(durationMillis); animation.setStartOffset((i * 100) / (-1 + viewgroup.getChildCount())); animation.setInterpolator(new OvershootInterpolator(2F)); inoutimagebutton.startAnimation(animation); } } public static void startAnimationsOut(ViewGroup viewgroup, int durationMillis) { for (int i = 0; i < viewgroup.getChildCount(); i++) { final ImageButton inoutimagebutton = (ImageButton) viewgroup .getChildAt(i); MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton .getLayoutParams(); Animation animation = new TranslateAnimation(0F, mlp.rightMargin - xOffset, 0F, yOffset + mlp.bottomMargin); animation.setFillAfter(true); animation.setDuration(durationMillis); animation.setStartOffset(((viewgroup.getChildCount() - i) * 100) / (-1 + viewgroup.getChildCount())); animation.setInterpolator(new AnticipateInterpolator(2F)); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation arg0) { } @Override public void onAnimationRepeat(Animation arg0) { } @Override public void onAnimationEnd(Animation arg0) { // TODO Auto-generated method stub inoutimagebutton.setVisibility(8); } }); inoutimagebutton.startAnimation(animation); } } }
布局文件写了所有按钮,主按钮控制显隐。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/path2_default_homebg"> <RelativeLayout android:id="@+id/composer_buttons_wrapper" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:clipChildren="false" android:clipToPadding="false" > <ImageButton android:id="@+id/composer_button_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="142dp" android:layout_marginRight="10.667dp" android:background="@drawable/path2_composer_camera" android:visibility="gone" /> <ImageButton android:id="@+id/composer_button_people" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="135.333dp" android:layout_marginRight="52dp" android:background="@drawable/path2_composer_with" android:visibility="gone" /> <ImageButton android:id="@+id/composer_button_place" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="116.666dp" android:layout_marginRight="89.33333333333333dp" android:background="@drawable/path2_composer_place" android:visibility="gone" /> <ImageButton android:id="@+id/composer_button_music" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="87.33333333333333dp" android:layout_marginRight="118.6666666666667dp" android:background="@drawable/path2_composer_music" android:visibility="gone" /> <ImageButton android:id="@+id/composer_button_thought" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="50dp" android:layout_marginRight="137.3333333333333dp" android:background="@drawable/path2_composer_thought" android:visibility="gone" /> <ImageButton android:id="@+id/composer_button_sleep" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="8.666666666666667dp" android:layout_marginRight="144dp" android:background="@drawable/path2_composer_sleep" android:visibility="gone" /> </RelativeLayout> <RelativeLayout android:id="@+id/composer_buttons_show_hide_button" android:layout_width="60dp" android:layout_height="57.33333333333333dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@drawable/path2_composer_button" > <ImageView android:id="@+id/composer_buttons_show_hide_button_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/path2_composer_icn_plus" /> </RelativeLayout> </RelativeLayout>
相关文章推荐
- 带Header与Footer的RecyclerViews
- Android fragment 重叠问题的解决方法
- Android Studio利用异步任务AsyncTask发送post请求获取json数据
- Android 2016新技术
- Android获取周围WIFI热点服务
- Activity跳转交换数据
- @+android:id 和@+id的区别
- Android报unsupported major.minor version 52.0错误
- Android绘图板的开发
- Android中Web Service学习(二)——Android调用Web Service实例
- android常用URI
- AndroidStudio 导入第三方库 Manifest 覆盖
- MVC模式在Android中的应用
- 关于安卓3.0以上的系统屏幕适配问题的一些心得
- (OK) init_in_android-x86_64.sh
- Android 图片选择器
- 个人开发者做一款Android App需要知道的事情
- AndroidStudio下配置ArcGIs
- Android自定义控件
- Android 手机sdcard目录或文件的拷贝、移动、删除(递归)