先上图看效果
实质上说是动画有点不妥,确切的说应该是手势的处理,废话不多说看代码
SimpleDragSample.java
public class SimpleDragSample extends Activity { protected int contentTop; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.layout_simpledrag) ; DisplayMetrics dm = getResources().getDisplayMetrics() ; final int screenWidth = dm.widthPixels ; final int screenHeight = dm.heightPixels ; Log.e("width,height", screenWidth + "," + screenHeight); final Button _Button = (Button)findViewById(R.id.buttonDrag) ; _Button.setOnTouchListener(new OnTouchListener() { int lastX , lastY ; boolean isDraging = false ; @Override public boolean onTouch(View v, MotionEvent event) { int ea = event.getAction() ; switch (ea) { case MotionEvent.ACTION_DOWN: isDraging = true ; lastX = (int) event.getRawX() ; lastY = (int) event.getRawY() ; if(contentTop == 0) { contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();//获取状态栏和标题栏的总高度 } break; case MotionEvent.ACTION_MOVE: if(isDraging) { int dx = (int)event.getRawX() - lastX ; int dy = (int)event.getRawY() - lastY ; int l = v.getLeft() + dx ; int b = v.getBottom() + dy ; int r = v.getRight() + dx ; int t = v.getTop() + dy ; //判断超出屏幕 if(l<0) { l = 0 ; r = v.getWidth() ; } if(t<0) { t = 0 ; b = v.getHeight() ; } if(r>screenWidth) { r = screenWidth ; l = r - v.getWidth() ; } if(b>screenHeight - contentTop) { b = screenHeight - contentTop ; t = b - v.getHeight() ; } v.layout(l, t, r, b) ; lastX = (int) event.getRawX() ; lastY = (int) event.getRawY() ; v.postInvalidate() ; } break ; case MotionEvent.ACTION_UP: isDraging = false ; break ; default: break; } return false; } }) ; } }
layout_simpledrag.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/buttonDrag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" > </Button> </RelativeLayout>
代码相对来说不是太复杂,这里就不多加解释了,只说下开发过程中遇到的问题
1.view.getLeft、view.getRight、view.getTop、view.getBottom都是相对父view的位置,这里button的父view为我们定义的RelativeLayout,是不包括顶部状态栏和标题栏的。
这篇文章讲解的很详细
http://blog.csdn.net/androiddevelop/article/details/8373782
2.用以下代码测试我的小米手机(480*854),获取的值为320*569
DisplayMetrics dm = getResources().getDisplayMetrics() ; final int screenWidth = dm.widthPixels ; final int screenHeight = dm.heightPixels ;
解决办法:
注: density
不等于1的情况下,需要设置targetSdkVersion在4-9之间,例如
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" />
需要在工程的AndroidManifest.xml文件中,加入supports-screens节点,具体的内容如下:
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:resizeable="true" android:anyDensity="true" />
研究之后发现,若没有设定多分辨率支持的话,Android系统会将240x320的低密度(120)和800x480以上的的高密度尺寸转换为中等密度(160)对应的尺寸
3.三种方式获取屏幕分辨率
(1)直接 int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); (2)通过getResources()获取DisplayMetrics对象, DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; (3)通过getWindowManager()获取 dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels;
android动画-拖动
时间: 2024-10-01 04:01:21