点击缩小的自定义布局


实现效果两个文件

ScaleAnimationHelper

package school.zn.publib.view.RelativeLayout;

import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;

public class ScaleAnimationHelper {
	private static int DURATION = 200;
	private float scale ;
	ScaleAnimation myAnimation_Scale;

	public ScaleAnimationHelper(float scale_size) {
		scale = scale_size;
	}

	// 放大的类,不需要设置监听器
	public void ScaleOutAnimation(View view) {
		myAnimation_Scale = new ScaleAnimation(scale, 1.0f, scale, 1f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		myAnimation_Scale.setInterpolator(new AccelerateInterpolator());
		AnimationSet aa = new AnimationSet(true);
		aa.addAnimation(myAnimation_Scale);
		aa.setDuration(DURATION);
		aa.setFillAfter(true);
		view.startAnimation(aa);
	}

	public void ScaleInAnimation(View view) {
		myAnimation_Scale = new ScaleAnimation(1.0f, scale, 1.0f, scale,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		myAnimation_Scale.setInterpolator(new AccelerateInterpolator());
		AnimationSet aa = new AnimationSet(true);
		aa.addAnimation(myAnimation_Scale);
		aa.setDuration(DURATION);
		aa.setFillAfter(true);
		view.startAnimation(aa);
	}
}


HomeRelativeLayout

package school.zn.publib.view.RelativeLayout;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import school.zn.publib.R;


@SuppressLint("HandlerLeak")
public class HomeRelativeLayout extends RelativeLayout {

	private ScaleAnimationHelper scaleAnimationHelper;
	private float scale_size = 1.0f;
    private boolean isnormal=true;

	private OnViewClick mOnViewClick;

	public HomeRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.HomeRelativeLayout);
		scale_size = ta.getFloat(R.styleable.HomeRelativeLayout_scale_size,
				1.0f);
		scaleAnimationHelper = new ScaleAnimationHelper(scale_size);
	}

	public HomeRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.HomeRelativeLayout);
		scale_size = ta.getFloat(R.styleable.HomeRelativeLayout_scale_size,
				1.0f);
		scaleAnimationHelper = new ScaleAnimationHelper(scale_size);
	}

	public void setOnViewClick(OnViewClick onViewClick) {
		mOnViewClick = onViewClick;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
            if(isnormal) {
                scaleAnimationHelper.ScaleInAnimation(HomeRelativeLayout.this);
                isnormal=false;
            }
			break;
		case MotionEvent.ACTION_MOVE:
            if(!isnormal) {
                scaleAnimationHelper.ScaleOutAnimation(HomeRelativeLayout.this);
                isnormal=true;
            }
			break;
		case MotionEvent.ACTION_UP:
			int[] location = new int[2];
			HomeRelativeLayout.this.getLocationOnScreen(location);

			if (inRangeOfView(HomeRelativeLayout.this, event)
					&& mOnViewClick != null) {
				mOnViewClick.onClick(HomeRelativeLayout.this.getId());
			}

            if(!isnormal) {
                scaleAnimationHelper.ScaleOutAnimation(HomeRelativeLayout.this);
                isnormal=true;
            }
			break;
		}
		return true;
	}

	private boolean inRangeOfView(View view, MotionEvent ev) {

		float X = ev.getRawX();
		float Y = ev.getRawY();

		int[] location = new int[2];
		view.getLocationOnScreen(location);
		int view_fromx = location[0];
		int view_tox = location[0] + view.getWidth();

		int view_fromy = location[1];
		int view_toy = location[1] + view.getHeight();

		if (X < view_fromx || X > (view_tox) || Y < view_fromy
				|| Y > (view_toy)) {
			return false;
		}
		return true;
	}

	public interface OnViewClick {
		public void onClick(int viewID);
	}
}


用法:

布局中使用

      <school.zn.publib.view.RelativeLayout.HomeRelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"
                android:layout_weight="1.0"
                android:background="@drawable/red_corners_big"
                scale:scale_size="0.95" >

                <TextView
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="考 勤"
                    style="@style/Tab_Text" 
                />
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" 
                    android:src="@mipmap/common_btn_homepage_hotel"/>


            </school.zn.publib.view.RelativeLayout.HomeRelativeLayout>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值