我已经制作了一个应用程序,其中按钮单击我滑动线性布局。滑动线性布局的代码工作正常,但问题是它滑动时滞后。还有我设置为滑动的高度布局在所有屏幕尺寸上看起来都不合适(特别是在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>
代码
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();
}
答案 0 :(得分:2)
您正在使用Animators
(不是Animations
),这可能会很滞后,特别是在移动小部件时。试试TranslateAnimation
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);
如果要将动画视图保留在目标坐标中,请使用
animation.setFillAfter(true);
但可能无法点击或可能出现其他问题。对于这些你可以使用
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)
};
});
编辑:
所以代替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);
上述后续步骤,fillAfter
或设置AnimationListener
以及onAnimationEnd
内设置最终的LayoutParams。
请注意ScaleAnimation
正在缩放,使用RelativeLayout
代替线性(重新调整其childers)可能更合适
答案 1 :(得分:2)
问题是,您正试图为布局的高度设置动画。这意味着,对于每个动画步骤,您调用setLayoutParams()会强制整个布局(包括子项)通过完整的视图层次结构进行转发,这会导致滞后动画。布置东西是一项昂贵的操作!
有一些&#34;变通办法&#34; (我不确定你想做什么):