android view设置坐标,android关于View的坐标

GestureActivity.java

public class GestureActivity extends Activity {

private GestureDetector gestureDetector;

private Screen screen;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

gestureDetector = new GestureDetector(this,onGestureListener);

//得到屏幕的大小

screen = GestureUtils.getScreenPix(this);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

return gestureDetector.onTouchEvent(event);

}

GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

float x = e2.getX() - e1.getX();

float y = e2.getY() - e1.getY();

//限制必须得划过屏幕的1/3才能算划过

float x_limit = screen.widthPixels / 3;

float y_limit = screen.heightPixels / 3;

float x_abs = Math.abs(x);

float y_abs = Math.abs(y);

if(x_abs >= y_abs){

//gesture left or right

if(x > x_limit || x < -x_limit){

if(x>0){

//right

show("right");

}else if(x

//left

show("left");

}

}

}else{

//gesture down or up

if(y > y_limit || y < -y_limit){

if(y>0){

//down

show("down");

}else if(y

//up

show("up");

}

}

}

return true;

}

};

private void show(String value){

Toast.makeText(this, value, Toast.LENGTH_SHORT).show();

}

}

GestureUtils.java

public class GestureUtils {

//获取屏幕的大小

public static Screen getScreenPix(Context context) {

DisplayMetrics dm = new DisplayMetrics();

WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

windowManager.getDefaultDisplay().getMetrics(dm);

return new Screen(dm.widthPixels,dm.heightPixels);

}

public static class Screen{

public int widthPixels;

public int heightPixels;

public Screen(){

}

public Screen(int widthPixels,int heightPixels){

this.widthPixels=widthPixels;

this.heightPixels=heightPixels;

}

@Override

public String toString() {

return "("+widthPixels+","+heightPixels+")";

}

}

}

来源:oschina

链接:https://my.oschina.net/u/1775143/blog/266281

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值