实现效果两个文件
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);
}
}
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>