图片旋转

/**
		 * 调用获取屏幕尺寸的方法,缩放图片、高度设为屏幕的宽、宽度同比压缩、mb是最初被编辑的bitmap对象
		 */

		getDisplayMetrics(this.getBaseContext());
		int w = (int) ((screenWidth * mb.getWidth()) / (mb.getHeight()));
		bmp = Bitmap.createScaledBitmap(mb, w, screenWidth, false); // 宽、高

		iv = (ImageView) findViewById(R.id.edit_picture);
		width = bmp.getWidth();
		height = bmp.getHeight();
		iv.setImageBitmap(bmp);

		ivTurnLeft = (ImageView) findViewById(R.id.turn_left);
		ivTurnRight = (ImageView) findViewById(R.id.turn_right);

		/**
		 * 监听左旋转按钮、逆时针转90度
		 */

		ivTurnLeft.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Matrix matrix = new Matrix();
				matrix.postRotate(-90);
				Bitmap new_bmp = Bitmap.createBitmap(bmp, 0, 0, width, height,
						matrix, true);
				BitmapDrawable bd = new BitmapDrawable(new_bmp);
				iv.setImageDrawable(bd);
				iv.setScaleType(ScaleType.CENTER);
				bmp = new_bmp;

				x = width;
				width = height;
				height = x;
			}
		});

		/**
		 * 监听右旋转按钮、顺时针转90度
		 */

		ivTurnRight.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Matrix matrix = new Matrix();
				matrix.postRotate(90);
				Bitmap new_bmp = Bitmap.createBitmap(bmp, 0, 0, width, height,
						matrix, true);
				BitmapDrawable bd = new BitmapDrawable(new_bmp);
				iv.setImageDrawable(bd);
				iv.setScaleType(ScaleType.CENTER);
				bmp = new_bmp;
				x = width;
				width = height;
				height = x;
			}
		});

/**
	 * 获取手机屏幕尺寸的方法
	 */
	public void getDisplayMetrics(Context c) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = c.getApplicationContext().getResources().getDisplayMetrics();
		screenWidth = dm.widthPixels;
		// screenHeight = dm.heightPixels;
	}

/**
应该提取出左选装和右旋转的公共部分、避免代码重复、
新建Matrix对象的操作不能提取、否则会造成旋转累计、
第一次转90、第二次转180、然后各种错
**/

你可能感兴趣的:(android)