【问题标题】:Android: Zoom in-out ImageView with buttons (without MotionEvent)Android:使用按钮放大 ImageView(没有 MotionEvent)
【发布时间】:2016-10-06 12:19:18
【问题描述】:

我需要能够使用两个按钮来放大/缩小 ImageView:一个用于放大,另一个用于缩小。 我已经成功地用 MotionEvent 实现了放大/缩小,但不知道如何用按钮来实现。 也许我可以创建虚假的 MotionEvent 或类似的东西?

【问题讨论】:

    标签: android motionevent zooming


    【解决方案1】:

    TouchImageView类中添加这2个方法

    public void zoomIn() { //bind on zoomIn Button
        oldScale = saveScale;
    
        if(saveScale<=maxScale)
        {
            saveScale += .5;
            matrix.setScale(saveScale, saveScale);
            setImageMatrix(matrix);
            invalidate();
    
            // Center the image
            // Center the image
            if(bmHeight>bmWidth)
            {
            redundantXSpace = width - (saveScale * bmWidth);
            redundantXSpace /= 2;
            }
            else 
            {
                redundantYSpace = height - (saveScale * bmHeight) ;
                redundantYSpace /= 2;
            }
    
            matrix.postTranslate(redundantXSpace , redundantYSpace );
            setImageMatrix(matrix);
            invalidate();
        }
    }
    
    public void zoomOut() { 
    
    
     //Bind on zoom Out Button
    
        if(saveScale>=minScale)
        {
            saveScale -= .5;
            matrix.setScale(saveScale, saveScale);
            setImageMatrix(matrix);
            invalidate();
    
            // Center the image
            if(bmHeight>bmWidth)
            {
            redundantXSpace = width - (saveScale * bmWidth);
            redundantXSpace /= 2;
            }
            else 
            {
                redundantYSpace = height - (saveScale * bmHeight) ;
                redundantYSpace /= 2;
            }
            matrix.postTranslate(redundantXSpace , redundantYSpace );
            setImageMatrix(matrix);
            invalidate();
        }
    }
    

    对于按钮点击 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

    setContentView(R.layout.LAYOUT_NAME);
    
    Button zoonIn = (Button)findViewById(R.id.ZOOM_IN_BUTTON_ID);
    Button zoonOut = (Button)findViewById(R.id.ZOOM_OUT_BUTTON_ID);
    
    final TouchImageView touch = (TouchImageView)findViewById(R.id.YOUR_TOUCH_IMAGE_VIEW_)ID);
    
    Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.DRAWABLE_ID);
    
    touch.setImageBitmap(bImage);
    
    touch.setMaxZoom(4f); //change the max level of zoom, default is 3f
    
    
    zoonIn.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            touch.zoomIn();
        }
    });
    
    
    zoonOut.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            touch.zoomOut();
        }
    });
    
    }
    

    【讨论】:

    • 优秀的答案!你拯救了我的一天。谢谢。
    • 我的荣幸.. :) @scat95
    【解决方案2】:

    您可以在 XML 中创建动画,例如:

    /res/anim/zoomin.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillEnabled="true"
        android:fillAfter="true">
        <scale
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="2000"
            android:fromXScale="1"
            android:fromYScale="1"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toXScale="1.5"
            android:fillEnabled="true"
            android:fillAfter="true"
            android:toYScale="1.5" >
        </scale>
    </set>
    

    还有一个类似的:

    /res/anim/zoomout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set
        android:fillEnabled="true"
        android:fillAfter="true"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <scale
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="2000"
            android:fromXScale="1.5"
            android:fromYScale="1.5"
            android:pivotX="50%"
            android:pivotY="50%"
            android:fillEnabled="true"
            android:fillAfter="true"
            android:toXScale="1"
            android:toYScale="1" >
        </scale>
    </set>
    

    在你的 onCreate 中:

    Animation zoomin= AnimationUtils.loadAnimation(this, R.anim.zoomin);
    Animation zoomout= AnimationUtils.loadAnimation(this, R.anim.zoomout);
    

    然后点击你的按钮 onClickListener :

    myImageView.startAnimation(zoomin);
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:
      **dependencies {
          compile 'com.github.chrisbanes:PhotoView:1.3.0'//image zooming
      }**
      

      将此导入您的等级中

      欲了解更多信息:https://github.com/chrisbanes/PhotoView

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-17
        • 2010-11-17
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        • 2020-11-29
        相关资源
        最近更新 更多