android 拖动图片拖动浮动按钮.docx
《android 拖动图片拖动浮动按钮.docx》由会员分享,可在线阅读,更多相关《android 拖动图片拖动浮动按钮.docx(14页珍藏版)》请在冰豆网上搜索。
![android 拖动图片拖动浮动按钮.docx](https://file1.bdocx.com/fileroot1/2022-11/16/c62f8b23-706b-43f5-9bc8-fae396d77734/c62f8b23-706b-43f5-9bc8-fae396d777341.gif)
android拖动图片拖动浮动按钮
android拖动图片/拖动浮动按钮
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{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(Bundleicicle){
super.onCreate(icicle);
MyViewtx=newMyView(this);
tx.setText("DragMe");
AbsoluteLayoutl=newAbsoluteLayout(this);
AbsoluteLayout.LayoutParamsp=newAbsoluteLayout.LayoutParams(
AbsoluteLayout.LayoutParams.WRAP_CONTENT,
AbsoluteLayout.LayoutParams.WRAP_CONTENT,10,10);
l.addView(tx,p);
setContentView(l);
}
}
classMyViewextendsButton{
publicMyView(Contextc){
super(c);
}
@Override
publicbooleanonMotionEvent(MotionEventevent){
intaction=event.getAction();
intmCurX=(int)event.getX();
intmCurY=(int)event.getY();
if(action==MotionEvent.ACTION_MOVE){
//this.setText("x:
"+mCurX+",y:
"+mCurY);
AbsoluteLayout.LayoutParamsp=new
AbsoluteLayout.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");
}
returntrue;
}
@Override
publicvoiddraw(Canvascanvas){
//TODOAuto-generatedmethodstub
super.draw(canvas);
}
}
拖拽图片效果
方法一:
Java代码
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnTouchListener;
importandroid.widget.ImageView;
publicclassDragSample01extendsActivity{
ImageViewimg;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.drag_sample01);
img=(ImageView)findViewById(R.id.img_view);
img.setOnTouchListener(newOnTouchListener(){
privateintmx,my;
publicbooleanonTouch(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()/2);
break;
}
returntrue;
}});
}
}
方法二:
Java代码
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnTouchListener;
importandroid.widget.ImageView;
publicclassDragSample01extendsActivity{
ImageViewimg;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.drag_sample01);
img=(ImageView)findViewById(R.id.img_view);
img.setOnTouchListener(newOnTouchListener(){
privatefloatx,y;
privateintmx,my;
publicbooleanonTouch(Viewv,MotionEventevent){
switch(event.getAction()){
caseMotionEvent.ACTION_DOWN:
x=event.getX();
y=event.getY();
caseMotionEvent.ACTION_MOVE:
mx=(int)(event.getRawX()-x);
my=(int)(event.getRawY()-50-y);
v.layout(mx,my,mx+v.getWidth(),my+v.getHeight());
break;
}
returntrue;
}});
}
}
拖动按钮到处跑
1.布局文件
Xml代码
xmlversion="1.0"encoding="utf-8"?
>
android="
android:
orientation="vertical"android:
layout_width="fill_parent"
android:
layout_height="fill_parent">
id="@+id/btn"android:
layout_width="fill_parent"
android:
layout_height="wrap_content"android:
text="拖动看看~~"/>
2.代码
Java代码
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.DisplayMetrics;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnTouchListener;
importandroid.widget.Button;
publicclassDraftTestextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
finalButtonbtn=(Button)findViewById(R.id.btn);
btn.setOnTouchListener(newOnTouchListener(){
int[]temp=newint[]{0,0};
publicbooleanonTouch(Viewv,MotionEventevent){
inteventaction=event.getAction();
intx=(int)event.getRawX();
inty=(int)event.getRawY();
switch(eventaction){
caseMotionEvent.ACTION_DOWN:
//touchdownsocheckifthe
temp[0]=(int)event.getX();
temp[1]=y-v.getTop();
break;
caseMotionEvent.ACTION_MOVE:
//touchdragwiththeball
v.layout(x-temp[0],y-temp[1],x+v.getWidth()
-temp[0],y