Android Imitation QQ Red Envelopes Collection Details Interface animation

Source: Internet
Author: User

In order to be able to clearly see this effect, I do not hesitate to spend a lot of money to the crowd in the distribution of red envelopes, to see red envelopes to receive the details of the animation effect of the interface, QQ effect

In the picture, we can see that the picture and the text of the animation are together, that is, at the same time and is the whole, attention, is the overall scale, and is moving with the finger to transform the size, if we follow the normal layout method, Imageview+textview way, It is impossible to accomplish this by using the normal animation that comes with Android, so it can only be achieved by customizing the view!

Custom view requires you to have a certain understanding of the internal drawing mechanism of view, such as Onmeasure,measurespec.exactly,measurespec.at_most,ondraw and so on, I do not explain in detail, Online introduction of the article very much! If you read one of my previous articles: Imitation American takeaway, then this article will be easier to understand!

Well, let's take a look at the implementation of this article:

Repeated thinking, according to the previous imitation of the United States to sell the animation to achieve the idea, according to the slide monitoring of a picture of the scale or relatively simple, and then add a few lines after the text to do? As said above, the animation is the whole, so the picture and text can not be implemented in two view, it can only be in a view, painting the picture of the text chant, Ah, that's it!

First, to make it easier to achieve results, the red area of the topmost avatar and the avatar need to be set to a fixed height, while the width can be set to a fixed value or match_parent! Perhaps you would think that if the width of the height is different, the avatar will be deformed? The answer is that if you have a custom view that measures the width and height and redraws it without doing something special, the deformation is inevitable!

First look at a set of code:

    @Override    protectedvoidonSizeChanged(intintintint oldh) {        super.onSizeChanged(w, h, oldw, oldh);        measuredWidth = w;        measuredHeight = h;        //将bitmap按测量宽高设置,Math.min(measuredWidth, measuredHeight)取宽高最小值        scalebitmap = Bitmap.createScaledBitmap(bitmap,Math.min(measuredWidth, measuredHeight),Math.min(measuredWidth, measuredHeight),true);    }

After measuring the width height by the Onmeasure method, the measured width and height value can be obtained in the onsizechanged,

scalebitmap = Bitmap.createScaledBitmap(bitmap,Math.min(measuredWidth, measuredHeight),Math.min(measuredWidth, measuredHeight),true);

This means that the original bitmap of the picture is regenerated by the measured width and height of a bitmap for redrawing, and

Math.min(measuredWidth, measuredHeight)

The width height is calculated by the minimum value of the measurement width high school, for example, we set the width of the custom view in XML: Android:layout_width= "200DP", High: android:layout_height= "80DP", Then the final picture size is 80X80DP, and the entire view of the rest of the left side is left to the text (note, it is best to set the width of the view to match_parent more appropriate)!

Then look at the OnDraw part of the code:

    @Overrideprotected void OnDraw (CanvasCanvas) {Super.ondraw (Canvas);//Painting pictures        Canvas. Scale(Mcurrentprogress, Mcurrentprogress,0, measuredheight);Canvas. Drawbitmap (Scalebitmap,0,0, Mpaint);//math.min (measuredwidth, measuredheight) picture width height        //Draw Text 1, starting coordinates x: For picture width y:50Mpainttext1.settextsize (SizeText1*mcurrentprogress);Canvas. DrawText (Text1, Math.min(Measuredwidth, Measuredheight), -, MPAINTTEXT1);//Draw Text 2, starting coordinates x: For picture width y: Picture height -50Mpainttext2.settextsize (sizeText2*mcurrentprogress);Canvas. DrawText (Text2, Math.min(Measuredwidth, Measuredheight), Math.min(Measuredwidth, Measuredheight)- -, MPAINTTEXT2); }

First, the extent of the image and text is based on the value of mcurrentprogress, whether the picture is based on the scale method to the canvas mcurrentprogress and the text is based on the size of the brush you set * The scale value is mcurrentprogress; the outer layer of our entire layout is used ScrollView not to mention, while Mcurrentprogress is based on the listening scrollview sliding distance compared to the height of the head area:

Myscrollview.setscrollviewlistener (NewScrollviewlistener () {@Override             Public void onscrollchanged(Myscrollview ScrollView,intXintYintOLDX,intOldy) {//1.0f is equivalent to a scaling factor, the larger the sensitivity of the finger scaling is lower                if(y>0&&y<=height) {myview.setcurrentprogress (1-(float) (y*1.0f)/(float) height);                Myview.postinvalidate (); }            }        });

The next point to note:

1. Drawing pictures

canvas.scale(mCurrentProgress, mCurrentProgress,0, measuredHeight);

The latter two parameters represent setting a zoom point (coordinate) and then zooming in to this point as the center!

2. Drawing text

Text 1:

canvas.drawText(text1, Math.min50, mPaintText1);

The middle two parameters represent the brush start coordinates, x is the picture width, and y is 50 pixels;

Text 2:

canvas.drawText(text2, Math.min(measuredWidth, measuredHeight), Math.min(measuredWidth, measuredHeight)-50, mPaintText2);

X is still the width of the picture, and y is the height of the picture-50 pixels (note the picture's width is the same), it is easy to understand it!

When the finger is constantly moving on the screen, it will stop calling myview.setcurrentprogress incoming progress, and call Postinvalidate () to redraw, so that the size of the view follows the finger move zoom!

In addition, you may want to say, if the real project, the avatar can not be a fixed image, if the dynamic acquisition, what should be? In fact, the following code is in the Init method of Custom view:

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head);

So, before we draw the network to get the image of the bitmap in the pass it?

Of course, the method used in this article is only a train of thought, certainly not the best, we brainstorm ideas, more divergent their own thinking, will own a better way to share the realization of common progress!

Known defects: The zoom effect of the view will be error when the screen is moved up or down quickly, and it is normal to be re-touched. (QQ interface has similar problems)

At present I did not find the reason, small partners, take out your intelligence, together to solve the problem!

ecdemo:http://download.csdn.net/detail/baiyuliang2013/9514509

Android Imitation QQ Red Envelopes Collection Details Interface animation

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.