【问题标题】:How to set selected area of Image to ImageView如何将图像的选定区域设置为 ImageView
【发布时间】:2014-02-11 12:08:58
【问题描述】:

我试图将图像从外部存储设置为 imageimageviewview,我已经完成了,但问题是它将整个图像设置为 imageview,我只想从中设置选定的正方形区域图片。 Just life facebook 提供了设置个人资料图片的功能。 有谁能帮我做吗?? 以下是我想要做的示例..

【问题讨论】:

  • 你的意思是要裁剪图像?
  • 将此与您的相机意图一起使用 - photoPickerIntent.putExtra("crop", "true");
  • 我不想修改原图,只想把选中区域设置成imageview
  • 使用刻度类型“矩阵”
  • @pskink 你能用小例子解释一下吗?我不知道该怎么做..

标签: android imageview


【解决方案1】:

类似这样的:

public static Bitmap cropBitmapToSquare(Bitmap bmp) {

    System.gc();
    Bitmap result = null;
    int height = bmp.getHeight();
    int width = bmp.getWidth();
    if (height <= width) {
        result = Bitmap.createBitmap(bmp, (width - height) / 2, 0, height,
                height);
    } else {
        result = Bitmap.createBitmap(bmp, 0, (height - width) / 2, width,
                width);
    }
    return result;
}

这是一个带有作物活动的示例:

http://khurramitdeveloper.blogspot.ru/2013/07/capture-or-select-from-gallery-and-crop.html

【讨论】:

    【解决方案2】:

    实际上我上面关于 setImageMatrix 的评论不是最好的解决方案,试试这个自定义的 Drawable(不需要分配任何临时位图):

    class CropDrawable extends BitmapDrawable {
    
        private Rect mSrc;
        private RectF mDst;
    
        public CropDrawable(Bitmap b, int left, int top, int right, int bottom) {
            super(b);
            mSrc = new Rect(left, top, right, bottom);
            mDst = new RectF(0, 0, right - left, bottom - top);
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.drawBitmap(getBitmap(), mSrc, mDst, null);
        }
    
        @Override
        public int getIntrinsicWidth() {
            return mSrc.width();
        }
    
        @Override
        public int getIntrinsicHeight() {
            return mSrc.height();
        }
    }
    

    和测试代码:

        ImageView iv = new ImageView(this);
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        Drawable d = new CropDrawable(b, 150, 100, 180, 130);
        iv.setImageDrawable(d);
        setContentView(iv);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多