layoutParams不变

时间:2013-07-19 13:18:27

标签: android android-layout android-animation

我正在尝试设置水平列表项目的动画 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()。高度打印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方法中,它改变了该视图的高度,并且我已经尝试将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)); 

2 个答案:

答案 0 :(得分:6)

您需要在某个时候致电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进行了更深入的讨论。

答案 1 :(得分:0)

不是真正的解决方案,但问题在于我找到的Horizo​​ntalListView,因为它适用于普通的列表视图