Android 使用图像视图循环查看,触摸时缩放
我对安卓有点陌生,我被这个问题困住了。我有一个保存图像视图的RecyclerView。我想做的是,当我触摸一个项目时,它应该将图像放大到其原始大小。RecyclerView目前占据了屏幕的五分之一。我的适配器如下所示:Android 使用图像视图循环查看,触摸时缩放,android,animation,imageview,android-recyclerview,Android,Animation,Imageview,Android Recyclerview,我对安卓有点陌生,我被这个问题困住了。我有一个保存图像视图的RecyclerView。我想做的是,当我触摸一个项目时,它应该将图像放大到其原始大小。RecyclerView目前占据了屏幕的五分之一。我的适配器如下所示: public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnTouchListener { private ArrayList&l
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;
}
}
我想它的逻辑应该是这样的,但我被卡住了。我希望我已经写了足够的关于这个问题的信息
我的片段如下所示:
我想触摸右边的一块,当我触摸时,它应该放大到原来的大小,当我停止触摸时,它应该回到列表中,大小与其他部分相同。我还希望能够移动它。首先是一些小修复: 从onBindViewHolder()中删除此行: 将其放在MyViewHolder中:
imageView.setOnTouchListener(this);
this.imageViewPiece.setOnTouchListener(this);
因为您只需要初始化侦听器1,而不是每次绑定调用
更改您的侦听器:
@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;
}
}
};
使用java
我添加了一个如何在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
}
是否要从动作中的回收中删除,并将其返回动作中?这个图像应该在哪里显示?@yshahaak我把它添加到问题中。请看结尾。在对话框\u image\u view\u layout中,这是否可以与Java一起使用?当然,您也可以使用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();
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
}