如何採用絕對座標的方式動畫方式移動view和如何點擊view的時候擷取它的cachebitmap並移動,

來源:互聯網
上載者:User

如何採用絕對座標的方式動畫方式移動view和如何點擊view的時候擷取它的cachebitmap並移動,

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,帶詳細注釋

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; // 放大多少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;//點擊btn1的時候image1將移動到距離螢幕左邊50的位置Button btn1 = (Button) findViewById(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);//// TranslateAnimation(float fromXDelta, float toXDelta, float//// fromYDelta, float toYDelta)//// float fromXDelta:這個參數表示動畫開始的點離當前View X座標上的差值;//// float toXDelta, 這個參數表示動畫結束的點離當前View X座標上的差值;//// 如果view在A(x,y)點 那麼動畫就是從B點(x+fromXDelta, y+fromYDelta)點移動到C//// 點(x+toXDelta,y+toYDelta)點.////ani.setDuration(300);//ani.dstX = switch_to_left;//ani.dstY = y;//// ani.setFillBefore(true);//ani.view = image1;//////這個使view最後停留在距離左邊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 void 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);}});//長按mView可以擷取它的cache並移動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) {mLastMotionX = 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 MyTranslateAnimation(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;//本身這個view先置為invisibleview.setVisibility(View.INVISIBLE);Rect r = new Rect();r.set(view.getScrollX(), view.getScrollY(), 0, 0);//把view組件的座標轉化為同一座標繫上offsetDescendantRectToMyCoords(view, r);//mTouchOffsetX為手指相對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.setDrawingCacheBackgroundColor(0);//如果有cache則先destroyif (color != 0) {view.destroyDrawingCache();}//buildDrawingCache後才可以用getDrawingCache取到view的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);//這裡destroy這個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 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);}@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>//這個方法才能時擷取的cache在螢幕上隨著手指移動</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>//這個方法傳入Bitmap</strong>mDragBitmap = bitmap;mOffsetX = offsetx;mOffsetY = offsety;mDragPaint = null;invalidate();}}



代碼:http://download.csdn.net/detail/baidu_nod/7759965




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.