資源描述:
《android 拖動(dòng)圖片拖動(dòng)浮動(dòng)按鈕》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、android拖動(dòng)圖片/拖動(dòng)浮動(dòng)按鈕Java代碼importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.os.Bundle;importandroid.view.MotionEvent;importandroid.widget.AbsoluteLayout;importandroid.widget.Button;publicclassDrag_And_DropextendsActivity{/**Calledwhentheactivityis
2、firstcreated.*/@OverridepublicvoidonCreate(Bundleicicle){super.onCreate(icicle);MyViewtx=newMyView(this);tx.setText("DragMe");AbsoluteLayoutl=newAbsoluteLayout(this);AbsoluteLayout.LayoutParamsp=newAbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.W
3、RAP_CONTENT,10,10);l.addView(tx,p);setContentView(l);}}classMyViewextendsButton{publicMyView(Contextc){super(c);}@OverridepublicbooleanonMotionEvent(MotionEventevent){intaction=event.getAction();intmCurX=(int)event.getX();intmCurY=(int)event.getY();if(action==MotionEvent.ACTION_MOVE){//this.setT
4、ext("x:"+mCurX+",y:"+mCurY);AbsoluteLayout.LayoutParamsp=newAbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,this.mLeft+mCurX,this.mTop+mCurY);this.setLayoutParams(p);}if(action==MotionEvent.ACTION_UP){//this.setText("notmoving");}retu
5、rntrue;}@Overridepublicvoiddraw(Canvascanvas){//TODOAuto-generatedmethodstubsuper.draw(canvas);}}拖拽圖片效果方法一:Java代碼importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.View.OnTouchListener;importandroid.widget.ImageView;public
6、classDragSample01extendsActivity{ImageViewimg;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.drag_sample01);img=(ImageView)findViewById(R.id.img_view);img.setOnTouchListener(newOnTouchListener(){privateintmx,my;publicbooleanonTouc
7、h(Viewv,MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_MOVE:mx=(int)(event.getRawX());my=(int)(event.getRawY()-50);v.layout(mx-img.getWidth()/2,my-img.getHeight()/2,mx+img.getWidth()/2,my+img.getHeight()/