`
whao189
  • 浏览: 123092 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

图片缩放

阅读更多
话不多说,直接看代码!
代码就一个文件!
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);

	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics