Android custom View-like Ctrip homepage click zoom ImageView, androidimageview

Source: Internet
Author: User
Tags eventbus

Android custom View-like Ctrip homepage click zoom ImageView, androidimageview

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.

I changed a simulator. Okay ............

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:

<Span style = "font-size: 14px;"> 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. gr Eenrobot. 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 (I Nt w, int h, int oldw, int oldh) {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 = ObjectAnimator. 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.setDuration (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 Motion Event. ACTION_MOVE: break; case MotionEvent. 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 () ;}</span>
Use in Activity:

  

<Span style = "font-size: 14px;"> 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 () ;}}</span>

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.