我想使视图缓慢滚动到移动页面,首先是这样的:
然后这样:
具有红色背景的视图缓慢滚动到移动页面。
这是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);
}
}
似乎很好。但是在红色视图滚动之后,底部仍有一些空间。页面不会自动更改其高度
答案 0 :(得分:0)
最后,我知道如何解决这个问题。我编辑更改代码以更改视图的translationY以更改视图的上边距。这是更改后的代码:
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);
}
}