图片缩放

话不多说,直接看代码!
代码就一个文件!

package com.bitmap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
/** Called when the activity is first created. */
private Bitmap bitMap;
private ImageView imageView01;
private View view;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据 drawable 中的资源 创建 一个bitmap
bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageBitmap(bitMap);

Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//加载 自定义 的布局!目的是为了让我们控制 要缩放的 大小
//顺便复习一个 自定义布局
LayoutInflater layout = LayoutInflater.from(MainActivity.this);
view = layout.inflate(R.layout.alert_dialog, null);
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setIcon(R.drawable.icon);
alert.setTitle("设置缩放的大小");
alert.setView(view);
alert.setButton(getApplicationContext().getText(R.string.YES), yesOnClick);
alert.setButton2(getApplicationContext().getText(R.string.NO), noOnClick);
alert.show();
}
});
}

private final android.content.DialogInterface.OnClickListener yesOnClick = new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
imageView01 = (ImageView)findViewById(R.id.imageView01);

//获得 用户输入的 要缩放 图片的高和宽
EditText text01 = (EditText)view.findViewById(R.id.height);
int height = Integer.valueOf(text01.getText().toString());

EditText text02 = (EditText)view.findViewById(R.id.width);
int width = Integer.valueOf(text02.getText().toString());

Log.i(TAG, "height---->"+ height+"--->width---->"+width);
//下面才是关键
Drawable draw = resizeImage(bitMap,height,width);

imageView01.setBackgroundDrawable(draw);
}
};
private final android.content.DialogInterface.OnClickListener noOnClick = new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
};
//拿到 要缩放的图片!并指定要缩放的大小
public static Drawable resizeImage(Bitmap bitmap, int w, int h) {

Bitmap BitmapOld = bitmap;

int width = BitmapOld .getWidth();
int height = BitmapOld .getHeight();
int newWidth = w;
int newHeight = h;


float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;


Matrix matrix = new Matrix();
//将图片翻转 变化
//API 这么 写的 (Postconcats the matrix with the specified scale)
//我的理解就是 根据指定的 大小 在绘制矩阵(就是图片啊)
matrix.postScale(scaleWidth, scaleHeight);
//重绘 之后的图片
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOld, 0, 0, width,height, matrix, true);

return new BitmapDrawable(resizedBitmap);

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值