我需要能够使用两个按钮放大/缩小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();
}
}
单击按钮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();
}
});
}
您可以用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);
点击ClickListener:上的按钮
myImageView.startAnimation(zoomin);
希望能有所帮助。
**dependencies {
compile 'com.github.chrisbanes:PhotoView:1.3.0'//image zooming
}**
将此导入您的gradel
有关详细信息:https://github.com/chrisbanes/PhotoView