Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ZoroLu/3158626 to your computer and use it in GitHub Desktop.
Save ZoroLu/3158626 to your computer and use it in GitHub Desktop.
zoom in or zoom out a picture 缩小,放大图片
package net.gng.lgq;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private ImageView mImageView;
private Button zoomInBtn;
private Button zoomOutBtn;
private RelativeLayout layout;
private Bitmap bmp;
int id = 0;
private int displayWidth;
private int displayHeight;
private float scaleWidth = 1;
private float scaleHeight = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//取得屏幕分辨率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth = dm.widthPixels;
displayHeight = dm.heightPixels -100;
//初始化
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
mImageView = (ImageView)findViewById(R.id.imageView);
zoomInBtn = (Button)findViewById(R.id.zoom_in_btn);
zoomOutBtn = (Button)findViewById(R.id.zoom_out_btn);
layout = (RelativeLayout)findViewById(R.id.layout);
zoomInBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoomIn();
}
});
zoomOutBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoomOut();
}
});
}
//放大
private void zoomIn()
{
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
//放大比例
double scale = 1.25;
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
//产生resize后的bitmap
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
if(id == 0)
{
layout.removeView(mImageView);
}
else
{
layout.removeView((ImageView)findViewById(id));
}
id++;
ImageView imageView = new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout.addView(imageView);
setContentView(layout);
if(scaleWidth*scale*bmpWidth > displayWidth ||
scaleHeight*scale*bmpHeight > displayHeight)
{
zoomInBtn.setEnabled(false);
}
}
//缩小
private void zoomOut()
{
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
//缩小比例
double scale = 0.8;
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);
//产生resize后的bitmap
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
if(id == 0)
{
layout.removeView(mImageView);
}
else
{
layout.removeView((ImageView)findViewById(id));
}
id++;
ImageView imageView = new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout.addView(imageView);
setContentView(layout);
zoomInBtn.setEnabled(true);
}
}
@waqarmkhani
Copy link

Nice but how to concave and convex an imageview image using button setonclickedlistener
Tnks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment