First look at the effect:
It's a good result!
Before entering the topic, first understand ImageView's ScaleType Center_crop, the network said is already very clear: The following copy from the network:
-
Android:scaletype= "Centercrop"
To fill the whole imageview for the purpose, the center of the original image to the center of the ImageView, and so on to enlarge the original image until the filling imageview (refers to the width and height of the ImageView to fill), the original version of more than the imageview part of the cut.
Scales the image evenly (preserving the original proportions of the image) so that the two coordinates (width, height) of the picture are greater than or equal to the corresponding view coordinates (negative padding). The image is in the center of the view. Syntax that can be used in XML: Android:scaletype= "Centercrop".
Do not say nonsense, directly into the theme!!
Ideas
- First, the layout of the TopView and the ListView, and then the TopView and TopView wrapped ImageView in the ListView, that is, the activity is generally layout
- Rewrite the Ontoucevent () method of the ListView, but without any interception, control the height of the ImageView as well as the TopView at the action, so that it can be re-layout and then re-laid out.
- The above is the approximate idea, here the specific analysis: when the Action_down record its initial position, action_move to get dy, through the dy to determine whether it is up or down:
(1) dy>0, is the drop-down, constantly reset the height of TopView and ImageView, and because ImageView Scaletype=center_crop, so the picture will be in accordance with this rule to stretch, When you reach the maximum picture, there is a process of amplification.
When the hand is released or the finger moves out of the screen (action_up|action_outside|action_cancel), it returns to its original position and is accompanied by a rebound process, where it has a rebound effect by customizing the animation.
(2) dy<0, is the pull up, push the process, because TopView and ImageView do not have the effect of scrolling, so the push is also through the control of TopView and ImageView height, And when TopView and ImageView slide out of the screen, they do not change height to prevent continuous drawing and improve performance.
OK, this is the general idea. The specific analysis code is as follows:
Realize:
Activity's XML
Stretch_act.xml:
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent"android:layout_height="Match_parent" Android:orientation="vertical"> <!--this is topview--> . <relativelayoutandroid:id= "@+id/rl_top"android:layout_width=" Match_parent "android:layout_height=" Wrap_content " > <!--This is ImageView, be sure to set ScaleType to Centercrop--> <imageview android:id= "@+id/iv_stretch_pic" android:layout_width= "match_parent" android:layout_height =< Span class= "Hljs-value" > "wrap_content" android:scaletype = "Centercrop" android:src = "@dr awable/stretch_s "/> <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:layout_alignbottom= "@id/iv_stretch_pic"android:text="You are the most beautiful, you are the coolest ... ^^" android:textsize="16sp"/> </relativelayout><!--This is a custom listview--> <com.example.zwr.myapplication.widget.StretchListViewandroid:id="@+id/listview" android:layout_width="Match_parent"android:layout_height="Match_parent" android:cachecolorhint="@null"android:divider= "@null"Android : Listselector="#00000000"/> </linearlayout>
Look at Stretchlistview:
/*** * @param topView * @param imgResId 图片id */ publicvoidsetTopViewint imgResId) { if (null != topView) { this.topView = topView; imageView = (ImageView) topView.findViewById(imgResId); } }
Through this external method, the ID of TopView and ImageView is passed in.
Analysis: Re-ontouchevent the ListView ():
ACTION_DOWN: case MotionEvent.ACTION_DOWN: startY = ev.getRawY(); if (!hadInit) {//初始化,只要初始化一次就够了 childAt0Top = getChildAt(0).getTop(); ivInitHeight = imageView.getHeight(); hadInit = true; } break;
Just a few initialization actions:
1. startY:相对于屏幕顶部的高度 2. childAt0Top,获取listview的第一个view的top距离、 3. ivInitHeight:获取ImageView的初始高度,即刚进来时的高度
Action_move:
CaseMOTIONEVENT.ACTION_MOVE:LOG.D (TAG,"dy ="+ dy); DY = Ev.getrawy ()-starty;if(Dy >0&&0= = Getfirstvisibleposition () && childat0top = = Getchildat (0). GetTop ()) {//(1) finger pull down from top: drop down intTempdy = (int) (Dy +0.5);//must also give TopView a certain height, otherwise from the top to the drop will not showImageview.getlayoutparams (). Height = imageview.getheight () + tempdy; Topview.getlayoutparams (). Height = topview.getheight () + tempdy; Topview.requestlayout (); Ischangedheight =true; }Else{//(2) finger from bottom to top: Pull up intTempdy = (int) (Dy-0.5);intCurrheight = Imageview.getheight ();floatTranslationy = Getnegativemaxvalue (Tempdy,-currheight,0);if(Translationy <=0&& currheight >0) {linearlayout.layoutparams LP = (linearlayout.layoutparams) topview.getl Ayoutparams ();//must subtract titlebar, if not remove the Winow.xxx.Title, but also to subtract this height, otherwise it will show not fullLp.height = Topview.getheight () + (int) Translationy; Topview.requestlayout ();//Ischangedheight =true; } }//Use this getrawy instead of Gety, because the ListView will change as well, //And Gety gets the listview itself y, so the basic change is not small, //Use Getrawy relative to the screen to ensure that the distance is changed by how far the slide is.Starty = Ev.getrawy (); Break;
Current Pull-down: the main conditions are as follows:
- Dy > 0 && 0 = = getfirstvisibleposition () && childat0top = =
Getchildat (0). GetTop ()
This means that the current pull, and the first position of the ListView display, in order to pull the enlarged image, which is to avoid, the ListView has been rolling, need to go back to the initial position in order to zoom in, or it will appear, immediately drop-zoom, experience bad
- Main conditions when pulling up
if (translationy <= 0 && currheight > 0)
Currheight>0: The height of the current ImageView, if it has scrolled to the top or exceeded, no longer scrolls to prevent the scrolled out of the screen from being seen and scrolling.
Translationy<=0: This value is the distance of the scroll, the distance cannot exceed the height of imageview, because the DY is negative when the pull up, so to determine whether it is less than 0, the main method is as follows:
float translationy = Getnegativemaxvalue (TempDy,- Currheight, 0 ); /*** * finger move up process dy is negative * return negative maximum: 0 is the maximum value, can not exceed * * @par Am value moves the final distance: the last position + the sum of the offsets of the second move is the final offset of this minor move * @param Canmov Emaxvalue movable maximum value * @param maxValue * @return
*/ public static float getnegativemaxvalue (float value,float canmovemaxvalue, float MaxValue) {return math.min (MaxValue, Math.max (Canmovemaxvalue, value)); }
ACTION_UP:
CaseMotionevent.action_outside: CaseMotionevent.action_cancel: CaseMOTIONEVENT.ACTION_UP:if(Ischangedheight) {if(Imageview.getheight () > Ivinitheight) {//(1) finger pull down from top: drop downResetanimation resetanimation =NewResetanimation (Ivinitheight, ImageView, TopView); Resetanimation.setduration ( $); Imageview.startanimation (resetanimation); }Else{//(2) finger from bottom to top: Pull up ... This does not need to deal with ... Because pull up and let it topview fixed} ischangedheight =false; } Break;
Ischangedheight: When a imageview occurs and is pulled down, this is the release of the hand or the finger out of the screen, it bounces back to the original position, here is the custom animation to change the height of its change, to achieve the rebound effect code as follows
/** * Custom bounce animations to make ImageView and TopView transition back to initial position */ StaticClass Resetanimation extends Animation {PrivateView TopView;Private intTopcurrheight;PrivateImageView Ivstretch;Private intIvinitheight;Private intIvcurrheight; Public resetanimation(intIvinitheiht, ImageView Ivstretch, View topView) { This. ivinitheight = ivinitheiht; This. Ivcurrheight = Ivstretch.getheight (); This. Topcurrheight = Topview.getheight (); This. Ivstretch = Ivstretch; This. TopView = TopView; }@Override protected void applytransformation(floatInterpolatedtime, transformation t) {intDY = (int) ((ivcurrheight-ivinitheight) * interpolatedtime); LOG.D (TAG,"Anim dy ="+ dy); Ivstretch.getlayoutparams (). height = Ivcurrheight-dy; Topview.getlayoutparams (). height = Topcurrheight-dy; Topview.requestlayout (); } }
In fact the main is applytransformation (float interpolatedtime, transformation t) this method
Mainly through this gradient factor interpolatedtime control, its value range is (0~1) so the height of the calculation gradient is as follows
int dy = (int) ((ivcurrheight-ivinitheight) * interpolatedtime);
Then be sure to recall calling Topview.requestlayout () to have it re-layout drawn. This is done, all code, also about 100 lines of code around, is not very simple. And through this demo, can be well extended to the ScrollView.
Note:
Some of the demos online are doing things through Overscrollby (), because
/*** * This method is a callback when sliding out of the screen, but because the Android system domestic manufacturers modified beyond recognition, some models will not be callback, such as Vivo * so do not use this method to do things * * @param SC ROLLX * @param scrolly * * @Override protected Boolean Overscrollby(intDeltaX,intDeltaY,intSCROLLX,intScrolly,intScrollrangex,intScrollrangey,intMAXOVERSCROLLX,intMaxoverscrolly,BooleanIstouchevent) {LOG.D (TAG,"DeltaX ="+ DeltaX +"DeltaY ="+ DeltaY);return Super. Overscrollby (DeltaX, DeltaY, Scrollx, scrolly, Scrollrangex, Scrollrangey, Maxoverscrollx, MaxOverScrollY, IsTouc hevent); }
其自带dy,还有一些其它的参数,应有尽有。但是由于android系统是开源的导致有些系统是无法回调这个方法的,以至于无法实现回弹效果(比如:vivoX5)等等。所以在onTouchEvent()搞事情才是王道
Ok! , please do not hesitate to correct the problem!!!
demo:http://download.csdn.net/detail/zhongwn/9765268
Android Super Simple pull back-imitation QQ personal homepage