View 的滑动
一般通过 三种方式可以实现View 的滑动:1、通过View本身提供的scrollTo/scrollBy方法 2、通过动画给View施加平移效果来实现 3、通过改变View的LayoutParams使View重新布局来实现。
- scrollTo/scrollBy
(1) scrollTo
(2) scrollBy
(3)onScrollChanged
scrollTo和scrollBy代码示例
public class MainActivity extends AppCompatActivity {
private LinearLayout layout;
private Button scrollToBtn;
private Button scrollByBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.layout);
scrollToBtn = findViewById(R.id.scroll_to_btn);
scrollByBtn = findViewById(R.id.scroll_by_btn);
scrollToBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* Set the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the x position to scroll to
* @param y the y position to scroll to
* 让View相对于初始的位置滚动某段距离
*
* public void scrollTo(int x, int y) {
* if (mScrollX != x || mScrollY != y) {
* int oldX = mScrollX;
* int oldY = mScrollY;
* mScrollX = x;
* mScrollY = y;
* invalidateParentCaches();
* onScrollChanged(mScrollX, mScrollY, oldX, oldY);
* if (!awakenScrollBars()) {
* postInvalidateOnAnimation();
* }
* }
* }
* mScrollX:其值等于view左边缘和view内容左边缘在水平方向的距离,单位是像素(view边缘是指view的位置)
* mScrollY: 其值等于view上边缘和view内容上边缘在竖直方向的距离,单位是像素(view内容边缘是指view中的内容的边缘)
* 当View没有使用scrollTo()和scrollBy()进行滑动的时候,mScrollX和mScrollY默认等于零
* view 左边缘在view内容左边缘右边时,mScrollX为正值,反之为负值。
* view 上边缘在view内容上边缘下边时,mScrollY为正值,反之为负值。
*/
layout.scrollTo(-60, -100);
}
});
scrollByBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/**
* Move the scrolled position of your view. This will cause a call to
* {@link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the amount of pixels to scroll by horizontally
* @param y the amount of pixels to scroll by vertically
* 让View相对于当前的位置滚动某段距离
*
* public void scrollBy(int x, int y) {
* scrollTo(mScrollX + x, mScrollY + y);
* }
*/
layout.scrollBy(-60, -100);
}
});
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
tools:context=".MainActivity">
<Button
android:id="@+id/scroll_to_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scrollTo"/>
<Button
android:id="@+id/scroll_by_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scrollBy"/>
</LinearLayout>
scrollTo和scrollBy变换示意图(view内容右下移动)
- 使用动画
可以使用视图动画或者属性动画来移动View。使用属性动画时,为了能够兼容3.0以下的版本,需要采用开源动画库https://github.com/JakeWharton/NineOldAndroids/
动画代码示例:
public class MainActivity extends AppCompatActivity {
private ImageView ivTransLate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivTransLate=findViewById(R.id.iv_trans);
Animation anim = new AnimationUtils().loadAnimation(this, R.anim.image_act_enlarge);
ivTransLate.startAnimation(anim);
anim.start();
ObjectAnimator.ofFloat(ivTransLate,"translationX",0,500).setDuration(1000).start();
ivTransLate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"点击了",Toast.LENGTH_SHORT).show();
}
});
}
}
anim 文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:zAdjustment="normal" android:fillAfter="true" >
<translate
android:duration="5000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="300"
android:toYDelta="300"/>
</set>
- 改变布局参数
可以通过重新设置一个View的LayoutParams来实现View的滑动
代码体现
public class MainActivity extends AppCompatActivity {
private ImageView ivTransLate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivTransLate=findViewById(R.id.iv_trans);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)ivTransLate.getLayoutParams();
params.leftMargin+=100;
//ivTransLate.requestLayout();
ivTransLate.setLayoutParams(params);
ivTransLate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"点击了", Toast.LENGTH_SHORT).show();
}
});
}
}
- 各种View 滑动方式对比
1 、scrollTo/scrollBy :操作简单,适合对View内容的滑动
2、动画:操作简单,主要适用于没有交互的View和实现复杂的动画效果
3、改变布局参数:操作稍微复杂,适用于有交互的View
使用示例:简单可拖动View
public class DragView extends ImageView {
private int mLastX;
private int mLastY;
public DragView(Context context) {
super(context);
}
public DragView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int rawY=(int)event.getRawY();
int rawX=(int)event.getRawX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int deltaX=rawX-mLastX;
int deltaY=rawY-mLastY;
// int translationX=(int)ViewHelper.getTranslationX(this)+deltaX;
// int translationY=(int)ViewHelper.getTranslationY(this)+deltaY;
// ViewHelper.setTranslationX(this,translationX);
// ViewHelper.setTranslationY(this,translationY);
setTranslationX(deltaX+getX());
setTranslationY(deltaY+getY());
break;
case MotionEvent.ACTION_UP:
break;
default:
break;
}
mLastX=rawX;
mLastY=rawY;
return true;
}
}
参考:1、 《Android 开发艺术探索》 任玉刚
下一篇:View的弹性滑动:https://blog.csdn.net/lwj_hewo/article/details/90176429