我检查参考以获得屏幕高度和宽度。我的手机的高度和宽度如下图
Point p = new Point();
getWindowManager().getDefaultDisplay().getSize(p);
p.x //width 540
p.y //height 960
我写了一个demo,但是得到的值不对 像这样的demo
import android.graphics.Point;
public class Strike {
private StrikeView view;
private int left = 0;
private int top = 0;
private Point win;
private boolean isDown = true;
private boolean isRight = true;
public Strike(StrikeView view, Point win) {
this.view = view;
this.win = win;
this.left = view.getLeft();
this.top = view.getTop();
}
/**
* Set direction and refresh
*/
public void setPostion() {
view.setLeft(left);
view.setTop(top);
view.invalidate();
}
/**
* calculate view direction
*/
public void postion() {
if (isRight) {
if (left + 30 < win.x)
left++;
else {
isRight = false;
left--;
}
} else {
if (left > 0)
left--;
else {
isRight = true;
left++;
}
}
if (isDown) {
if (top + 30 < win.y)
top++;
else {
isDown = false;
top--;
}
} else {
if (top > 0)
top--;
else {
isDown = true;
top++;
}
}
}
}