该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public class MainActivity extends Activity {
private ImageButton movebtn;//可拖动按钮
private boolean clickormove = true;//点击或拖动,点击为true,拖动为false
private int downX, downY;//按下时的X,Y坐标
private boolean hasMeasured = false;//ViewTree是否已被测量过,是为true,否为false
private View content;//界面的ViewTree
private int screenWidth,screenHeight;//ViewTree的宽和高
private int[] Left=new int[12];
private int[] Bottom=new int[12];
private int[] Top=new int[12];
private int[] Right=new int[12];
private int[] button={R.id.imageButton1,R.id.imageButton2,R.id.imageButton3,R.id.imageButton4,R.id.imageButton5,R.id.imageButton6,
R.id.imageButton7,R.id.imageButton8,R.id.imageButton9,R.id.imageButton10,R.id.imageButton11,R.id.imageButton12};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = getWindow().findViewById(Window.ID_ANDROID_CONTENT);//获取界面的ViewTree根节点View
DisplayMetrics dm = getResources().getDisplayMetrics();//获取显示屏属性
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
ViewTreeObserver vto = content.getViewTreeObserver();//获取ViewTree的监听器
vto.addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// TODO Auto-generated method stub
if(!hasMeasured)
{
screenHeight = content.getMeasuredHeight();//获取ViewTree的高度
hasMeasured = true;//设置为true,使其不再被测量。
}
return true;//如果返回false,界面将为空。
}
});
for(int i=0;i
movebtn=(ImageButton)findViewById(button[i]);
LinearLayout.LayoutParams linearParams =new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linearParams.width=dm.widthPixels/3;
linearParams.height=dm.widthPixels/3;
movebtn.setLayoutParams(linearParams);
aaa(movebtn,i);
Log.d("aa",Integer.toString(Left[i]));
}
Button but=(Button)findViewById(R.id.button1);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
for(int i=0;i
movebtn=(ImageButton)findViewById(button[i]);
Top[i]=movebtn.getTop();
Bottom[i]=movebtn.getBottom();
Left[i]=movebtn.getLeft();
Right[i]=movebtn.getRight();
}
}
});
}
void aaa(final ImageButton btn,final int id){
btn.setOnTouchListener(new OnTouchListener() {//设置按钮被触摸的时间
int lastX, lastY; // 记录移动的最后的位置
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int ea = event.getAction();//获取事件类型
switch (ea) {
case MotionEvent.ACTION_DOWN: // 按下事件
btn.bringToFront();
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
downX = lastX;
downY = lastY;
break;
case MotionEvent.ACTION_MOVE: // 拖动事件
// 移动中动态设置位置
int dx = (int) event.getRawX() - lastX;//位移量X
int dy = (int) event.getRawY() - lastY;//位移量Y
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
//++限定按钮被拖动的范围
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
//--限定按钮被拖动的范围
v.layout(left, top, right, bottom);//按钮重画
// 记录当前的位置
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP: // 弹起事件
int rx=(int)event.getRawX()-downX;
int ry=(int)event.getRawY()-downY;
if (Math.abs(rx) > 5|| Math.abs(ry) > 5){
clickormove = false;
int x=Left[id]+(Right[id]-Left[id])/2;
int y=Top[id]+(Bottom[id]-Top[id])/2;
for(int i=0;i
if(i!=id&&rx+x>Left[i]&&rx+xTop[i]&&ry+y
v.layout(Left[i], Top[i], Right[i], Bottom[i]);
ImageButton ib=(ImageButton)findViewById(button[i]);
ib.layout(Left[id], Top[id], Right[id], Bottom[id]);
jx(i,id);
break;
}
if(i==button.length-1){
v.layout(Left[id], Top[id], Right[id], Bottom[id]);
Log.d("iidd", Integer.toString(id));
Log.d("left",Integer.toString(Left[id]));
}
}
}
else
clickormove = true;
break;
}
return true;
}
});
}
private void jx(int a,int b){
int x=Left[a];
Left[a]=Left[b];
Left[b]=x;
x=Top[a];
Top[a]=Top[b];
Top[b]=x;
x=Right[a];
Right[a]=Right[b];
Right[b]=x;
x=Bottom[a];
Bottom[a]=Bottom[b];
Bottom[b]=x;
}
}