Anu*_*nuj 5 animation android android-linearlayout
我制作了一个应用程序,在其中单击按钮时,我滑动了一个线性布局。滑动线性布局的代码工作正常,但问题是滑动时滞后。此外,我设置的滑动布局的高度看起来并不适用于所有屏幕尺寸(特别是在 Nexus 5 上)。请帮帮我
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_upload_add"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/add" />
<LinearLayout
android:id="@+id/ll_upload_options"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/choosefile_bg"
android:gravity="center"
android:orientation="vertical">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="@+id/tv_upload_gallery"
style="@style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="5dp"
android:text="GALLERY"
android:textSize="20sp"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@drawable/line" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="@+id/tv_upload_camera"
style="@style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="5dp"
android:text="CAMERA"
android:textSize="20sp"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@drawable/line" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
android:id="@+id/tv_upload_file"
style="@style/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="5dp"
android:text="FILE"
android:textSize="20sp"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/all_left_mar"
android:layout_marginRight="@dimen/all_right_mar"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.EditTextFont
style="@style/edit_text"
android:drawableBottom="@drawable/line_black"
android:hint="File Title"
android:textColor="#000"
android:textColorHint="#000" />
<AutoCompleteTextView
style="@style/edit_text"
android:layout_marginTop="10dp"
android:completionThreshold="1"
android:drawableBottom="@drawable/line_black"
android:hint="Type"
android:textColor="#000"
android:textColorHint="#000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Switch
android:id="@+id/switch1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:textOff="No"
android:textOn="Yes" />
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.TextViewGeneral
style="@style/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="@string/Protect"
android:textColor="#000" />
</LinearLayout>
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.EditTextFont
style="@style/edit_text"
android:layout_marginTop="10dp"
android:drawableBottom="@drawable/line_black"
android:hint="Password"
android:inputType="numberPassword"
android:textColor="#000"
android:textColorHint="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/all_left_mar"
android:layout_marginRight="@dimen/all_right_mar"
android:layout_weight="1">
<pocketdocs.indiehustlers.com.pocketdocsv2.Utils.ButtonFont
style="@style/button"
android:text="UPLOAD" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
代码
if (llUploadOptions.getMeasuredHeight() != 0) {
// tvGallery.setVisibility(View.GONE);
// tvCamera.setVisibility(View.GONE);
// tvFile.setVisibility(View.GONE);
ValueAnimator anim = ValueAnimator.ofInt(llUploadOptions.getMeasuredHeight(),200);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llUploadOptions.getLayoutParams();
layoutParams.height = val;
llUploadOptions.setLayoutParams(layoutParams);
}
});
anim.setDuration(700);
anim.start();
} else {
ValueAnimator anim = ValueAnimator.ofInt(llUploadOptions.getMeasuredHeight(),250);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = llUploadOptions.getLayoutParams();
layoutParams.height = val;
llUploadOptions.setLayoutParams(layoutParams);
}
});
anim.setDuration(700);
anim.start();
}
Run Code Online (Sandbox Code Playgroud)
问题是,您正在尝试为布局的高度设置动画。这意味着,在您调用 setLayoutParams() 的每个动画步骤中,这会强制您的整个布局(包括子项)通过完整的视图层次结构进行重新布局,这会导致动画滞后。布局东西是一项昂贵的操作!
有一些“解决方法”(我不确定你想做什么):
您正在使用Animators(not Animations),这可能会很滞后,尤其是在移动小部件时。尝试翻译动画
TranslateAnimation animation = new TranslateAnimation(0.0f, 200.0f,
0.0f, 0.0f);
// these are delta's, check doc for this constructor
// TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
animation.setDuration(700);
llUploadOptions.startAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
如果您想保留动画视图的最终坐标,请使用
animation.setFillAfter(true);
Run Code Online (Sandbox Code Playgroud)
但它可能无法点击或可能出现其他问题。对于这些你可能会使用
animation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation anim)
{
};
public void onAnimationRepeat(Animation anim)
{
};
public void onAnimationEnd(Animation anim)
{
//set fixed position in here, using LayoutParams or setTop/setRight etc. methods of View (API 11)
};
});
Run Code Online (Sandbox Code Playgroud)
编辑:
所以你可以像这样而不是 ValueAnimator
int desiredHeightInPx = getResources().getDimensionPixelSize(R.dimen.expandedHeight);
//note those are pixels, not dp. you might set this as =200 or =250 like you have
ScaleAnimation animation = new ScaleAnimation(0, 0, 0, desiredHeightInPx/llUploadOptions.getMeasuredHeight(), Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(1000); //ms
llUploadOptions.startAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
接下来的步骤如上所述,fillAfter或者设置AnimationListener并在内部onAnimationEnd设置最终的 LayoutParams。
请注意,这ScaleAnimation是缩放,它可能更适合使用RelativeLayout而不是线性(它重新缩放其子项)
| 归档时间: |
|
| 查看次数: |
11001 次 |
| 最近记录: |