如何在Android视图上模拟/动画滑动?

时间:2015-12-10 14:27:46

标签: android android-layout android-animation android-canvas android-recyclerview

我在RecyclerView中有这些项目:

enter image description here

当用户在任何项目中滑动带有Canvas的背景时,使用Paint实例(将背景变为粉红色)和deleteIcon的Bitmap(出现垃圾图标),如下所示: enter image description here

稍微滑动

enter image description here

这是我的代码,通过使用ItemTouchHelper.SimpleCallback和方法onChildDraw()显示粉红色背景和trashicon图像显示为用户滑动:

public class CartSwipeListener extends ItemTouchHelper.SimpleCallback{

     //more code....

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                            float dX, float dY, int actionState, boolean isCurrentlyActive) {

        //If user is Swiping over item
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            // Get RecyclerView item from the ViewHolder
            View itemView = viewHolder.itemView;

            //The color for the background that appears when swiping
            Paint p = new Paint();
            p.setColor(context.getResources().getColor(R.color.MAIN_A));

            //Make options so Bitmap isn't displayed pixelated
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;

            //create the deleteIcon
            Bitmap deleteIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_trash, options);

            // Draw background rectangle with varying left side, equal to the item's right side plus negative displacement dX
            c.drawRect(
                    (float) itemView.getRight() + dX,    //left: notice we use + dX which is displacement of X by swipe
                    (float) itemView.getTop(),           //top
                    (float) itemView.getRight(),         //right
                    (float) itemView.getBottom(),        //bottom
                    p                                    //paint
            );

            //Draw the deleteIcon
            c.drawBitmap(
                    deleteIcon, //Bitmap to be drawn
                    (float) itemView.getRight()/1.1f, // position of the LEFT side of the bitmap being drawn
                    (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - deleteIcon.getHeight()) / 2, //The position of the TOP side of the bitmap being drawn
                    null //The paint used to draw the bitmap (may be null)
            );
        }

        //Default onChildDraw implementation
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

我的问题:

我只需要为RecyclerView的第一行设置动画,就好像它向左或多或少向左滑动100px,然后粉红色背景显示和垃圾图标显示然后撤消滑动并返回原始位置。这是为了向用户提供他/她可以滑动以删除的提示。

我一直在阅读有关动画但我不知道如何绘制不在itemView中的所有教程我发现只是操纵视图(旋转,淡入淡出等)而不添加带位图的画布就像我刷卡。

0 个答案:

没有答案