作物可绘制坊 [英] Crop Drawable to Square

查看:96
本文介绍了作物可绘制坊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我已经调整到合适的所需高度(25dp)一个绘制,而宽度按比例缩放。不过,我想它的形象是正方形,25dp的宽度为好。不过,我不希望扭曲的形象,所以我期待裁剪右侧等分离开了绘制的给​​我中心25dpx25dp。这可能吗?怎么样?我一直在努力,在过去两个小时,我对准备认输...


解决方案



位图目标= Bitmap.createBitmap(宽度,高度,Config.ARGB_8888);
帆布帆布=新的Canvas(目标)
浮HSCALE =宽/(浮点)source.getWidth();
浮VSCALE =身高/(浮点)source.getHeight();
浮规模= Math.min(HSCALE,VSCALE);
字模=新的Matrix();
matrix.setScale(秤,秤);
matrix.postTranslate(宽度/ 2 - source.getWidth()/ 2 *刻度,高度/ 2 - source.getHeight()/ 2 *标度);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(源,矩阵,新的油漆());

I have a drawable that I have resized to the appropriate desired height (25dp), and the width is scaled proportionally. However, I want an image that is square, with a width of 25dp as well. However, I do not want to distort the image, so I am looking to to crop equal portions of the right and left of the drawable to give me the center 25dpx25dp. Is this possible? How? I have been trying for the past two hours and I'm about ready to throw in the towel...

解决方案


Bitmap target= Bitmap.createBitmap(width, height,Config.ARGB_8888);
Canvas canvas = new Canvas(target) 
float hScale = width/(float)source.getWidth();
float vScale = height/(float)source.getHeight();
float scale = Math.min(hScale, vScale);
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
matrix.postTranslate(width/2 - source.getWidth()/2 * scale, height/2 - source.getHeight()/2 * scale);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(source, matrix, new Paint());

这篇关于作物可绘制坊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆