How to use absolute coordinates to animation the view and how to obtain and move its cachebitmap when clicking the view,

Source: Internet
Author: User

How to use absolute coordinates to animation the view and how to obtain and move its cachebitmap when clicking the view,

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 comments

Public class MyLinearlayout extends RelativeLayout {private View mDragView; private Bitmap mDragBitmap = null; private int mBitmapOffsetX; 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; // how many private float flood; 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 (). getDimensionPix ElSize (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; // when you click btn1, image1 will be moved to the position 50 from the left of the screen. Button btn1 = (Button) findViewById (R. id. btn1); btn1.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// final MyTranslateAnimation ani = new inline (// image1.getLeft ()-switch_to_left, 0, image1.getTop () //-y, 0); // TranslateAnimation (float fromXDelta, float toXDelta, float //// fromYDelta, float toYDelta) /// float fromXDelta: the difference between the start point of the animation and the coordinates of the current View X; // float toXDelta, this parameter indicates the difference between the animation end point and the current View X coordinate; // if the view is in A (x, y) then, the animation moves from point B (x + fromXDelta, y + fromYDelta) to point C // (x + toXDelta, y + toYDel Ta. /// ani. setDuration (300); // ani. dstX = switch_to_left; // ani. dstY = y; // ani. setFillBefore (true); // ani. view = image1; // This makes the view last stay at the position left by switch_to_left. // 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 voi D onAnimationRepeat (Animation animation) {//} // public void onAnimationEnd (Animation animation) {// ani. view. setVisibility (View. VISIBLE); //}); // image1.setVisibility (View. INVISIBLE); // image1.startAnimation (ani); TranslateAnimation trans = new TranslateAnimation (0,-switch_to_left, 0, 0); trans. setFillAfter (true); trans. setDuration (300); image1.startAnimation (trans) ;}}); // you can obtain the cache and move the mView by pressing mView. se TOnLongClickListener (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) {mLastMotionX = x; mLastMotionY = y; re QuestFocus ();} if (mDragging) {return true;} return super. onInterceptTouchEvent (ev);} class extends TranslateAnimation {public int dstX; public int dstY; public View view; public comment (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; // The view is set to invisibleview first. setVisibility (View. INVISIBLE); Rect r = new Rect (); r. set (view. getScrollX (), view. getScrollY (), 0, 0); // convert the coordinates of the view component to offsetDescendantRectToMyCoords (view, r) in the same coordinate system ); // mTouchOffsetX indicates the click position of the finger relative to the view. mTouchOffsetX = mLastMotionX-r. left; mTouchOffsetY = mLastMotionY-r. top; view. clearFocus (); view. setPressed (false); boolean willNotCache = vi Ew. willNotCacheDrawing (); view. setWillNotCacheDrawing (false); // Reset the drawing cache background color to fully transparent // for the duration of this operationint color = view. getDrawingCacheBackgroundColor (); view. setDrawingCacheBackgroundColor (0); // destroyif (color! = 0) {view. destroyDrawingCache ();} // After buildDrawingCache, you can use getDrawingCache to obtain the bitmapview of the view. 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); // destroy cacheview here. 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 that is currently being dragged */private Bitmap mDragBitmap = null; private float mLastMotionX; private float mLastMotionY; private float mOffsetX; private float mOffsetY; private Paint mDragPaint; private static final int TRANSITION_DURATION = 250; public MyDragLayer (Context context, AttributeSet attrs) {super (context, attrs) ;}@ Overridepubli C 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> // The cache obtained when this method is used. Move your finger on the screen </strong> super. dispatchDraw (canvas); if (mDragBitmap! = Null &&! MDragBitmap. isRecycled () {// Draw actual icon being draggedcanvas. drawBitmap (mDragBitmap, getScrollX () + response-mOffsetX, getScrollY () + response-mOffsetY, mDragPaint);} public void startDrag (Bitmap bitmap, int offsetx, int offsety) {<strong> // This method is used to pass in Bitmap </strong> mDragBitmap = bitmap; mOffsetX = offsetx; mOffsetY = offsety; mDragPaint = null; invalidate ();}}

:



Code: http://download.csdn.net/detail/baidu_nod/7759965




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.