1,图片缩放
|
01
| public
static Drawable resizeImage(Bitmap bitmap,
int w, int
h) { |
04 | Bitmap BitmapOrg = bitmap;
|
06 | int
width = BitmapOrg.getWidth(); |
07 | int
height = BitmapOrg.getHeight(); |
12 | float
scaleWidth = ((float) newWidth) / width;
|
13 | float
scaleHeight = ((float) newHeight) / height;
|
16 | Matrix matrix =
new Matrix();
|
18 | matrix.postScale(scaleWidth, scaleHeight);
|
23 | Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg,
0, 0, width,
|
24 | height, matrix,
true); |
28 | return
new BitmapDrawable(resizedBitmap);
|
android:scaleType可控制图片的缩放方式,示例代码如下:
<ImageView android:id="@+id/img"
android:src="@drawable/logo"
android:scaleType="centerInside"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_centerVertical="true"/>
说明:centerInside表示按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度。
注意:控制的图片为资源而不是背景,即 android:src="@drawable/logo",而非android:background="@drawable/logo",我就笨笨地犯了这个低级错误,导致错怪人家scaleType不起作用。程序中动态加载图片也类似,如:应该 imgView.setImageResource((Integer)mData.get(position).get("img"));而非 imgView.setBackgroundResource((Integer)mData.get(position).get("img"));
但同时你也要自己写事件监听,有方法可以实现。可以直接调用.流程是得到一个view,然后view.setImageMatrix(Matrix m).然后创建一个matrix,matrix.postScale(s1,s2,x,y);s1,s2是横向缩放比例,s2是纵向缩放比例,x,y是中心点的坐标。缩放比例大于1,就是放大,小于1是缩小