медленно прокручивать страницу - PullRequest
0 голосов
/ 26 февраля 2019

Я хочу, чтобы представление медленно прокручивалось на мобильную страницу, сначала вот так:

потом вот так:

представление с красным фоном медленно прокручивается на мобильную страницу.Вот файл xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv_clean_complete"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_marginTop="80dp"
            android:src="@mipmap/img_clean_completed_ok"
            android:layout_centerHorizontal="true"/>

        <RelativeLayout
            android:id="@+id/layout_btm"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="288dp">

            <TextView
                android:text="Hello World"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>

        </RelativeLayout>

    </RelativeLayout>

</ScrollView>

Вот код:

    public class MainActivity extends AppCompatActivity {

    private RelativeLayout layoutBtm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layoutBtm = findViewById(R.id.layout_btm);
        int topMargin = getHeightPixels();
        ViewGroup.MarginLayoutParams marginLayoutParams =
                new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 288));
        marginLayoutParams.topMargin = topMargin;
        RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
        layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutBtm.setLayoutParams(layoutParams2);

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(layoutBtm,  "translationY", -dip2px(this, 288));
        objectAnimator.setDuration(1000);
        objectAnimator.start();
    }

    private int getHeightPixels(){
        DisplayMetrics dm = new DisplayMetrics();
        dm = getResources().getDisplayMetrics();
        return dm.heightPixels;
    }

    private int getStatusBarHeight(Activity activity){
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            Field field = clazz.getField("status_bar_height");
            int dpHeight = Integer.parseInt(field.get(object).toString());
            return activity.getResources().getDimensionPixelSize(dpHeight);
        } catch (Exception e) {
            return 0;
        }
    }

    private int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}

Кажется, все в порядке.Но после прокрутки красного представления внизу остается некоторое пространство. Страница не меняет свою высоту автоматически

1 Ответ

0 голосов
/ 26 февраля 2019

Наконец-то я знаю, как решить эту проблему.Я изменяю код изменения перевода Y представления, чтобы изменить верхнее поле представления. Вот код после изменения:

public class MainActivity extends AppCompatActivity {

private RelativeLayout layoutBtm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layoutBtm = findViewById(R.id.layout_btm);
    int topMargin = getHeightPixels();
    ViewGroup.MarginLayoutParams marginLayoutParams =
            new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(this, 288));
    marginLayoutParams.topMargin = topMargin;
    RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
    layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layoutBtm.setLayoutParams(layoutParams2);


    ValueAnimator animator = ValueAnimator.ofInt(getHeightPixels(), getHeightPixels() - getStatusBarHeight(this) -dip2px(this, 288));
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animator) {

            int currentValue = (int) animator.getAnimatedValue();

            ViewGroup.MarginLayoutParams marginLayoutParams =
                    new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip2px(MainActivity.this, 288));
            marginLayoutParams.topMargin = currentValue;
            RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(marginLayoutParams);
            layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            layoutBtm.setLayoutParams(layoutParams2);

            layoutBtm.requestLayout();

        }
    });
    animator.setDuration(1000);
    animator.start();
}

private int getHeightPixels(){
    DisplayMetrics dm = new DisplayMetrics();
    dm = getResources().getDisplayMetrics();
    return dm.heightPixels;
}

private int getStatusBarHeight(Activity activity){
    try {
        Class<?> clazz = Class.forName("com.android.internal.R$dimen");
        Object object = clazz.newInstance();
        Field field = clazz.getField("status_bar_height");
        int dpHeight = Integer.parseInt(field.get(object).toString());
        return activity.getResources().getDimensionPixelSize(dpHeight);
    } catch (Exception e) {
        return 0;
    }
}

private int dip2px(Context context, float dipValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dipValue * scale + 0.5f);
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...