gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:31:14 24 4
gpt4 key购买 nike

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

最佳答案

TouchImageView 类中添加这两个方法

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

}

关于Android:使用按钮放大缩小 ImageView(没有 MotionEvent),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39895812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com