android 图片处理

 

1,图片缩放

 

01

public static Drawable resizeImage(Bitmap bitmap, int w, int h) {
02  
03                // load the origial Bitmap
04                Bitmap BitmapOrg = bitmap;
05  
06                int width = BitmapOrg.getWidth();
07                int height = BitmapOrg.getHeight();
08                int newWidth = w;
09                int newHeight = h;
10  
11                // calculate the scale
12                float scaleWidth = ((float) newWidth) / width;
13                float scaleHeight = ((float) newHeight) / height;
14  
15                // create a matrix for the manipulation
16                Matrix matrix = new Matrix();
17                // resize the Bitmap
18                matrix.postScale(scaleWidth, scaleHeight);
19                // if you want to rotate the Bitmap
20                // matrix.postRotate(45);
21  
22                // recreate the new Bitmap
23                Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
24                                height, matrix, true);
25  
26                // make a Drawable from Bitmap to allow to set the Bitmap
27                // to the ImageView, ImageButton or what ever
28                return new BitmapDrawable(resizedBitmap);
29  
30        }
 
 
 
 
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是缩小
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值