图片等比例缩放,从0.0左边开始剪切 保证图片不变形 /** * @Description * @Created by YCH on 2015/11/9. */ public class CropImageView extends ImageView { /** * 画布的宽度 */ private int width; /** * 画布的高度 */ private int height; float scaleWidth = 1.0f; float scaleHeight = 1.0f; public CropImageView(Context context) { super(context); } public CropImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); width = canvas.getWidth(); height = canvas.getHeight(); Drawable drawable = getDrawable(); if (drawable != null) { Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); if(bitmap != null){ int bmWidth = bitmap.getWidth(); int bmHeight = bitmap.getHeight(); float scale = 1.0f; float scaleBitmap = (float)bmWidth/(float)bmHeight;//图片宽高比 float scaleContainer = (float)width/(float)height;//容器宽高比 //如果容器的比例大于图片的比例 if(scaleContainer > scaleBitmap){ //根据宽度进行计算 scale = (float) width / (float) bmWidth; }else { //根据高度进行计算 scale = (float) height / (float) bmHeight; } Matrix matrix = new Matrix(); matrix.postScale(scaleWidth * scale, scaleHeight * scale); Rect rect = new Rect(); rect.left = 0; rect.top = 0; rect.right = width; rect.bottom = height; Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bmWidth, bmHeight, matrix, true); canvas.drawBitmap(bm, rect, rect, null); } } } }
相关推荐
android自定义ImageView实现旋转动画
自定义ImageView,实现指定任意角为圆角,可以用Glide实现,上面两个为圆角,下面两个为直角
自定义ImageView实现图片的缩放功能源码,具体实现细节见博文:http://blog.csdn.net/l1028386804/article/details/50062231
Android实现圆形、圆角、椭圆自定义ImageView,使用Xfermode渲染模式渲染图形实现的,代码有注释,读起来通俗易懂,有需要的可以下载哈 Android实现圆形、圆角、椭圆自定义ImageView,使用Xfermode渲染模式渲染图形...
该自定义ImageView除了可以显示png图片,还可以显示gif图片的动画效果
android自定义ImageView实现缩放,回弹效果。
这是我找了半天的demo,自定义的ImageView从网络获取图片在显示到手机上缩放,再改成适合自己的缩放图片,公布给需要找图片缩放功能的小伙伴。
android自定义ImageView,简单的一个自定义View的demo,该view有圆形和圆角矩形两种。
width=100%就可以使得图片宽度充满屏幕,高度自适应
觉得这个自定义的imageview来实现图片放大缩小很好用 性能不错 所以拿出来分享给大家
可拖动、拉伸、压缩、的imageview
android自定义ImageView,圆角和圆形,抗锯齿.亲测可用。直接拷贝代码就行。
自定义ImageView及竖向文字
自定义ImageView 封装(jar): 图片放大缩小,自由移动等实例
自定义控件,自定义ImageView点击反馈效果
Google官方带旋转动画的自定义ImageView,绝对完爆别的代码,淡入淡出的一个效果。感兴趣的朋友可以下载看看~
CaptchaImageView:自定义ImageView生成验证码图片
自定义ImageView控件,具有缩放和拖拽功能,适合图片的浏览,方便复用