<ImageView android:id="@+id/picture_view"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:maxHeight="300dip" android:maxWidth="300dip"
android:minHeight="300dip" android:minWidth="300dip"
android:scaleType="fitXY">
</ImageView>
public class PictureView extends Activity {
private ImageView mImageView;
private Button mButton01;
private Button mButton02;
private LinearLayout layout;
private LinearLayout layout1;
private ScrollView scrollView;
private int displayHeight;
private int displayWidth;
private Bitmap bmp;
private int id = 0;
private float scaleWidth = 1;
private float scaleHeight = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.picture_view);
setTitle("图片名称带后缀名");
layout = (LinearLayout) findViewById(R.id.picture_show);
scrollView = (ScrollView) findViewById(R.id.picture_scroll);
layout1 = (LinearLayout) findViewById(R.id.picture_area);
mImageView = (ImageView) findViewById(R.id.picture_view);
mImageView.setImageDrawable(getResources()
.getDrawable(R.drawable.pop32));
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth = dm.widthPixels;
displayHeight = dm.heightPixels - 80;
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pop32);
mButton01 = (Button) findViewById(R.id.menu_button);
mButton02 = (Button) findViewById(R.id.back_button);
mButton01.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
small();
}
});
mButton02.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
big();
}
});
}
/* 图片缩小的method */
private void small() {
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) {
/* 如果是第一次按,就删除原来默认的ImageView */
layout1.removeView(mImageView);
} else {
/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
layout1.removeView((ImageView) findViewById(id));
}
/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(PictureView.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout);
/* 因为图片放到最大时放大按钮会disable,所以在缩小时把他重设为enable */
mButton02.setEnabled(true);
}
/* 图片放大的method */
private void big() {
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) {
/* 如果是第一次按,就删除原来设置的ImageView */
layout1.removeView(mImageView);
} else {
/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
layout1.removeView((ImageView) findViewById(id));
}
/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(PictureView.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout);
/* 如果再放大会超过屏幕大小,就把Button disable */
if (scaleWidth * scale * bmpWidth > displayWidth
|| scaleHeight * scale * bmpHeight > displayHeight) {
mButton02.setEnabled(false);
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。