放大和缩小 button 在不工作android

发布时间: 2016/10/10 20:09:51
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

下面是我使用的放大和缩小按钮的代码,但它缩放为特定的位置,我需要这些按钮的作品一样如何我们缩放图像时缩放 button 像 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);
}

解决方法 1:

做这个你activity的变化。我添加了 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);
        }
    }
}
官方微信
官方QQ群
31647020