Android滑動繪製座標和view的移動
阿新 • • 發佈:2019-02-08
1.Android座標系 ,原點位於螢幕的左上角.
2.檢視座標系,原點位於父檢視的左上角.
3.
1. layout(左,上,右,下)
public class CustomView extends View { private int lastX; private int lastY; public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } publicCustomView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomView(Context context) { super(context); } public boolean onTouchEvent(MotionEvent event) { //獲取到手指處的橫座標和縱座標 int x = (int) event.getX(); int y = (int) event.getY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = x; lastY = y; break; case MotionEvent.ACTION_MOVE: //計算移動的距離 int offsetX = x - lastX; int offsetY = y - lastY; //呼叫layout方法來重新放置它的位置layout(getLeft()+offsetX, getTop()+offsetY, getRight()+offsetX , getBottom()+offsetY); break; } return true; } }
2. offsetLeftAndRight()和offsetTopAndBottom()
public class CustomView extends View { private int lastX; private int lastY; public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomView(Context context) { super(context); } public boolean onTouchEvent(MotionEvent event) { //獲取到手指處的橫座標和縱座標 int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = x; lastY = y; break; case MotionEvent.ACTION_MOVE: //計算移動的距離 int offsetX = x - lastX; int offsetY = y - lastY; //對left和right進行偏移 offsetLeftAndRight(offsetX); //對top和bottom進行偏移 offsetTopAndBottom(offsetY); break; } return true; } }3. layoutParams.leftMargin 和 layoutParams.topMargin
public class CustomView extends View { private int lastX; private int lastY; public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomView(Context context) { super(context); } public boolean onTouchEvent(MotionEvent event) { //獲取到手指處的橫座標和縱座標 int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = x; lastY = y; break; case MotionEvent.ACTION_MOVE: //計算移動的距離 int offsetX = x - lastX; int offsetY = y - lastY; LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams(); layoutParams.leftMargin = getLeft() + offsetX; layoutParams.topMargin = getTop() + offsetY; setLayoutParams(layoutParams); break; } return true; } }
4.給view設定動畫
<1.
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="300" android:duration="1000"/> </set>
mCustomView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.translate));
<2.屬性動畫
ObjectAnimator.ofFloat(mCustomView,"translationX",0,300).setDuration(1000).start();
5.scollTo與scollBy轉載:http://blog.csdn.net/itachi85/article/details/50724558