仿Iphone右划关闭Activity

使用很简单,只需继承SupportActivity,并在清单中加入android:theme="@android:style/Theme.Translucent"即可  


package com.support;


import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

 /**
  * 基类,所有Activity必须继承该Activity,并在清单中声明,</br>
  * 例   <activity  android:name="com.support.test.TwoActivity"  android:theme="@android:style/Theme.Translucent"/>
  * 
  * </br></br>
  * 不要在application节点和默认启动Activity下使用 android:theme="@android:style/Theme.Translucent"
  * @author Young
  *
  */
 

public abstract class SupportActivity extends  Activity {
	private boolean isTouchable=true;//是否可以右划返回,默认启动 的主Activity要设置为false
	private FrameLayout rootView;
	 private static final String MAIN="android.intent.action.MAIN";
	private int width ;//屏幕宽度
	private final int time=200;//动画时长
	public void setContentView(View root) {
		addContainer(root);
		  
	}

	public void setContentView(int layoutRes) {
		addContainer(LayoutInflater.from(getApplicationContext()).inflate(layoutRes, null));
		 
	}
	 
	
	private void addContainer(View root) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		Intent intent=getIntent();
		//把用户要显示的界面加入到可右划的布局中
		ContainerView frameLayout=new ContainerView(getApplicationContext());
		frameLayout.setBackgroundColor(Color.parseColor("#88000000")); 
		frameLayout.addView(root);
		rootView=frameLayout;
		
		if (intent==null||MAIN.equals(intent.getAction())) {
			frameLayout.canTouch(false);
			isTouchable=false;
		}
		super.setContentView(rootView);
	}
	
	@Override
	public void startActivity(Intent intent) {
		// TODO Auto-generated method stub
		super.startActivity(intent);
		overridePendingTransition(0, 0);//取消用户设置的或系统默认的Activity切换动画
	}
	 
 
	 
 
	@Override
	public void finish() {
		if (isTouchable==false) {
			super.finish();
			overridePendingTransition(0, 0);//取消用户设置的或系统默认的Activity切换动画
			return;
		}
		// TODO Auto-generated method stub
		ObjectAnimator animator= ObjectAnimator.ofFloat(rootView.getChildAt(0), "translationX", 0,width);
		animator.setDuration(time*2);
		animator.addListener(new AnimatorListenerAdapter(){
			@Override
			public void onAnimationEnd(Animator animation) {
				// TODO Auto-generated method stub
				super.onAnimationEnd(animation);
				endToFinish();
				 
			}
		});
		animator.addUpdateListener(new AnimatorUpdateListener() {
			
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				// TODO Auto-generated method stub
				Float cost=(Float) animation.getAnimatedValue();
				 
				float alp=(float) (1-cost/time/2 );
				rootView.setAlpha(alp);
				rootView.getChildAt(0).setAlpha(alp);
			}
		});
		animator.start();
		overridePendingTransition(0, 0);
	}

	private void endToFinish(){
		super.finish();
	}
	@Override//取消用户设置的或系统默认的Activity切换动画
	public void overridePendingTransition(int enterAnim, int exitAnim ) {
		super.overridePendingTransition(0, 0);
	}
	  
	
	
	/**
	 * 可右划的容器
	 * @author Young
	 *
	 */
	private final class ContainerView extends FrameLayout {

		/**
		 * 按下时x坐标
		 */
		private float downX;
		private float downY;
		private Context context;
		/**
		 * 左侧8dp为右划感应区域
		 */
		private final int scaledTouchSlop=8;
		
		public ContainerView(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
			this.context=context;
			getScreenWidth();
		}

		public ContainerView(Context context, AttributeSet attrs) {
			super(context, attrs);
			this.context=context;
			getScreenWidth();
		}

		public ContainerView(Context context) {
			super(context);
			this.context=context;
			getScreenWidth();
		}
		
		private void getScreenWidth() {
			// TODO Auto-generated method stub
			WindowManager wm = (WindowManager)context
		            .getSystemService(Context.WINDOW_SERVICE);
			width = wm.getDefaultDisplay().getWidth();
		}

		@Override
		public boolean onInterceptTouchEvent(MotionEvent ev) {

			 
			if (ev.getAction()==MotionEvent.ACTION_DOWN) {
				downX=ev.getRawX();
				downY=ev.getRawX();
				return super.onInterceptTouchEvent(ev);//交给子控件处理事件
			}
			if (ev.getAction()==MotionEvent.ACTION_MOVE) {
				float x=ev.getRawX();
				float y=ev.getRawX();
				//ViewConfiguration.get(context).getScaledTouchSlop()   手机可以识别是最小滑动距离
				if (x-downX>Math.abs(y-downY)&&Math.abs(x-downX)>=ViewConfiguration.get(context).getScaledTouchSlop()&&downX<dip2px(scaledTouchSlop ) ) {
					return true;//交给自己的onTouchEvent(MotionEvent event)处理事件
					
				}
			}
			return super.onInterceptTouchEvent(ev);
		}
		
	    @Override
		public boolean onTouchEvent(MotionEvent event) {
			// TODO Auto-generated method stub
			if (isTouchable==false) {
				return false;
			}
			switch (event.getAction()) {
				case MotionEvent.ACTION_MOVE:
					if (downX<dip2px(scaledTouchSlop)&&getChildCount()==1) {
						
						getChildAt(0).setTranslationX(event.getRawX()-downX);
						float alp=(float) ((event.getRawX()-downX)/width );
						setAlpha(1-alp);
//						getChildAt(0).setAlpha(1-alp/5);
					}
					break;
		
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
					if (downX<dip2px(scaledTouchSlop)&&getChildCount()==1) {
						
						float t=getChildAt(0).getTranslationX();
						
						if (t>width/2) {// 
							
							ObjectAnimator animator= ObjectAnimator.ofFloat(getChildAt(0), "translationX", t,width);
							animator.setDuration(time);
							
							 ObjectAnimator.ofFloat(this, "translationX", t,width).setDuration(time).start();
							animator.addListener(new AnimatorListenerAdapter(){
								@Override
								public void onAnimationEnd(Animator animation) {
									// TODO Auto-generated method stub
									super.onAnimationEnd(animation);
									 
									endToFinish();
								}
							});
							 
							animator.start();
						}else{
							ObjectAnimator animator= ObjectAnimator.ofFloat(getChildAt(0), "translationX", t,0);
							animator.setDuration(time);
							animator.addListener(new AnimatorListenerAdapter(){
								@Override
								public void onAnimationEnd(Animator animation) {
									// TODO Auto-generated method stub
									super.onAnimationEnd(animation);
									getChildAt(0).setAlpha(1);
									setAlpha(1);
									  
								}
							});
							animator.start();
						}
					}
					
					break;
			}
			
			 
			
			return true;
		}
		
		public    int dip2px(  float dpValue) {  
	        final float scale = context.getResources().getDisplayMetrics().density;  
	        return (int) (dpValue * scale + 0.5f);  
	    }  

		
		public void canTouch(boolean flag) {
			isTouchable=flag;
		}
	}
	
}

下载地址   http://pan.baidu.com/s/1kT1PvRD

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
仿iPhone热门应用栏是一个非常流行的产品趋势,许多手机品牌已经开始推出类似的产品。仿iPhone热门应用栏是指手机系统底部的一排常用应用图标,用户可以轻松访问和切换这些应用程序。 优点: 1. 提高用户体验:仿iPhone热门应用栏将常用的应用程序放置在屏幕底部,用户可以轻松地操作和访问这些应用。这种设计提高了用户的便利性和操作效率。 2. 提升工作效率:对于经常使用特定应用程序的用户来说,将它们放在热门应用栏中可以节省寻找和打开应用的时间,提高工作效率。 3. 可定制性:用户可以根据自己的需求和喜好,在热门应用栏中添加或删除特定的应用程序。这种可定制性使得用户可以根据自己的使用习惯进行个性化设置。 缺点: 1. 屏幕空间受限:仿iPhone热门应用栏通常会占据屏幕底部的一部分空间,可能会减少用户在其他应用程序中可见的屏幕空间。这对于一些需要较大屏幕空间才能展示内容的应用程序来说可能是一个问题。 2. 可能存在兼容性问题:由于不同手机品牌和操作系统之间存在差异,仿iPhone热门应用栏可能在某些设备上无法正常运行或显示。这需要开发人员在设计和开发过程中考虑到兼容性问题。 总结: 仿iPhone热门应用栏是一种流行的设计趋势,它提供了一种方便和高效的方式来访问和切换常用应用程序。虽然它具有许多优点,但也存在一些限制和挑战,特别是在屏幕空间和设备兼容性方面。因此,在设计和实施仿iPhone热门应用栏时,需要综合考虑这些因素,并根据特定的用户需求进行定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值