Android custom View-like Ctrip homepage click zoom ImageView
I recently launched a Ctrip App. I clicked on the home page and noticed that when I clicked the button, it wasn't the color-changing effect we often saw. Instead, I first contracted it. When I opened it, back to the original size, I felt that this effect was small, but it was very novel. So I decided to imitate this small effect and first look at the effect on Ctrip, as shown in:
Let's take a look at the simulated results, as shown in. The results are basically the same.
0 .. 0 no black borders will appear on the real machine. I don't know why on the simulator...
First, let's talk about the overall idea: 1. inherit ImageView and rewrite the onTouchEvent method.
2. Trigger Our zoom-in property animation when we press down.
3. Trigger the zoom-in animation when the image is up.
4. Define an interface callback to respond to the processing of click events.
The following is the implementation code:
Package view; import module. busEvent; import android. animation. animator; import android. animation. objectAnimator; import android. animation. propertyValuesHolder; import android. content. context; import android. OS. handler; import android. util. attributeSet; import android. view. motionEvent; import android. view. animation. linearInterpolator; import android. widget. imageView; import de. greenrobot. event. eventBus ;/*** @ Author rzq **/public class ClickImageView extends ImageView {private Animator anim1; private Animator anim2; private int mHeight; private int mWidth; private float mX, mY; private Handler mHandler = new Handler (); private ClickListener listener; public ClickImageView (Context context, AttributeSet attrs) {super (context, attrs); init ();} @ Overrideprotected void onSizeChanged (int w, int h, int oldw, int old H) {super. onSizeChanged (w, h, oldw, oldh); mHeight = getHeight ()-getPaddingTop ()-getPaddingBottom (); mWidth = getWidth ()-getPaddingLeft ()-getPaddingRight (); mX = getX (); mY = getY ();} private void init () {PropertyValuesHolder valueHolder_1 = PropertyValuesHolder. ofFloat ("scaleX", 1f, 0.9f); PropertyValuesHolder valuesHolder_2 = PropertyValuesHolder. ofFloat ("scaleY", 1f, 0.9f); anim1 = ObjectAnima Tor. ofPropertyValuesHolder (this, valueHolder_1, valueholder_2); anim1.setDuration (200); anim1.setInterpolator (new LinearInterpolator (); PropertyValuesHolder valueHolder_3 = PropertyValuesHolder. ofFloat ("scaleX", 0.9f, 1f); PropertyValuesHolder valuesHolder_4 = PropertyValuesHolder. ofFloat ("scaleY", 0.9f, 1f); anim2 = ObjectAnimator. ofPropertyValuesHolder (this, valueHolder_3, valuesHolder_4); anim2.setDu Ration( 200); anim2.setInterpolator (new LinearInterpolator ();} public void setClickListener (ClickListener clickListener) {this. listener = clickListener;} @ Overridepublic boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: mHandler. post (new Runnable () {@ Overridepublic void run () {anim2.end (); anim1.start () ;}}); break; case MotionEvent. ACTION_MOVE: break; case M OtionEvent. ACTION_UP: mHandler. post (new Runnable () {@ Overridepublic void run () {anim1.end (); anim2.start () ;}}); if (listener! = Null) {listener. onClick ();} // EventBus. getDefault (). post (BusEvent. TYPE); break; case MotionEvent. ACTION_CANCEL: break;} return true;} // whether the pressed point is within the View protected boolean innerImageView (float x, float y) {if (x> = mX & y <= mX + mWidth) {if (y> = mY & y <= mY + mHeight) {return true ;}} return false;}/*** Click Event Processing callback * @ author renzhiqiang **/public interface ClickListener {public void onClick () ;}@ Overrideprotected void onDetachedFromWindow () {// TODO Auto-generated method stubsuper. onDetachedFromWindow ();}}
Use in Activity:
Package activity. animatior; import view. clickImageView; import view. clickImageView. clickListener; import android. app. activity; import android. OS. bundle; import android. widget. toast; import com. example. listviewanimationdemo. r; public class ClickImageActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. click_image_layout); ClickImageView view = (ClickImageView) findViewById (R. id. image_view_1); view. setClickListener (new ClickListener () {@ Overridepublic void onClick () {Toast. makeText (ClickImageActivity. this, "ImageView is clicked... ", Toast. LENGTH_SHORT ). show () ;}}) ;}@ Overrideprotected void onDestroy () {// TODO Auto-generated method stubsuper. onDestroy ();}}