盒子
盒子
文章目录
  1. View的滑动
    1. 使用scrollTo/scrollBy
    2. 使用动画
    3. 改变布局参数

探索自定义View

View的滑动

使用scrollTo/scrollBy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class WidgeTer extends android.support.v7.widget.AppCompatTextView {
private static final String TAG =WidgeTer.class.getSimpleName() ;
private Scroller scroller;
public WidgeTer(Context context) {
super(context);
}
public WidgeTer(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
scroller=new Scroller(context);
smoothScrollTo(-500,-500);
}
@Override
public void computeScroll() {
if(scroller.computeScrollOffset()){
scrollTo(scroller.getCurrX(),scroller.getCurrY());
Log.i(TAG,"CurrX:"+scroller.getCurrX()+"CurrY:"+scroller.getCurrY());
postInvalidate();
}
}
private void smoothScrollTo(int destX,int destY){
int scrollX=getScrollX();
int scrollY=getScrollY();
int deltaX=destX-scrollX;
int deltaY=destY-scrollY;
Log.i(TAG,"destX:"+destX+"scrollX:"+scrollX+" delta:"+deltaX);
scroller.startScroll(scrollX,scrollY,deltaX,deltaY,5000);
invalidate();
}
}

  • getScrollX
  • 返回向左滚动的视图,即滚动时的当前位置,右和下为负,左和上为正

    1
    2
    3
    4
    5
    6
    7
    <io.github.grooters.practicer.viewer.WidgeTer
    android:id="@+id/wid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintStart_toStartOf="parent"
    android:text="Test"
    android:textSize="20sp"/>
    1
    2
    3
    4
    5
    6
    7
    WidgeTer:  destX:-500 scrollX:0 delta:-500
    WidgeTer: CurrX:0 CurrY:0 ScrollX:0
    WidgeTer: CurrX:-1 CurrY:-1 ScrollX:-1
    WidgeTer: CurrX:-2 CurrY:-2 ScrollX:-2
    WidgeTer: CurrX:-8 CurrY:-8 ScrollX:-8
    WidgeTer: CurrX:-500 CurrY:-500 ScrollX:-500
    WidgeTer: CurrX:-500 CurrY:-500 ScrollX:-500

    达到弹性滑动除了通过Scroller实现外,还可以通过延时策略实现:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    myHandler=new MyHandler();
    myHandler.sendEmptyMessage(0);

    class MyHandler extends Handler{
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    count++;
    if(count<=30){
    float fraction=count/(float)30;
    Log.i(TAG,"fraction: "+fraction);
    widgeTer.scrollTo((int)(-fraction*100),0);
    myHandler.sendEmptyMessageDelayed(0,1000);
    }
    }
    }

    若想要移动跨度大点的话只需将30改小即可

    使用动画

  • View动画
  • Android Resource Directory -> Android Resource file

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?xml version="1.0" encoding="utf-8"?>
    <set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true">
    <translate
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="100"
    android:toYDelta="100"/>
    </set>
    1
    2
    3
    Animation animation=AnimationUtils.loadAnimation(this,R.anim.widgeter);
    button.setAnimation(animation);
    //button.startAnimation(animation);

    注意:

    View动画无法改变View真实属性,比如虽然View位置看上去移动了,但其实它仍在原处,甚至如果将下列属性设为false,View执行完动画后还会恢复View的位置

    1
    android:fillAfter="true"

  • 属性动画
  • 1
    ObjectAnimator.ofFloat(this,"translationX",0,500).setDuration(1000).start();

    改变布局参数

    1
    2
    3
    ViewGroup.MarginLayoutParams params=(ViewGroup.MarginLayoutParams) getLayoutParams();
    params.leftMargin+=100;
    requestLayout();

    详见:动态操作布局

    支持一下
    扫一扫,支持Grooter
    • 微信扫一扫
    • 支付宝扫一扫