【问题标题】:zoom in and zoom out button not working in android放大和缩小按钮在android中不起作用
【发布时间】:2016-09-06 07:17:02
【问题描述】:

下面是我用于放大和缩小按钮的代码,但它正在缩放特定位置,我需要这些按钮的工作方式与我们在按下缩放按钮时缩放图像的方式相同,就像在 windows imageviewer 中一样。我不需要捏缩放选项。任何人都可以帮忙吗?提前致谢。

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

【问题讨论】:

标签: android button uiimageview onclicklistener zooming


【解决方案1】:

在您的活动中进行此更改。我添加了zoomIn()zoomOut() 函数来缩放图像视图。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多