Android图片的缩放处理

/**
 * resize Bitmap
 *
 * @param bitmap
 * @param newWidth
 * @return
 */
public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {
	if (bitmap == null)
		return null;
	int w = bitmap.getWidth();
	int h = bitmap.getHeight();

	Log.e("Jarvis", w + "~" + h);

	float temp = ((float) h) / ((float) w);
	int newHeight = (int) (newWidth * temp);
	float scaleWidth = ((float) newWidth) / w;
	float scaleHeight = ((float) newHeight) / h;
	Matrix matrix = new Matrix();
	matrix.postScale(scaleWidth, scaleHeight);
	Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
			true);
	if (!bitmap.isRecycled()) {
		bitmap.recycle();
	}

	return resizedBitmap;
}
/**
 * 放大缩小图片
 *
 * @param bitmap
 * @param w
 * @param h
 * @return
 */
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
	int width = bitmap.getWidth();
	int height = bitmap.getHeight();
	Matrix matrix = new Matrix();
	float scaleWidht = ((float) w / width);
	float scaleHeight = ((float) h / height);
	matrix.postScale(scaleWidht, scaleHeight);
	Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
			matrix, true);
	return newbmp;
}
时间: 2024-09-20 23:32:42

Android图片的缩放处理的相关文章

Android图片旋转,缩放,位移,倾斜,对称完整示例(二)——Bitmap.createBitmap()和Matrix

MainActivity如下: package cc.c; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; /** * Demo描述: * 利用B

Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix

MainActivity如下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.app.Activity; import android.graphics.Matrix; /** * Demo描述:

android图片的缩放

  import android.app.Activity;    import android.graphics.Bitmap;    import android.graphics.BitmapFactory;    import android.graphics.Matrix;    import android.graphics.drawable.BitmapDrawable;    import android.os.Bundle;    import android.view.Vie

android 图片操作(缩放移动) 实例代码_Android

view_show.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_par

android 图片操作(缩放移动) 实例代码

view_show.xml 复制代码 代码如下:<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_pare

Android实现通过手势控制图片大小缩放的方法_Android

本文实例讲述了Android实现通过手势控制图片大小缩放的方法.分享给大家供大家参考,具体如下: 该程序实现的是通过手势来缩放图片,从左向右挥动图片时图片被放大,从右向左挥动图片时图片被缩小,挥动速度越快,缩放比越大.程序思路如下:在界面中定义一个ImageView来显示图片,使用一个GestureDetector来检测用户的手势,并根据用户的手势在横向的速度来缩放图片. 在介绍这个实例前,先介绍一下Android中处理手势触摸事件的大概框架. 一.添加语句实现OnGestureListener

Android 图片缩放实例详解_Android

本文实现Android中的图片的缩放效果 首先设计布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_par

动画-关于android图片缩放的问题

问题描述 关于android图片缩放的问题 我有一张很大的图片,比如说它的分辨率为960*800(宽和高),而手机的分辨率为480*800(宽和高),刚开始的时候图片放在一个imageview中,它的scaleType为center,如果我想通过动画的形式把这个图片的scaleType设为fitcenter,动画效果要求:图片本身不动,慢慢从四周缩小到fitcenter的比例形式. 解决方案 自己做个View/ImageView,然后override onDraw.

android-求大神帮忙啊!Android用手势缩放图片的时候图片变黑

问题描述 求大神帮忙啊!Android用手势缩放图片的时候图片变黑 源码: public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { // TODO Auto-generated method stub velocityX=velocityX>4000?4000:velocityX; velocityX=velocityX<-4000?-4000:velo