public class MainActivity extends Activity implements PathView.OnItemClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment); setupView(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } /** * 初始化菜单 */ private void setupView() { PathView mPathView = (PathView) this.findViewById(R.id.mPathView_yixin); ImageButton startMenu = new ImageButton(this); startMenu.setBackgroundResource(R.drawable.start_menu_btn); mPathView.setStartMenu(startMenu); int[] drawableIds = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher }; View[] items = new View[drawableIds.length]; for (int i = 0; i < drawableIds.length; i++) { ImageButton button = new ImageButton(this); button.setBackgroundResource(drawableIds[i]); items[i] = button; } mPathView.setItems(items); mPathView.setOnItemClickListener(this); } @Override public void onItemClick(View view, int position) { // TODO Auto-generated method stub switch (position) { case 1: break; case 2: break; case 3: break; case 4: break; default: break; } }}
/** 边角竖向path **/public class PathView extends RelativeLayout { // private boolean isExpand = false; // private View[] items; private View startMenu; private View bgView; public int DURATION = 300; public int MARGIN_RIGHT = 16; public int MARGIN_TOP = 16; public int MARGIN_BOTTOM = 16; public PathView(Context context) { super(context); initContentView(); } public PathView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initContentView(); } public PathView(Context context, AttributeSet attrs) { super(context, attrs); initContentView(); } public void initContentView() { bgView = new View(getContext()); //bgView.setBackgroundColor(Color.argb(100, 0, 0, 0)); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); addView(bgView, params); bgView.setVisibility(View.GONE); bgView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ cols(DURATION); bgView.setVisibility(View.GONE); return true; } return false; } }); } public void setStartMenu(View view) { LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.rightMargin = MARGIN_RIGHT; params.bottomMargin = MARGIN_BOTTOM; params.topMargin = MARGIN_TOP; addView(view, params); this.startMenu = view; startMenu.setId(66666); startMenu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (isExpand) { cols(DURATION); } else { expand(DURATION); } } }); } public void expand(int duration) { bgView.setVisibility(View.VISIBLE); for (int i = 0; i < items.length; i++) { final View view = items[i]; view.setEnabled(false); float dy = startMenu.getY() - view.getY(); TranslateAnimation translateAnim = new TranslateAnimation( -startMenu.getWidth() / 2, 0, dy, 0); translateAnim.setDuration(duration); ScaleAnimation scaleAnim = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnim.setDuration(duration); AnimationSet set = new AnimationSet(true); set.addAnimation(scaleAnim); set.addAnimation(translateAnim); set.setInterpolator(getContext(), android.R.anim.linear_interpolator); set.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { view.setVisibility(View.VISIBLE); } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { isExpand = true; view.setEnabled(true); } }); roate(0, -135, DURATION); view.startAnimation(set); } } public void cols(int duration) { for (int i = 0; i < items.length; i++) { final View view = items[i]; view.setEnabled(false); float dy = startMenu.getY() - view.getY(); TranslateAnimation translateAnim = new TranslateAnimation(0, -startMenu.getWidth() / 2, 0, dy); translateAnim.setDuration(duration); ScaleAnimation scaleAnim = new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnim.setDuration(duration); AnimationSet set = new AnimationSet(true); set.addAnimation(scaleAnim); set.addAnimation(translateAnim); set.setInterpolator(getContext(), android.R.anim.linear_interpolator); set.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { view.setEnabled(true); view.setVisibility(View.GONE); isExpand = false; } }); bgView.setVisibility(View.GONE); roate(-135, 0, DURATION); view.startAnimation(set); } } private void roate(int fromDegree, int toDegree, int duration) { RotateAnimation anim = new RotateAnimation(fromDegree, toDegree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setFillAfter(true); anim.setDuration(duration); anim.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { startMenu.setEnabled(false); } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { startMenu.setEnabled(true); } }); startMenu.startAnimation(anim); } public void setItems(View[] items) { for (int i = 0; i < items.length; i++) { View view = items[i]; view.setId(i + 1); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); if (i == 0) { params.addRule(RelativeLayout.ABOVE, startMenu.getId()); } else { params.addRule(RelativeLayout.ABOVE, items[i - 1].getId()); } // params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.ALIGN_RIGHT, startMenu.getId()); // params.rightMargin = MARGIN_RIGHT; addView(view, params); view.setVisibility(View.INVISIBLE); } this.items = items; } public void setOnItemClickListener(final OnItemClickListener listener) { for (int i = 0; i < items.length; i++) { final View view = items[i]; final int position = i; view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(v.getVisibility()==View.VISIBLE){ cols(DURATION); fadeOut(view, DURATION); listener.onItemClick(view, position); } } }); } } private void fadeOut(final View view, int duration) { AlphaAnimation alphaAnim = new AlphaAnimation(1, 0.0f); alphaAnim.setDuration(duration); ScaleAnimation scaleAnim = new ScaleAnimation(1f, 1.5f, 1f, 1.5f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnim.setDuration(duration); AnimationSet set = new AnimationSet(true); set.addAnimation(scaleAnim); set.addAnimation(alphaAnim); set.setInterpolator(getContext(), android.R.anim.accelerate_interpolator); set.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { view.setVisibility(View.GONE); } }); view.startAnimation(set); } public interface OnItemClickListener { void onItemClick(View view, int position); }}
接下里 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.example.pathview.PathView android:id="@+id/mPathView_yixin" android:layout_width="wrap_content" android:layout_height="wrap_content" > </com.example.pathview.PathView></RelativeLayout>
在 github 上又1个开源的库 可以实现 pathview 的效果
所有的效果。
代码地址: http://download.csdn.net/detail/kongbaidepao/7060469