package com.example.scaleview;
import android.content.Context;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class ScaleView extends ImageView{
Context context;
public ScaleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context=context;
// TODO Auto-generated constructor stub
}
public ScaleView(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public ScaleView(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Animation big,small;
big=AnimationUtils.loadAnimation(context, R.anim.big);
small=AnimationUtils.loadAnimation(context, R.anim.small);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.clearAnimation();
this.startAnimation(small);
this.clearAnimation();
this.startAnimation(big);
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
R.anim.big和R.anim.small
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:fromXScale="0.3"
android:toXScale="1"
android:fromYScale="0.3"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"
/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
android:fromXScale="1"
android:toXScale="0.3"
android:fromYScale="1"
android:toYScale="0.3"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"
/>
</set>