使用ImageViews的RecyclerView,放大触摸

时间:2015-07-23 10:29:57

标签: android animation imageview android-recyclerview

我是Android的新手,我遇到了这个问题。我有一个包含ImageViews的RecyclerView。我想要做的是,当我触摸某个项目时,它应该将图像放大到原始大小。 RecyclerView目前占据了屏幕的五分之一。我的适配器如下:

<select name="exempleselect">
    <option class="value1" value="1">value 1</option> 
    <option class="value2" value="2">value 2</option>
    <option class="value3" value="3">value 3</option>
</select>

我的片段类中的相关部分如下:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnTouchListener {

    private ArrayList<Bitmap> pieces;
    private RecyclerViewOnTouchListener touchListener;

    public interface RecyclerViewOnTouchListener {
        void onTouch(View v, MotionEvent event);
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
    ImageView imageViewPiece;

    public MyViewHolder(View itemView) {
        super(itemView);
        this.imageViewPiece = (ImageView) itemView.findViewById(R.id.pieceImage);
        }
    }

    public MyAdapter(ArrayList<Bitmap> pieces, RecyclerViewOnTouchListener touchListener) {
        this.pieces = pieces;
        this.touchListener = touchListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Inflate layout for recycler view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view, parent, false);

        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
        ImageView imageView = holder.imageViewPiece;
        imageView.setOnTouchListener(this);
        imageView.setTag(listPosition);
        imageView.setImageBitmap(pieces.get(listPosition));
    }

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


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView imageView = (ImageView) v.findViewById(R.id.pieceImage);

        if (touchListener != null) {
            touchListener.onTouch(imageView, event);
        }
        return false;
    }

}

我认为它的逻辑应该是这样的,但我被卡住了。我希望我已经写了足够的关于这个问题的信息。

我的片段如下所示:http://i.imgur.com/alEG7OP.png

我想要触摸右边的一块,当我这样做时,它应该放大到原来的大小,当我停止触摸时,它应该回到列表中,与其他列表的大小相同。我也希望能够移动它。

3 个答案:

答案 0 :(得分:2)

首先做一些小修补:

从onBindViewHolder()中删除此行:

 imageView.setOnTouchListener(this);

将其改为MyViewHolder:

this.imageViewPiece.setOnTouchListener(this);

因为您只需要初始化侦听器1,而不是每次绑定调用。

更改OnTouchListener:

@Override
 public boolean onTouch(View v, MotionEvent event) {
    if (touchListener != null) {
        touchListener.onTouch((ImageView) v, event);
    }
    return false;
}

由于您知道在onTouch中收到的项目是您注册的项目。

然后你可以做类似的事情,你可以做一些调整:

MyAdapter.RecyclerViewOnTouchListener onTouchListener = new MyAdapter.RecyclerViewOnTouchListener() {
    Bitmap bitmap;
    int originalPosition, width, height;
    ImageView imageView;
    RelativeLayout.LayoutParams params;
    @Override
    public void onTouch(ImageView v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int x = (int)event.getRawX();
                int y = (int) event.getRawY();
                params.leftMargin =  x - width / 2;
                params.topMargin =  y - height / 2;
                imageView.setLayoutParams(params);
                break;
            case MotionEvent.ACTION_DOWN:
                originalPosition = (int)v.getTag();
                bitmap = recycleAdapter.pieces.remove(originalPosition);
                recycleAdapter.nottifyDataSetChanged();
                width = bitmap.getWidth(); //maybe you need the drawable to get the intrinsic original dimens
                height = bitmap.getHeight();
                imageView = new ImageView(v.getContext());
                params = new RelativeLayout.LayoutParams(width, height);
                imageView.setImageBitmap(bitmap);
                RelativeLayout container; //here you need to bind this view to your container of fragment, better if it RelativeLayout to do dragging
                container.addView(imageView, params);
                // what you need to do is inflate here
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                container.removeView(imageView);
                recycleAdapter.pieces.add(originalPosition, bitmap);
                recycleAdapter.nottifyDataSetChanged();
                break;
            default:
                break;
        }
    }


};

答案 1 :(得分:0)

使用https://github.com/MikeOrtiz/TouchImageView java

Bitmap bitmap; Drawable d = itemImage.getDrawable(); bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); d.draw(canvas); Dialog dialog=new Dialog(context,android.R.style.Theme_Black_NoTitleBar_Fullscreen); dialog.setContentView(R.layout.dialog_image_view_layout); TouchImageView imageDialog = (TouchImageView) dialog.findViewById(R.id.imageDialog); imageDialog.setImageBitmap(bitmap); dialog.show();

答案 2 :(得分:0)

我添加了一个运行示例https://github.com/MikeOrtiz/TouchImageView/pull/217,该示例介绍如何在RecyclerView中缩放图像

这是魔术

        imageview.setOnTouchListener { view, event ->
            var result = true
            //can scroll horizontally checks if there's still a part of the image
            //that can be scrolled until you reach the edge
            if (event.pointerCount >= 2 || view.canScrollHorizontally(1) && canScrollHorizontally(-1)) {
                //multi-touch event
                result = when (event.action) {
                    MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                        // Disallow RecyclerView to intercept touch events.
                        parent.requestDisallowInterceptTouchEvent(true)
                        // Disable touch on view
                        false
                    }
                    MotionEvent.ACTION_UP -> {
                        // Allow RecyclerView to intercept touch events.
                        parent.requestDisallowInterceptTouchEvent(false)
                        true
                    }
                    else -> true
                }
            }
            result
        }