我正在尝试为水平列表项关闭设置动画 alpha 动画有效,layoutparam 值也随时间减少 但出于某种原因,这并没有改变列表项的实际高度。
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final int initialHeight = view.getMeasuredHeight();
if(interpolatedTime == 1){
imageAdapter.remove(view, position);
}else{
view.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
view.setAlpha(1 - interpolatedTime);
System.out.println(" v height = " + view.getHeight());
System.out.println(" v layoutparams = " + view.getLayoutParams().height);
view.forceLayout();
view.invalidate();
view.requestLayout();
}
}
这是在 imadeAdapter 的 getView 方法中 binBtn 是您触摸以删除列表项的按钮
binBtn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_UP)
{
View vp = (View) v.getParent();
SqueezeAnimation ani = new SqueezeAnimation(this,vp,position);
ani.setDuration(1000);
v.startAnimation(ani);
在 Tanis 的回答后更改为此代码,但仍然是同样的问题, getHeight() 打印 674 和 getLayoutParams().height 打印 100
else{
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(100, 100);
view.setLayoutParams(lps);
view.requestLayout();
System.out.println(" v height = " + view.getHeight());
System.out.println(" v layoutparams = " + view.getLayoutParams().height);
作为旁注,这是在 imageadapter 的 getView 方法中,它会更改该 View 的高度,我尝试传入 backgroundfillIV 以进行动画处理,但仍然无效
View backgroundfillIV = gridView.findViewById(R.id.backroundfillbar);
double mult = products.get(position).value/ products.get(0).value;
backgroundfillIV.getLayoutParams().height = (int) ( (((bottleheight+20)*0.95)/mult));
最佳答案
您需要在某个时候调用 setLayoutParams()
以保存对 LayoutParams 的更改。
例如:
LayoutParams lps = view.getLayoutParams();
lps.height = initialHeight - (int)(initialHeight * interpolatedTime);
view.setAlpha(1 - interpolatedTime);
view.setLayoutParams(lps);
这是因为 getLayoutParams()
方法只返回 LayoutParams 引用的副本,而不是对 LayoutParams 本身的引用。在 Does Java return by reference or value 上对此有更深入的讨论。 .
关于android - layoutParams 不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17747123/