android 拖动图片拖动浮动按钮.docx
- 文档编号:2991766
- 上传时间:2022-11-16
- 格式:DOCX
- 页数:14
- 大小:27.12KB
android 拖动图片拖动浮动按钮.docx
《android 拖动图片拖动浮动按钮.docx》由会员分享,可在线阅读,更多相关《android 拖动图片拖动浮动按钮.docx(14页珍藏版)》请在冰豆网上搜索。
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- android 拖动图片拖动浮动按钮 拖动 图片 浮动 按钮
![提示](https://static.bdocx.com/images/bang_tan.gif)