Android Path菜单的简单实现,android开发软件
作者:互联网
-
mHomeparams.width = LayoutParams.WRAP_CONTENT;
-
mHomeparams.height = LayoutParams.WRAP_CONTENT;
-
switch (position) {
-
case LEFT_TOP:
-
mHomeparams.gravity = Gravity.LEFT | Gravity.TOP;
-
for (int i = 0; i < menuResIds.length; i++) {
-
int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);
-
int height_padding = mHEIGHT / ((menuResIds.length - 1) * 2);
-
ImageView imageView = new ImageView(mContext);
-
imageView.setImageResource(menuResIds[i]);
-
addView(imageView);
-
LayoutParams params = (FrameLayout.LayoutParams) imageView
-
.getLayoutParams();
-
params.width = LayoutParams.WRAP_CONTENT;
-
params.height = LayoutParams.WRAP_CONTENT;
-
params.leftMargin = mWIDTH / 2
-
- ((menuResIds.length - i - 1) * width_padding);
-
params.topMargin = mHEIGHT / 2 - i * height_padding;
-
params.gravity = Gravity.LEFT | Gravity.TOP;
-
imageView.setLayoutParams(params);
-
}
-
break;
-
case RIGHT_TOP:
-
mHomeparams.gravity = Gravity.RIGHT | Gravity.TOP;
-
for (int i = 0; i < menuResIds.length; i++) {
-
int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);
-
int height_padding = mHEIGHT / ((menuResIds.length - 1) * 2);
-
ImageView imageView = new ImageView(mContext);
-
imageView.setImageResource(menuResIds[i]);
-
addView(imageView);
-
LayoutParams params = (FrameLayout.LayoutParams) imageView
-
.getLayoutParams();
-
params.width = LayoutParams.WRAP_CONTENT;
-
params.height = LayoutParams.WRAP_CONTENT;
-
params.rightMargin = mWIDTH / 2
-
- ((menuResIds.length - i - 1) * width_padding);
-
params.topMargin = mHEIGHT / 2 - i * height_padding;
-
params.gravity = Gravity.RIGHT | Gravity.TOP;
-
imageView.setLayoutParams(params);
-
}
-
break;
-
case RIGHT_BOTTOM:
-
mHomeparams.gravity = Gravity.RIGHT | Gravity.BOTTOM;
-
for (int i = 0; i < menuResIds.length; i++) {
-
int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);
-
int height_padding = mHEIGHT / ((menuResIds.length - 1) * 2);
-
ImageView imageView = new ImageView(mContext);
-
imageView.setImageResource(menuResIds[i]);
-
addView(imageView);
-
LayoutParams params = (FrameLayout.LayoutParams) imageView
-
.getLayoutParams();
-
params.width = LayoutParams.WRAP_CONTENT;
-
params.height = LayoutParams.WRAP_CONTENT;
-
params.rightMargin = mWIDTH / 2
-
- ((menuResIds.length - i - 1) * width_padding);
-
params.bottomMargin = mHEIGHT / 2 - i * height_padding;
-
params.gravity = Gravity.RIGHT | Gravity.BOTTOM;
-
imageView.setLayoutParams(params);
-
}
-
break;
-
case LEFT_BOTTOM:
-
mHomeparams.gravity = Gravity.LEFT | Gravity.BOTTOM;
-
for(int i = 0; i < menuResIds.length; i++){
-
int width_padding = mWIDTH / ((menuResIds.length - 1) * 2);
-
int height_padding = mHEIGHT / ((menuResIds.length -1) * 2);
-
ImageView imageView = new ImageView(mContext);
-
imageView.setImageResource(menuResIds[i]);
-
addView(imageView);
-
LayoutParams params = (FrameLayout.LayoutParams)imageView.getLayoutParams();
-
params.width = LayoutParams.WRAP_CONTENT;
-
params.height = LayoutParams.WRAP_CONTENT;
-
params.leftMargin = mWIDTH / 2 - ((menuResIds.length - i - 1) * width_padding);
-
params.bottomMargin = mHEIGHT / 2 - i * height_padding;
-
params.gravity = Gravity.LEFT | Gravity.BOTTOM;
-
imageView.setLayoutParams(params);
-
}
-
break;
-
default:
-
break;
-
}
-
mHome.setLayoutParams(mHomeparams);
-
}
-
private OnClickListener listener = new OnClickListener() {
-
public void onClick(View v) {
-
if (!bMenuShow) {
-
startAnimationIn(PathMenuView.this, 300);
-
} else {
-
startAnimationOut(PathMenuView.this, 300);
-
}
-
bMenuShow = !bMenuShow;
-
}
-
};
-
/**
-
* 菜单隐藏动画.
-
*
-
* @param group
-
* @param duration
-
*/
-
private void startAnimationIn(ViewGroup group, int duration) {
-
for (int i = 1; i < group.getChildCount(); i++) {
-
ImageView imageview = (ImageView) group.getChildAt(i);
-
imageview.setVisibility(0);
-
MarginLayoutParams mlp = (MarginLayoutParams) imageview
-
.getLayoutParams();
-
Animation animation = null;
-
switch (position) {
-
case LEFT_TOP:
-
animation = new TranslateAnimation(0F,-mlp.leftMargin+xOffset,0F,-mlp.topMargin + yOffset);
-
break;
-
case RIGHT_TOP:
-
animation = new TranslateAnimation(mlp.rightMargin - xOffset,0F,-mlp.topMargin + yOffset,0F);
-
break;
-
case LEFT_BOTTOM:
-
animation = new TranslateAnimation(0F, -mlp.leftMargin+ xOffset, 0F, -yOffset + mlp.bottomMargin);
-
break;
-
case RIGHT_BOTTOM:
-
animation = new TranslateAnimation(mlp.rightMargin-xOffset,0F,-yOffset + mlp.bottomMargin, 0F);
-
break;
-
default:
-
break;
-
}
-
animation.setFillAfter(true);
-
animation.setDuration(duration);
-
animation.setStartOffset((i * 100) / (-1 + group.getChildCount()));
-
animation.setInterpolator(new OvershootInterpolator(2F));
-
imageview.startAnimation(animation);
-
}
-
}
-
/**
-
* 菜单显示动画.
-
*
-
* @param group
-
* @param duration
-
*/
-
private void startAnimationOut(ViewGroup group,int duration){
-
for (int i = 1; i < group.getChildCount(); i++) {
-
final ImageView imageview = (ImageView) group
-
.getChildAt(i);
-
MarginLayoutParams mlp = (MarginLayoutParams) imageview.getLayoutParams();
-
Animation animation = null;
-
switch (position) {
-
case LEFT_TOP:
-
animation = new TranslateAnimation(-mlp.leftMargin+xOffset,0F,-mlp.topMargin + yOffset,0F);
-
break;
-
case RIGHT_TOP:
-
animation = new TranslateAnimation(0F,mlp.rightMargin - xOffset,0F,-mlp.topMargin + yOffset);
-
break;
-
case LEFT_BOTTOM:
-
animation = new TranslateAnimation(-mlp.leftMargin+xOffset,0F, -yOffset + mlp.bottomMargin,0F);
-
break;
-
case RIGHT_BOTTOM:
-
animation = new TranslateAnimation(0F,mlp.rightMargin-xOffset, 0F,-yOffset + mlp.bottomMargin);
-
break;
-
default:
-
break;
-
}
-
animation.setFillAfter(true);animation.setDuration(duration);
-
animation.setStartOffset(((group.getChildCount()-i) * 100)
-
/ (-1 + group.getChildCount()));
-
animation.setInterpolator(new AnticipateInterpolator(2F));
-
imageview.startAnimation(animation);
-
}
-
}
-
}
第四步:PathTestActivity.java以及用到的布局文件main.xml代码如下:
PathTestActivity.java(基本没修改代码)代码如下:
[java] view plain copy
-
package com.tutor.path;
-
import android.app.Activity;
-
import android.os.Bundle;
-
public class PathTestActivity extends Activity {
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
}
-
}
main.xml代码如下:
[java] view plain copy
- <?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
xmlns:tutor=“http://schemas.android.com/apk/res/com.tutor.path”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
android:orientation=“vertical” >
-
<com.tutor.path.PathMenuView
-
android:id="@+id/text"
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
tutor:position=“right_bottom”
-
/>
最后
下面是辛苦给大家整理的学习路线
-
android:layout_height=“fill_parent”
-
android:orientation=“vertical” >
-
<com.tutor.path.PathMenuView
-
android:id="@+id/text"
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
tutor:position=“right_bottom”
-
/>
最后
下面是辛苦给大家整理的学习路线
[外链图片转存中…(img-cse5ggaX-1643518121306)]
标签:开发软件,LayoutParams,height,mlp,animation,params,imageView,Path,Android 来源: https://blog.csdn.net/m0_66144910/article/details/122751957