超炫按钮 Path按钮的实现+源码

这个鄙人在CSDN上面发表的第一篇文章,很多不足,望大家体谅。

首先将PathButtons做为一个工具类,该类代码稍长,但方便其他程序使用Path按钮时直接继承就可以了。

/**
 * 该类作用:
 *@auther TangBowen
 *@Tel:
 *@E-mail:289665044@qq.com
 *@data 2013-5-23上午12:05:43
 */
package com.Berwin.pathbuttons.utils;

import com.Berwin.pathbuttons.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

/**
 * 该类作用:将PathButtons生成一个工具类,方便其他程序中使用PathButton时直接继承就可用
 * 
 * @author TangBowen Tel:18200387036 E-mail:289665044@qq.com 2013-5-23上午12:05:43
 */
public class PathButtons extends Activity implements OnClickListener {
	// 红色按钮
	private ImageView ivComposer;
	private RotateAnimation clockwiseAm;
	private RotateAnimation anticlockwiseAm;

	// 摄像头
	private ImageView ivCamera;
	private TranslateAnimation cameraOutTA;
	private TranslateAnimation cameraInTA;

	// 个人信息
	private ImageView ivWith;
	private TranslateAnimation withOutTA;
	private TranslateAnimation withInTA;

	// 地方
	private ImageView ivPlace;
	private TranslateAnimation placeOutTA;
	private TranslateAnimation placeInTA;

	// 音乐
	private ImageView ivMusic;
	private TranslateAnimation musicOutTA;
	private TranslateAnimation musicInTA;

	// 聊天
	private ImageView ivThought;
	private TranslateAnimation thoughtOutTA;
	private TranslateAnimation thoughtInTA;

	// 睡眠
	private ImageView ivHome;
	private TranslateAnimation homeOutTA;
	private TranslateAnimation homeInTA;

	public static boolean isClockwise = true;
	AnimationSet animationSet = new AnimationSet(true);
	
	private RelativeLayout camera_Layout = null;
	private RelativeLayout music_Layout= null;
	private RelativeLayout place_Layout= null;
	private RelativeLayout home_Layout= null;
	private RelativeLayout thought_Layout= null;
	private RelativeLayout with_Layout= null;
	private ViewFlipper mViewFlipper = null;
	private Context mContext = null;
	private int mLastPosition = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		findView();
	}

	/**
	 * 通过findViewById找到对应的组件 并设置一些移动或者旋转的属性
	 */
	private void findView() {
		ivComposer = (ImageView) this.findViewById(R.id.ivComposer);
		ivCamera = (ImageView) this.findViewById(R.id.ivCamera);
		ivWith = (ImageView) this.findViewById(R.id.ivWith);
		ivPlace = (ImageView) this.findViewById(R.id.ivPlace);
		ivMusic = (ImageView) this.findViewById(R.id.ivMusic);
		ivThought = (ImageView) this.findViewById(R.id.ivThought);
		ivHome = (ImageView) this.findViewById(R.id.ivHome);

		camera_Layout = (RelativeLayout) this.findViewById(R.id.camera_page);
		music_Layout = (RelativeLayout) this.findViewById(R.id.music_page);
		place_Layout = (RelativeLayout) this.findViewById(R.id.place_page);
		home_Layout = (RelativeLayout) this.findViewById(R.id.home_page);
		thought_Layout = (RelativeLayout) this.findViewById(R.id.thought_page);
		with_Layout = (RelativeLayout) this.findViewById(R.id.with_page);

		mViewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper1);

		camera_Layout.setVisibility(View.GONE);
		music_Layout.setVisibility(View.GONE);
		place_Layout.setVisibility(View.GONE);
		home_Layout.setVisibility(View.GONE);
		thought_Layout.setVisibility(View.GONE);
		with_Layout.setVisibility(View.GONE);

		ivComposer.setOnClickListener(this);
		ivCamera.setOnClickListener(this);
		ivWith.setOnClickListener(this);
		ivPlace.setOnClickListener(this);
		ivMusic.setOnClickListener(this);
		ivThought.setOnClickListener(this);
		ivHome.setOnClickListener(this);

		clockwiseAm = new RotateAnimation(0, +360, Animation.RELATIVE_TO_SELF,
				0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		clockwiseAm.setDuration(300);
		// 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等
		// LinearInterpolator平稳不变的
		// DecelerateInterpolator 在中间加速,两头慢
		// CycleInterpolator,曲线运动特效,要传递float型的参数
		// LinearInterpolator lin = new LinearInterpolator();
		DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator();
		clockwiseAm.setInterpolator(decelerateInterpolator);
		clockwiseAm.setAnimationListener(clockwiseAmListener);

		anticlockwiseAm = new RotateAnimation(0, -360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		anticlockwiseAm.setDuration(300);
		anticlockwiseAm.setInterpolator(decelerateInterpolator);
		anticlockwiseAm.setAnimationListener(anticlockwiseAmListener);
		// http://www.cnblogs.com/sysbug/archive/2012/01/18/2325530.html
		cameraOutTA = new TranslateAnimation(Animation.ABSOLUTE, -5.0f,
				Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 240.0f,
				Animation.ABSOLUTE, 10.0f);
		withOutTA = new TranslateAnimation(Animation.ABSOLUTE, -75f,
				Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 225.0f,
				Animation.ABSOLUTE, 10.0f);
		placeOutTA = new TranslateAnimation(Animation.ABSOLUTE, -135f,
				Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE, 190f,
				Animation.ABSOLUTE, 10.0f);
		musicOutTA = new TranslateAnimation(Animation.ABSOLUTE, -175f,
				Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE, 130f,
				Animation.ABSOLUTE, 10.0f);
		thoughtOutTA = new TranslateAnimation(Animation.ABSOLUTE, -205f,
				Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE, 70f,
				Animation.ABSOLUTE, 10.0f);
		homeOutTA = new TranslateAnimation(Animation.ABSOLUTE, -215f,
				Animation.ABSOLUTE, -10.0f, Animation.ABSOLUTE, 5f,
				Animation.ABSOLUTE, 10.0f);
		// 设置持续时间
		cameraOutTA.setDuration(200);
		withOutTA.setDuration(200);
		placeOutTA.setDuration(200);
		musicOutTA.setDuration(200);
		thoughtOutTA.setDuration(200);
		homeOutTA.setDuration(200);

		cameraInTA = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
				Animation.ABSOLUTE, -5.0f, Animation.ABSOLUTE, 10.0f,
				Animation.ABSOLUTE, 240.0f);
		withInTA = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
				Animation.ABSOLUTE, -75f, Animation.ABSOLUTE, 10.0f,
				Animation.ABSOLUTE, 225.0f);
		placeInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,
				Animation.ABSOLUTE, -135f, Animation.ABSOLUTE, 10.0f,
				Animation.ABSOLUTE, 190f);
		musicInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,
				Animation.ABSOLUTE, -175f, Animation.ABSOLUTE, 10.0f,
				Animation.ABSOLUTE, 130f);
		thoughtInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,
				Animation.ABSOLUTE, -205f, Animation.ABSOLUTE, 10.0f,
				Animation.ABSOLUTE, 70f);
		homeInTA = new TranslateAnimation(Animation.ABSOLUTE, -10.0f,
				Animation.ABSOLUTE, -215f, Animation.ABSOLUTE, 10.0f,
				Animation.ABSOLUTE, 5f);

		cameraInTA.setDuration(200);
		withInTA.setDuration(200);
		placeInTA.setDuration(200);
		musicInTA.setDuration(200);
		thoughtInTA.setDuration(200);
		homeInTA.setDuration(200);

		ScaleAnimation scaleAnimation = new ScaleAnimation(1, 10f, 1, 10f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		scaleAnimation.setDuration(150);
		animationSet.addAnimation(scaleAnimation);
		// animationSet.setFillAfter(true); //让其保持动画结束时的状态。

	}

	private void buttomOutAction() {
		ivComposer.startAnimation(clockwiseAm);

		ivCamera.startAnimation(cameraOutTA);
		ivCamera.setVisibility(View.VISIBLE);

		// 设置持续时间
		withOutTA.setStartOffset(20);
		ivWith.startAnimation(withOutTA);
		ivWith.setVisibility(View.VISIBLE);

		placeOutTA.setStartOffset(40);
		ivPlace.startAnimation(placeOutTA);
		ivPlace.setVisibility(View.VISIBLE);

		musicOutTA.setStartOffset(60);
		ivMusic.startAnimation(musicOutTA);
		ivMusic.setVisibility(View.VISIBLE);

		thoughtOutTA.setStartOffset(80);
		ivThought.startAnimation(thoughtOutTA);
		ivThought.setVisibility(View.VISIBLE);

		homeOutTA.setStartOffset(100);
		ivHome.startAnimation(homeOutTA);
		ivHome.setVisibility(View.VISIBLE);
	}

	private void buttomInAction() {
		ivComposer.startAnimation(anticlockwiseAm);

		ivHome.startAnimation(homeInTA);
		ivHome.setVisibility(View.GONE);

		thoughtInTA.setStartOffset(20);
		ivThought.startAnimation(thoughtInTA);
		ivThought.setVisibility(View.GONE);

		musicInTA.setStartOffset(40);
		ivMusic.startAnimation(musicInTA);
		ivMusic.setVisibility(View.GONE);

		placeInTA.setStartOffset(60);
		ivPlace.startAnimation(placeInTA);
		ivPlace.setVisibility(View.GONE);

		withInTA.setStartOffset(80);
		ivWith.startAnimation(withInTA);
		ivWith.setVisibility(View.GONE);

		cameraInTA.setStartOffset(100);
		ivCamera.startAnimation(cameraInTA);
		ivCamera.setVisibility(View.GONE);
	}

	private void AllButtonGone() {
		ivComposer.startAnimation(anticlockwiseAm);
		ivCamera.setVisibility(View.GONE);
		ivWith.setVisibility(View.GONE);
		ivPlace.setVisibility(View.GONE);
		ivMusic.setVisibility(View.GONE);
		ivThought.setVisibility(View.GONE);
		ivHome.setVisibility(View.GONE);

	}

	private void ActivityAction(int number){
		mViewFlipper.setInAnimation(mContext, R.anim.push_up_in);
		mViewFlipper.getChildAt(mLastPosition).setVisibility(View.VISIBLE);
		mViewFlipper.setOutAnimation(mContext, R.anim.push_up_out);
		mViewFlipper.setDisplayedChild(number);
		
	}
	/**
	 * 点击Button的事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ivComposer:
			if (isClockwise) {
				buttomOutAction();
			} else {
				buttomInAction();
			}
			break;
		case R.id.ivCamera:
			ivCamera.startAnimation(animationSet);
			camera_Layout.setVisibility(View.VISIBLE);

			ActivityAction(0);
			mLastPosition = 0;
			AllButtonGone();
			break;
		case R.id.ivWith:
			ivWith.startAnimation(animationSet);
			with_Layout.setVisibility(View.VISIBLE);
			
			ActivityAction(1);
			mLastPosition = 1;
			AllButtonGone();
			break;
		case R.id.ivPlace:
			ivPlace.startAnimation(animationSet);
			place_Layout.setVisibility(View.VISIBLE);

			ActivityAction(2);
			mLastPosition = 2;
			AllButtonGone();
			break;
		case R.id.ivMusic:
			ivMusic.startAnimation(animationSet);
			music_Layout.setVisibility(View.VISIBLE);

			ActivityAction(3);
			mLastPosition = 3;
			AllButtonGone();
			break;
		case R.id.ivThought:
			ivThought.startAnimation(animationSet);
			thought_Layout.setVisibility(View.VISIBLE);

			ActivityAction(4);
			mLastPosition = 4;
			AllButtonGone();
			break;
		case R.id.ivHome:
			System.out.println("ivSleep");
			ivHome.startAnimation(animationSet);
			home_Layout.setVisibility(View.GONE);
			
			ActivityAction(5);
			mLastPosition = 5;
			AllButtonGone();
			break;
		}
		isClockwise = isClockwise ? false : true;
	}

	/**
	 * 当点击红色Button弹出功能按键时“+”的旋转特效
	 */
	private AnimationListener clockwiseAmListener = new AnimationListener() {
		public void onAnimationEnd(Animation arg0) {
			Matrix matrix = new Matrix();
			matrix.setRotate(45);
			Bitmap source = BitmapFactory.decodeResource(
					PathButtons.this.getResources(),
					R.drawable.composer_icn_plus);
			Bitmap resizedBitmap = Bitmap.createBitmap(source, 0, 0,
					source.getWidth(), source.getHeight(), matrix, true);
			ivComposer.setImageBitmap(resizedBitmap);
			if (source != null && !source.isRecycled())
				source.recycle();
		}

		public void onAnimationRepeat(Animation animation) {
		}

		public void onAnimationStart(Animation animation) {
		}
	};
	/**
	 * 当点击红色Button收回功能按键时“+”的旋转特效
	 */
	private AnimationListener anticlockwiseAmListener = new AnimationListener() {
		public void onAnimationEnd(Animation arg0) {
			Bitmap source = BitmapFactory.decodeResource(
					PathButtons.this.getResources(),
					R.drawable.composer_icn_plus);
			ivComposer.setImageBitmap(source);
		}

		public void onAnimationRepeat(Animation animation) {
		}

		public void onAnimationStart(Animation animation) {
		}
	};
}


在实现这个应用的MainActivity类,该类直接继承PathButtons即可。

package com.Berwin.pathbuttons;

import com.Berwin.pathbuttons.utils.PathButtons;
import android.os.Bundle;
import android.view.View;
/**
 * 
 * @author TangBowen
 *
 */
public class PathButtonsMainActivity extends PathButtons{
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.main_activity);
		super.onCreate(savedInstanceState);
	}
	@Override
	public void onClick(View v) {
		super.onClick(v);
	}
}

上面是Path按钮的实现的主要类,当然还有其他的xml文件,用于控制点击按钮的画面切换,和动画效果,在此不写出。源码地址点击打开链接点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值