【问题标题】:Crop Drawable to Square将可绘制对象裁剪为正方形
【发布时间】:2012-03-04 12:06:31
【问题描述】:

我有一个已调整为所需高度 (25dp) 的可绘制对象,并且宽度按比例缩放。但是,我想要一个正方形的图像,宽度也为 25dp。但是,我不想扭曲图像,所以我希望裁剪可绘制对象的左右相等部分,以给我中心 25dpx25dp。这可能吗?如何?过去两个小时我一直在努力,我准备认输...

【问题讨论】:

    标签: android image drawable crop


    【解决方案1】:
    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());

    【讨论】:

    • 我对在画布上绘图非常陌生。绘制后如何将其添加到我的布局中?
    • 如果你使用 ImageView 那么 imageView.setImageBitmap(target);developer.android.com/reference/android/widget/…
    • 我能以某种方式将它作为复合可绘制对象添加到文本视图中吗?非常感谢你给我的帮助!
    • 如果要用于setBackgroundDrawable(),可以使用setBackgroundDrawable(new BitmapDrawable(target));
    • 我也尝试了你的代码,但我无法绘制画布是什么问题
    猜你喜欢
    • 1970-01-01
    • 2011-05-23
    • 2011-05-30
    • 2017-06-13
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多