基于Empty创建安卓应用ZoomImageByButton
导入图片
在activity_main.xml输入内容
然后在MainActivity内编辑就好了。
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView ivMickey;
private double imageWidth;
private double imageHeight;
private double screenWidth;
private double screenHeight;
private double scale = 0.95;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivMickey=findViewById(R.id.iv_mickey);
screenHeight=getWindowManager().getDefaultDisplay().getHeight();
screenWidth=getWindowManager().getDefaultDisplay().getWidth();
imageWidth=ivMickey.getLayoutParams().width;
imageHeight=ivMickey.getLayoutParams().height;
}
public void doShinkImage(View view){
int newWidth=(int)(imageWidth*scale);
int newHeight=(int)(imageHeight*scale);
if(newWidth>50){
ivMickey.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
imageWidth=ivMickey.getLayoutParams().width;
imageHeight=ivMickey.getLayoutParams().height;
}else {
Toast.makeText(this,"温馨提示:图片不能再缩小",Toast.LENGTH_SHORT).show();
}
}
public void doEnlargeImage(View view){
int newWidth=(int)(imageWidth/scale);
int newHeight=(int)(imageHeight/scale);
if(newWidth<screenWidth){
ivMickey.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
imageWidth=ivMickey.getLayoutParams().width;
imageHeight=ivMickey.getLayoutParams().height;
}else {
Toast.makeText(this,"温馨提示:图片不能再放大",Toast.LENGTH_SHORT).show();
}
}
public void doExit(View view){
finish();
}
}