0

我有类似这样的列表视图:在此处输入图像描述

添加代码,不多,但可能有帮助:

自定义适配器:

public class MyAdapter extends BaseAdapter{

private LayoutInflater mInflater;
private Animation fadeIn;
private Animation fadeOut;



public MyAdapter(Activity c) {
    mInflater = c.getLayoutInflater();
    fadeIn = AnimationUtils.loadAnimation(c.getApplicationContext(), R.anim.alpha_show);
    fadeIn.setFillAfter(true);
    fadeOut = AnimationUtils.loadAnimation(c.getApplicationContext(), R.anim.alpha_dissappear);
    fadeOut.setFillAfter(true);
}

public int getCount() {
    return MainButtonsList.getList().getSize() + 
            GlobalPrefs.getEmptyRowsAtEnd() + 
            GlobalPrefs.getEmptyRowsAtStart();
}


public View getView(int position, View v, ViewGroup parent) {
    View convertView = v;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.main_list_item, null);
    }
    ImageView iv = (ImageView) convertView.findViewById(R.id.image);
    if ((position > GlobalPrefs.getEmptyRowsAtStart() - 1) && (position < getCount() - GlobalPrefs.getEmptyRowsAtEnd())) {
        iv.setImageResource(MainButtonsList.getList().getListImageResource(position - GlobalPrefs.getEmptyRowsAtStart()));
        iv.setAlpha(255);
        iv.setTag(MainButtonsList.UNPRESSED_BUTTON_TAG);
    } else {
        iv.setTag(MainButtonsList.UNUSED_BUTTON_TAG);
        iv.setAlpha(0);
        iv.setVisibility(0);
        iv.setClickable(false);
        iv.setImageResource(R.drawable.logo_list_null);

    }
    iv.setMaxHeight(GlobalPrefs.getRowHeight());
    iv.setMaxWidth(GlobalPrefs.getRowWidth());
    iv.setBackgroundResource(0);





    return convertView;
}

public Object getItem(int position) {
    return MainButtonsList.getList().getObject(position);
}

public long getItemId(int position) {
    return position;
}

}

自定义列表视图:

public class CopyOfListView3d extends ListView{
private final Camera mCamera = new Camera();
private final Matrix mMatrix = new Matrix();
private Context context;
private Paint mPaint;


public CopyOfListView3d(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.setChildrenDrawingOrderEnabled(true);

}

@Override
protected int getChildDrawingOrder (int childCount, int i) {
      //sets order number to each child, so makes overlap and center is always on top
    }

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    // get top left coordinates
    boolean isCenter = false;
    final int top = child.getTop();
    final int bottom = child.getBottom();
    Bitmap bitmap = child.getDrawingCache();
    if (bitmap == null) {
        child.setDrawingCacheEnabled(true);
        child.buildDrawingCache();
        bitmap = child.getDrawingCache();
    }

    final int centerY = child.getHeight() / 2;
    final int centerX = child.getWidth() / 2;
    final int radius = getHeight() / 2;
    final int absParentCenterY = getTop() + getHeight() / 2;
    final int absChildCenterY = child.getTop() + centerY;
    final int distanceY = (absParentCenterY - absChildCenterY) / 2;
    final int absDistance = Math.min(radius, Math.abs(distanceY));

    final float translateZ = (float) Math.sqrt((radius * radius) - (absDistance * absDistance));
    mCamera.save();
    float myTranslateX = (float) (translateZ * (1.5f));

    int density = GlobalPrefs.getDensity();
    if (density < DisplayMetrics.DENSITY_LOW) {
        myTranslateX = (float) myTranslateX - 80;
    } else if (density == DisplayMetrics.DENSITY_LOW) {
        myTranslateX = (float) myTranslateX - GlobalPrefs.getScreenWidth() + density + 40;
    } else if (density <= DisplayMetrics.DENSITY_MEDIUM) {
            myTranslateX = (float) myTranslateX - ((float)(GlobalPrefs.getScreenWidth()*0.75)) + density/2;
    } else
        if (density <= DisplayMetrics.DENSITY_HIGH) {
            myTranslateX = (float) myTranslateX - 320;
        } else
            if (density > DisplayMetrics.DENSITY_HIGH) {
                //Log.i("density", "this is more than high");
                myTranslateX = (float) myTranslateX;
            }
    if ((top < absParentCenterY) && (bottom > absParentCenterY)) {
        //make center row bigger
        isCenter = true;
        mCamera.translate((float) myTranslateX, 0, (float) -160);//130
        child.setPressed(true);
        child.setTag(MainButtonsList.PRESSED_BUTTON_TAG);
    }  
    else {
        //top
        child.setTag(MainButtonsList.UNPRESSED_BUTTON_TAG);
        child.setPressed(false);
        mCamera.translate((float) myTranslateX, 0, -150);//120;
    };
    mCamera.getMatrix(mMatrix);
    mCamera.restore();

    // create and initialize the paint object
    if (mPaint == null) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
    }
    //calculates alpha for each row, so far from center, rows are barely visible
    mPaint.setAlpha(calculateAlpha(absChildCenterY));

    mMatrix.postTranslate((float) (-centerX * 1.5f), top);
    canvas.drawBitmap(bitmap, mMatrix, mPaint);
    return false;
}



}

它们由 ImageViews 组成,其中白色为选中状态,蓝色为未选中状态,我通过屏幕坐标更改选中/未选中,因此始终选择中心。

我有自定义 3dListView,它扩展了 ListView 并制作了自定义适配器,我在其中将 imageviews 设置为行。代码太多了,但有很简单的

其中之一可能会有所帮助:

1)我怎样才能使中心行淡入(也许也淡出)(我可以在给父母或孩子计数时找到哪一行是中心)?(更好的)

2) 如果有任何 OnStateChange 监听器来监听视图的按下,它也可能会有所帮助。然后,当视图被按下和淡出时,我可以随时淡入,当松开按下时。

4

2 回答 2

4

我自己找到了答案。在 drawChild 方法结束时将此代码段添加到我的自定义 ListView 中:

final ImageView iv = (ImageView) child.findViewById(R.id.image);
            child.setTag(MainButtonsList.PRESSED_BUTTON_TAG);

            Animation anim = fadeOut();
            anim.setAnimationListener(new Animation.AnimationListener() {

                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub

                }

                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                public void onAnimationEnd(Animation animation) {
                    child.setPressed(true);
                    iv.startAnimation(fadeIn());
                }
            });

            iv.clearAnimation();
            iv.startAnimation(anim);

在列表项布局中,添加了另一个图像,它放在项下,现在看起来很漂亮。

于 2012-05-31T14:29:13.070 回答
3

您需要在列表视图的适配器中进行淡入淡出。

在您的适配器中

private List<YourListObject> deleteItems;

// Constructor to init list and other vars

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Draw stuff
    // including your object


    for(YourListObject obj : deleteItems){
            if(currentObj.equals(obj)){
                Animation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
                fadeOutAnimation.setDuration(300);
                DeleteAnimationListener listener = new DeleteAnimationListener(currentObj);
                fadeOutAnimation.setAnimationListener(listener);
                someView.startAnimation(fadeOutAnimation);
            }
        }

    return someView;
}


   public void remove(YourListObject obj){
        deleteItems.add(obj);
        notifyDataSetChanged();
    }

   protected class DeleteAnimationListener extends EndAnimationListener {

        private final YourListObject obj;

        public DeleteAnimationListener(YourListObject obj) {
            this.obj = obj;
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            yourObjects.remove(obj);
            deleteItems.remove(obj);
            notifyDataSetChanged();
        }
    };

currentObj 是您当前正在操作的列表中的视图

yourObjects 是您存储数据的列表。

deleteItems 是一个新列表。

你调用.remove(obj)你的适配器来移除一个。一旦动画完成,getView 将从列表中删除它(带有淡出动画),AnimationListener 将从数据集中删除实际对象。

于 2012-05-30T12:16:17.287 回答