在recycleview时间轴中为每个视图添加drawable

问题描述 投票:0回答:1

我正在创建一个逐帧动画应用程序。在主绘图屏幕上,我在屏幕底部有一个recyclerview时间轴,用于保存每个画布图。当用户完成绘图并想要创建新帧时,应更新时间线视图以在时间线中包含该特定绘图的图像。

我正在将每个位图转换为可绘制对象,然后将其存储在具有正确位置的hashmap中。

我得到的错误是每个视图是用户绘制的第一帧。

drawing screen with timeline at bottom

这是我主要活动中的方法

//setting up the hashmap of drawable objects
public static Map<Integer, Drawable> drawables = new HashMap<>();

public void onItemClick(View view, int position) {

    // this sets the bitmap to the drawing on screen
    this.canvasView.setDrawingCacheEnabled(true);
    bitmap = this.canvasView.getDrawingCache();

    // this is just to reset the adapterPosition so the else statement gets called in onBindViewHolder
    adapterPosition = position;

   // convert bitmap to drawable then places into right position in hashmap
    myDrawable = new BitmapDrawable(getResources(), bitmap);
    drawables.put(this.pos,myDrawable); 

    // change the position value to the position of the recyclerview item clicked
    this.pos = position

    //call onBindViewHolder to update the recylerview
    adapter.notifyDataSetChanged();

这是我的onBindViewHolder方法

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Integer res = mFrames.get(position);

    //if statement only happens on first frame so set the image as default image
    if (adapterPosition == 20) {
        holder.myView.setBackgroundResource(res);
    }
    else{
        //get the drawable for the each position of the recyclerview using hashmap
        holder.myView.setBackground(drawables.get(position));
    }

调用onBindView时,视图应为hashmap drawable中的每个位置获取不同的图像,但每次返回图像的位置都在第一个位置

android android-recyclerview
1个回答
0
投票

这是我在你的代码中看到的:

  1. 你目前在adapterPosition设置onItemClick()的方法是错误的。你应该只使用position中提供的onBindViewHolder()
  2. drawables地图不应该是静态的。

这些可能无法立即解决您的问题,但步骤正确。显示更多代码也有帮助,例如如何将帧添加到mFrames,谁调用onItemClick()等。

© www.soinside.com 2019 - 2024. All rights reserved.