放大和缩小按钮在 android 中不起作用

zoom in and zoom out button not working in android

下面是我用于放大和缩小按钮的代码,但它是针对特定位置进行缩放的,我需要这些按钮的工作方式与按下缩放按钮时缩放图像的方式相同,例如 windows 图片查看器。我不需要捏缩放选项。谁能帮忙。提前致谢。

    Button zoonIn = (Button) findViewById(R.id.zoomIn);
    Button zoonOut = (Button) findViewById(R.id.zoomOut);

    zoonIn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            zoom(2f, 2f, new PointF(0, 0));
        }
    });


    zoonOut.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            zoom(0.5f, 0.5f, new PointF(0, 0));
        }
    });


}

缩放方式

public void zoom(Float scaleX, Float scaleY, PointF pivot) {
    ivImage.setPivotX(pivot.x);
    ivImage.setPivotY(pivot.y);
    ivImage.setScaleX(scaleX);
    ivImage.setScaleY(scaleY);
}

在 activity.I 添加 zoomIn()zoomOut() 函数来缩放 imageView。

public class MainActivity extends AppCompatActivity {
    Button zoomInButton, zoomOutButton;
    ImageView imageView;
    int offset = 0, duration = 100;
    float scaleX = 1.0f, scaleY = 1.0f;
    float maxZoomLimit = 2.6f, minZoomLimit = 1.0f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        zoomInButton= (Button) findViewById(R.id.button2);
        zoomOutButton= (Button) findViewById(R.id.button3);
        imageView = (ImageView) findViewById(R.id.imageView);
        zoomInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zoomIn(imageView);
            }
        });
        zoomOutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                zoomOut(imageView);
            }
        });

    }

    private void zoomIn(View v) {
        if (scaleX < maxZoomLimit && scaleY < maxZoomLimit) {
            Animation animation = new ScaleAnimation(scaleX, (scaleX + 0.2f), scaleY, (scaleY + 0.2f), 50, 50);
            scaleX += 0.2f;
            scaleY += 0.2f;
            animation.setInterpolator(new DecelerateInterpolator());
            animation.setDuration(duration);
            animation.setStartOffset(offset);
            animation.setFillAfter(true);
            v.startAnimation(animation);
        }
    }

    private void zoomOut(View v) {
        if (scaleX > minZoomLimit && scaleY > minZoomLimit) {
            float x = v.getScaleX();
            float y = v.getScaleY();
            Animation animation = new ScaleAnimation(scaleX, (scaleX - 0.2f), scaleY, (scaleY - 0.2f), 50, 50);
            scaleY -= 0.2f;
            scaleX -= 0.2f;
            animation.setInterpolator(new DecelerateInterpolator());
            animation.setDuration(duration);
            animation.setStartOffset(offset);
            animation.setFillAfter(true);
            v.startAnimation(animation);
        }
    }
}