![](/img/trans.png)
[英]Size of ImageViews (with VectorDrawables) in RecyclerView
[英]RecyclerView with ImageViews, zoom on touch
我對 Android 有點陌生,我被這個問題困住了。 我有一個包含 ImageViews 的 RecyclerView。 我想要做的是,當我觸摸一個項目時,它應該將圖像放大到原始大小。 RecyclerView 目前覆蓋屏幕的五分之一。 我的適配器如下:
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;
}
}
我的片段類中的相關部分如下:
MyAdapter.RecyclerViewOnTouchListener onTouchListener = new MyAdapter.RecyclerViewOnTouchListener() {
@Override
public void onTouch(ImageView v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_DOWN:
// Enlarge image to its original size and remove it from the list, also it should be able to move in the recycler view.
break;
case MotionEvent.ACTION_UP:
// Put image back to its position at list
break;
default:
break;
}
}
};
我想它的邏輯應該是這樣的,但我被卡住了。 我希望我已經寫了足夠的關於這個問題的信息。
我的片段看起來像這樣: http : //i.imgur.com/alEG7OP.png
我想觸摸右邊的一塊,當我觸摸它時,它應該放大到原來的尺寸,當我停止觸摸時,它應該回到與其他尺寸相同的列表中。 我也希望能夠移動它。
首先是一些小修復:
從 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;
}
}
};
我添加了一個運行示例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
}
使用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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.