Android:使用按钮放大缩小 ImageView(没有 MotionEvent)

Android: Zoom in-out ImageView with buttons (without MotionEvent)

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

TouchImageView中添加这2个方法Class

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();
    }
}

for on Button click 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();
    }
});

}
**dependencies {
    compile 'com.github.chrisbanes:PhotoView:1.3.0'//image zooming
}**

在你的等级中导入这个

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

您可以在 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);

希望对您有所帮助。