How to animate the view by using the absolute coordinate mode and how to get its cachebitmap and move when the view is clicked

Source: Internet
Author: User

Layout:

<?xml version= "1.0" encoding= "UTF-8"? ><com.example.android_test. Mydraglayer xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/rootview    " Android:layout_width= "Fill_parent"    android:layout_height= "Fill_parent" > <com.example.android_test. Mylinearlayout     android:id= "@+id/linear"    android:layout_width= "fill_parent"    android:layout_height= " Fill_parent "></com.example.android_test. Mylinearlayout></com.example.android_test. Mydraglayer>

Mylinearlayout, with detailed notes

public class Mylinearlayout extends Relativelayout {private View mdragview;private Bitmap mdragbitmap = null;private int m    bitmapoffsetx;private int mbitmapoffsety;private Boolean mdragging = False;public Mydraglayer mDragLayer;    /** * X offset from where we touched on the cell to its upper-left corner */private float mtouchoffsetx;    /** * Y offset from where we touched on the cell to its upper-left corner */private float mtouchoffsety; Private static final float Drag_scale = 18.0f; Zoom in on how much private float mlastmotionx;private float mlastmotiony;private ImageView mview;public mylinearlayout (Context Context, AttributeSet Attrs) {Super (context, attrs); Inflate (context, r.layout.relative_ex, this); init (context);} private void init (context context) {Final int top = This.getresources (). Getdimensionpixelsize (r.dimen.top); final ImageView Image1 = (ImageView) Findviewbyid (R.ID.ITEM1); final ImageView image2 = (ImageView) Findviewbyid (R.ID.ITEM2); Final int x = 50;final INT y = top;mview = image2;final int switch_to_left = 50;//Click btn1 the Image1 will move to the left 50 of the screen button btn1 = (Button) findviewbyi D (R.ID.BTN1); Btn1.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {final mytranslateanimation ani = new Mytranslateanimation (Image1.getleft ()-Switch_to_left, 0, Image1.gettop ()-y, 0);//Trans Lateanimation (float Fromxdelta, float toxdelta, float//Fromydelta, float toydelta)//float Fromxdelta: This parameter represents the difference between the point at which the animation starts and the current view x-coordinate, or float toxdelta, which represents the difference between the point at which the animation ends and the current view x-coordinate;//If the view is in a (x, y) point then the animation is from point B (X+fromxdelta, Y +fromydelta) point to the c//Point (X+toxdelta,y+toydelta) point. ani.setduration, ani.dstx = Switch_to_left;ani.dsty = y;// Ani.setfillbefore (true); Ani.view = image1;//This causes the view to remain at the left switch_to_left of the distance ani.view.layout (ANI.DSTX, Ani.dsty, Ani.dstx + ani.view.getWidth (), Ani.dsty + ani.view.getHeight ()); Ani.setanimationlistener (new Animation.animationlistener () {public void Onanimationstart (Animation Animation) {}public void Onanimationrepeat (Animation Animation) {}public void Onanimationend (Animation Animation) {ani.view.setVisibility (view.visible);}}); Image1.setvisibility (view.invisible); Image1.startanimation (ANI);}); /Long press Mview to get its cache and move mview.setonlongclicklistener (new View.onlongclicklistener () {@Overridepublic Boolean Onlongclick (View v) {StartDrag (); return false;}}); @Overridepublic boolean onintercepttouchevent (motionevent ev) {//System.out.println ("onintercepttouchevent:" + ev); int action = Ev.getaction (), final float x = Ev.getx (), final float y = ev.gety (), if (action = = Motionevent.action_down) {ML Astmotionx = X;mlastmotiony = Y;requestfocus ();} if (mdragging) {return true;} return super.onintercepttouchevent (EV);} Class Mytranslateanimation extends Translateanimation {public int dstx;public int dsty;public View view;public Mytranslat Eanimation (float fromxdelta, float toxdelta,float fromydelta, float Toydelta) {super (Fromxdelta, Toxdelta, Fromydelta, Toydelta);}} public void StartDrag () {View view = mview;if (view!)= null) {Mdragview = View;mdragging = true;//itself This view is first placed as invisibleview.setvisibility (view.invisible); Rect r = new rect (); R.set (View.getscrollx (), view.getscrolly (), 0, 0);// Convert the coordinates of the view component to the same coordinate system offsetdescendantrecttomycoords (view, R);//mtouchoffsetx the position of the finger relative to the view Mtouchoffsetx = Mlastmotionx-r.left;mtouchoffsety = Mlastmotiony-r.top;view.clearfocus (); view.setpressed (false); Boolean Willnotcache = View.willnotcachedrawing (); view.setwillnotcachedrawing (false);//Reset the drawing cache background Color to fully transparent//for the duration of this operationint color = View.getdrawingcachebackgroundcolor (); view.setd Rawingcachebackgroundcolor (0);//If there is a cache then destroyif (color! = 0) {View.destroydrawingcache ();} Builddrawingcache after the Getdrawingcache can be taken to the view of the Bitmapview.builddrawingcache (); Bitmap Viewbitmap = View.getdrawingcache (); int width = viewbitmap.getwidth (); int height = viewbitmap.getheight (); Matrix scale = new matrix (), Float scalefactor = View.getwidth (); scalefactor = (Scalefactor + DRAg_scale)/Scalefactor;scale.setscale (Scalefactor, scalefactor); mdragbitmap = Bitmap.createbitmap (viewBitmap, 0, 0, width, Height,scale, true);//Here Destroy this cacheview.destroydrawingcache (); View.setwillnotcachedrawing (Willnotcache ); View.setdrawingcachebackgroundcolor (color); final Bitmap dragbitmap = Mdragbitmap;mbitmapoffsetx = ( Dragbitmap.getwidth ()-width)/2;mbitmapoffsety = (dragbitmap.getheight ()-height)/2;invalidate (); Mdraglayer.startdrag (Mdragbitmap, (int) (Mtouchoffsetx + mbitmapoffsetx), (int) (mtouchoffsety + mbitmapoffsety));}}}

Mydraglayer:

public class Mydraglayer extends Framelayout {/** * The bitmap it is currently being dragged */private bitmap Mdragbitma p = null;private float mlastmotionx;private float mlastmotiony;private float moffsetx;private float moffsety;private Pain T mdragpaint;private static final int transition_duration = 250;public Mydraglayer (context context, AttributeSet Attrs) {s Uper (context, attrs);} @Overridepublic boolean onintercepttouchevent (motionevent ev) {Mlastmotionx = Ev.getx (); mlastmotiony = Ev.gety (); int DX = 0;int dy = 0;invalidate (); Boolean result = Super.onintercepttouchevent (EV); return result;} @Overrideprotected void <strong>dispatchDraw</strong> (canvas canvas) {<strong>// This method can be acquired when the cache on the screen moves with the finger </strong>super.dispatchdraw (canvas); if (mdragbitmap! = NULL &&! Mdragbitmap.isrecycled ()) {//Draw actual icon being Draggedcanvas.drawbitmap (Mdragbitmap, GETSCROLLX () + mlastmotionx- Moffsetx, getscrolly () + Mlastmotiony-moffsety,mdragpaint);}} public void STARtdrag (Bitmap Bitmap, int offsetx, int offsety) {<strong>//This method passed in bitmap</strong>mdragbitmap = Bitmap; Moffsetx = Offsetx;moffsety = Offsety;mdragpaint = Null;invalidate ();}}


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.