android - 捏合以放大或缩小动态添加到布局的 ImageView

标签 android imageview android-imageview pinchzoom

我正在开发一个应用程序,在该应用程序中,动态创建 ImageView 并在用户从设备图库中选择图像后将其添加到布局中。

使用我编写的代码,可以移动和旋转 ImageView。但是,我还想为 ImageView 实现双指缩放。

代码中存在的错误是在将 ImageView 添加到布局后,将 ImageView 带到最右边时会缩小,所以我想做的是,但这应该在捏合时发生。

编辑:调试我的代码后,我发现我想通过增加宽度和高度来增加 ImageView 的大小。 当我更改以下行时: final RelativeLayout.LayoutParams 参数 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 收件人:

final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                                    240,
                                    200);

我得到了我想要的。那么我能否在 class DragImageView 中捏合时执行此操作。

这是自定义 Imageview 的代码。

public class DragImageView extends ImageView {

    private float mLastTouchX;
    private float mLastTouchY;

    private float mDeltaX;
    private float mDeltaY;
    private Bitmap bmpImg;
    Context mContext;

    public DragImageView(Context context, Bitmap bmpImg) {
        super(context);
        this.bmpImg = bmpImg;
        this.mContext = context;
        init();

    }

    public DragImageView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // TODO Auto-generated method stub

        Bitmap resized = Bitmap.createScaledBitmap(bmpImg, 180, 200, true);
        Bitmap conv_bm = getRoundedShape(resized); //function to get the imageview as     rounded shape
        setImageBitmap(conv_bm);

        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(final View v, MotionEvent event) {
                PopupMenu popup = new PopupMenu(mContext, v);
                // Inflating the Popup using xml file
                popup.getMenuInflater().inflate(R.menu.popup_menu,
                        popup.getMenu());

                popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        int itemId = item.getItemId();
                        if (itemId == R.id.delete_DragImgView) {
                            ViewGroup parentView = (ViewGroup) v.getParent();
                            parentView.removeView(v);
                        } else if (itemId == R.id.rotate_DraagImgView) {
                            final RotateAnimation rotateAnim = new RotateAnimation(
                                    0.0f, 90, RotateAnimation.RELATIVE_TO_SELF,
                                    0.5f, RotateAnimation.RELATIVE_TO_SELF,
                                    0.5f);
                            rotateAnim.setDuration(0);
                            rotateAnim.setFillAfter(true);
                            v.startAnimation(rotateAnim);
                        }
                        return false;
                    }
                });

                final int action = event.getAction();

                mLastTouchX = event.getRawX();
                mLastTouchY = event.getRawY();

                switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) getLayoutParams();
                    mDeltaX = mLastTouchX - lParams.leftMargin;
                    mDeltaY = mLastTouchY - lParams.topMargin;
                    popup.show();
                    break;
                }
                case MotionEvent.ACTION_MOVE: {
                    mLastTouchX = event.getRawX();
                    mLastTouchY = event.getRawY();

                    final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
                    params.leftMargin = (int) (mLastTouchX - mDeltaX);
                    params.topMargin = (int) (mLastTouchY - mDeltaY);
                    setLayoutParams(params);

                    break;
                }
                }
                invalidate();

                return true;
            }
        });
    }

从 Activity 中添加 ImageView 如下。

final DragImageView dynamicImgView = new DragImageView(
                            getApplicationContext(), yourSelectedImage);



                    final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
                    dynamicImgView.setLayoutParams(params);
                    relativeLayout.addView(dynamicImgView);

我知道捏合缩放是可能的,但在尝试了很多代码和来自 stackover flow 的帮助之后,我想到了最终将它放上去。 请帮我。提前致谢。

最佳答案

解决了我的问题。我只是增加了 imageview 的高度和宽度。代码

} else if (itemId == R.id.zoom_in) {
                            if (height > 100) {
                                height = height - ZoomCounter;
                                width = width - ZoomCounter;
                                final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
                                getLayoutParams().height = height;
                                getLayoutParams().width = width;
                                setLayoutParams(params);
                                invalidate();
                            }

                        } else if (itemId == R.id.zoom_out) {
                            if (height < 600) {
                                height = height + ZoomCounter;
                                width = width + ZoomCounter;
                                final RelativeLayout.LayoutParams params = (LayoutParams) getLayoutParams();
                                getLayoutParams().height = height;
                                getLayoutParams().width = width;
                                setLayoutParams(params);

                                invalidate();
                            }
                        }

关于android - 捏合以放大或缩小动态添加到布局的 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24825449/

相关文章:

android - 在 GridView 中使用 setGravity()

java - 回收 ImageViews 以避免 OutOfMemoryError

Android picasso 在加载到 imageView 之前检查图像 url 是否存在

android - picasso 居中图像而不调整大小到 ImageView

android - React Native构建失败:文件超出大小限制2147483647

android - 一款开发android/ios/bb/windows 8的软件

android - Phonegap 错误 - "No Content-Security-Policy meta tag found. Please add one when using the cordova-plugin-whitelist plugin."

java - 如何为多个 ImageView 设置触摸监听器?

java - ImageView 圆角

java - 通用图像加载器 (UIL) 使图像在加载到 ImageView 时太小