如何实现多个Button的移动? android按钮移动 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将多个button放到layout里,实现layout的移动 声明一个List<View> 集合,然后findViewById()以后把需要移动的控件放进去,然后在你拖动时候遍历就可以了 public class MainActivity extends Activity { private ImageView i; private int width,height,defaultx,defaulty; private float mx,my; private int intscreenx,intscreeny; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayMetrics dm=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); intscreenx=dm.widthPixels; intscreeny=dm.heightPixels; width=100; height=100; i=(ImageView) findViewById(R.id.imageView1); i.setImageResource(R.drawable.ic_launcher); RestoreButton(); } @Override public boolean onTouchEvent(MotionEvent event){ float x=event.getX(); float y=event.getY(); try{ switch(event.getAction()){ case MotionEvent.ACTION_DOWN: picMove(x,y); break; case MotionEvent.ACTION_MOVE: picMove(x,y); break; case MotionEvent.ACTION_UP: picMove(x,y); break; } } catch(Exception e){ e.printStackTrace(); } return true; } private void picMove(float x,float y){ mx=x-(width/2); my=y-(height/2); if((mx+width)>intscreenx){ mx=intscreenx-width; } else if(mx<0){ mx=0; } else if((my+height)>intscreeny){ my=intscreeny-height; } else if(my<0){ my=0; } i.setLayoutParams(new AbsoluteLayout.LayoutParams(width,height,(int)(mx),(int)(my))); } public void RestoreButton(){ defaultx=((intscreenx-width)/2); defaulty=((intscreeny-height)/2); i.setLayoutParams(new AbsoluteLayout.LayoutParams(width,height,defaultx,defaulty)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}不要说是我告诉你的,记得给分就行了,,, linearlayout 点击变色有问。求大侠解决 求一个等待对话框loading 具体说明前看内容 Android ListView中元素添加弹出Dialog确认框 新手求救 怎样制作地图 Android的GPS应用开发问题,请教大牛,非常感谢~ 求最简单制作APP的方法(嵌入网站形式) 求帮助~~有谁使用过百度的API来定位显示当前的城市呢? 请教一个关于即时通讯的问题 新人学习遇到问题求助,有关于IO流读取数据的(应该是吧,新人不太确定) 三星 4.3系统上的Gsensor开发 帮我看看这个编译出错
private ImageView i;
private int width,height,defaultx,defaulty;
private float mx,my;
private int intscreenx,intscreeny;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
intscreenx=dm.widthPixels;
intscreeny=dm.heightPixels;
width=100;
height=100;
i=(ImageView) findViewById(R.id.imageView1);
i.setImageResource(R.drawable.ic_launcher);
RestoreButton();
}
@Override
public boolean onTouchEvent(MotionEvent event){
float x=event.getX();
float y=event.getY();
try{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
picMove(x,y);
break;
case MotionEvent.ACTION_MOVE:
picMove(x,y);
break;
case MotionEvent.ACTION_UP:
picMove(x,y);
break;
}
}
catch(Exception e){
e.printStackTrace();
}
return true;
}
private void picMove(float x,float y){
mx=x-(width/2);
my=y-(height/2);
if((mx+width)>intscreenx){
mx=intscreenx-width;
}
else if(mx<0){
mx=0;
}
else if((my+height)>intscreeny){
my=intscreeny-height;
}
else if(my<0){
my=0;
}
i.setLayoutParams(new AbsoluteLayout.LayoutParams(width,height,(int)(mx),(int)(my)));
} public void RestoreButton(){
defaultx=((intscreenx-width)/2);
defaulty=((intscreeny-height)/2);
i.setLayoutParams(new AbsoluteLayout.LayoutParams(width,height,defaultx,defaulty));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
不要说是我告诉你的,记得给分就行了,,,