从Recyclerview添加到购物车的Android动画

3
我有一个Recycler View,显示了产品列表,每个 Recycler View 行项目中都有一个购物车图标。 点击购物车图标后,我希望产品图片能够复制、缩放并移动到右上角的操作栏上的购物车图标中。 我的方法: 在 RecyclerView 的行项目 XML 中创建了一个 ImageView,并最初将其设置为不可见。 当从 RecyclerView 点击购物车图标时,使该 ImageView 可见,并用图像 URL 填充 ImageView,并添加从起始位置到结束位置的平移动画。 当前结果: 图像在各自的单个项目行内移动,并在触及行项目边界后消失。我的意思是,图像没有出现在行项目之外,也没有到达顶部操作栏菜单项。 尝试的解决方案:
public static void setAllParentsClip(View v, boolean enabled) {
        while (v.getParent() != null && v.getParent() instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) v.getParent();
            viewGroup.setClipChildren(enabled);
            viewGroup.setClipToPadding(enabled);
            v = viewGroup;
        }
    }

同时在xml中手动设置所有父级:
android:clipChildren="false"
android:clipToPadding="false"

当图像触及行项目的边界后,它仍然会消失。任何建议都将是巨大的帮助。

我的RecyclerView适配器代码:

public class TrendingItemsAdapter extends RecyclerView.Adapter<TrendingItemsAdapter.MyViewHolder> {

    private List<ProductEventDetail> productEventDetailList;
    private Context context;


    public TrendingItemsAdapter (List<ProductEventDetail> productEventDetailList){
        this.productEventDetailList=productEventDetailList;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView productName;
        private TextView progressDisplay;
        private ImageView imageView, addToCart,animImg;
        private ProgressBar progressBar;

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public MyViewHolder(View view) {
            super(view);
            productName=(TextView)view.findViewById(R.id.trending_Product_Name);
            imageView=(ImageView)view.findViewById(R.id.trending_product_img);
            addToCart=(ImageView)view.findViewById(R.id.add_to_cart);
            animImg=(ImageView)view.findViewById(R.id.animProductImg);
            progressDisplay=(TextView)view.findViewById(R.id.event_progress_value);
            progressBar=(ProgressBar)view.findViewById(R.id.event_progress);
            progressBar.setProgressDrawable(context.getDrawable(R.drawable.progress_tint));

            addToCart.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if(v.getId()==addToCart.getId()){

                Toast.makeText(context,productEventDetailList.get(getAdapterPosition()).getProductName(),Toast.LENGTH_SHORT).show();
                setAllParentsClip(animImg,false);

                // Animation code

                Animations anim = new Animations();
                int fromLoc[] = new int[2];
                imageView.getLocationOnScreen(fromLoc);
                animImg.setVisibility(View.VISIBLE);

                Glide.with(context)
                        .load(productEventDetailList.get(getAdapterPosition()).getProductImageUrl())
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .fitCenter()
                        .into(animImg);

                float startX = fromLoc[0];
                float startY = fromLoc[1];
                Animation a = anim.fromAtoB(startX,startY, 1028, 143, animL,2000);
                animImg.setAnimation(a);
                a.startNow();

            }



        }

        Animation.AnimationListener animL = new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //this is just a method call you can create to delete the animated view or hide it until you need it again.
                animImg.setVisibility(View.GONE);
            }
        };
    }



    @Override
    public TrendingItemsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        context=parent.getContext();

        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.trending_item_row_detail, parent, false);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callProductEventDetails();
            }
        });


        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(TrendingItemsAdapter.MyViewHolder holder, int position) {

        ProductEventDetail productEventDetail= productEventDetailList.get(position);
        holder.productName.setText(productEventDetail.getProductName()+ " xxxx xxxxxxxxxxxxxxxx..");
        holder.progressBar.setProgress(productEventDetail.getEventProgressStatus());
        holder.progressDisplay.setText(String.valueOf(productEventDetail.getEventProgressStatus())+"% ");

        Glide.with(context)
                .load(productEventDetail.getProductImageUrl())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .fitCenter()
                .into(holder.imageView);


    }

    @Override
    public int getItemCount() {
        return productEventDetailList.size();
    }

private void callProductEventDetails(){
    Intent intent= new Intent(context, ProductEventDetailActivity.class);
    intent.putExtra("eventStatus","inProgress");
    context.startActivity(intent);
}

    public static void setAllParentsClip(View v, boolean enabled) {
        while (v.getParent() != null && v.getParent() instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) v.getParent();
            viewGroup.setClipChildren(enabled);
            viewGroup.setClipToPadding(enabled);
            v = viewGroup;
        }
    }
    }

你做了吗? - Cătălin Florescu
@FlorescuGeorgeCătălin 是的...我有点做到了,我能够将图像作为静态大小发送到购物车...但是当它向上移动时调整大小会使其偏离目标...但如果它是一个具有静态大小的图像,则可以完全移动到购物车中... - Moulesh
用这段代码? - Cătălin Florescu
不行,这段代码不会起作用...由于我只是部分地完成了它,所以无法将其发布为解决方案...你需要帮忙吗? - Moulesh
是的,如果您想的话。我想看看您创建动画并启动它的代码,请。也许我们可以找出如何做到这一点。 - Cătălin Florescu
请查看此链接:https://dev59.com/EIfca4cB1Zd3GeqPdAAi,其中提供了TextView的解决方案,我已经将其复制到ImageView上,但是我无法逐渐缩放图像...如果您能够解决缩放问题,请告诉我。 - Moulesh
1个回答

0

我想你正在寻找加入购物车动画, 你可以在这里找到解决方案 飞向购物车动画


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接