在imageView中显示位图的特定区域



这是我的问题,我在ListView中显示一些来自web的图片。我想在我的ImageView中只显示我的图片的特定区域,而不调整它的大小。

下面是一个具体的例子:https://docs.google.com/file/d/0B6J9AVdWXjwuWld5dU5CWXFSTjQ/edit?usp=sharing

我认为用位图方法是可能的,但是我真的不知道怎么做…

谢谢你的提示

是的,这样做的方法是在源图像中创建一个位图对象。下面是一个例子:

File thumbFile = new File(thumbFileDir, imageName);
    //Bitmap source = your image
    Bitmap target= Bitmap.createBitmap(thumbSize, thumbSize,Config.ARGB_8888); //in my case the thumb image is square, use your dimensions instead of "thumbSize, thumbSize"
    Canvas canvas = new Canvas(target);
    float hScale = thumbSize/(float)source.getWidth(); //again, thumb dimensions here
    float vScale = thumbSize/(float)source.getHeight(); //and here
    float scale = Math.max(hScale, vScale);
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    matrix.postTranslate(thumbSize/2 - source.getWidth()/2 * scale, thumbSize/2 - source.getHeight()/2 * scale); //and here
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(source, matrix, new Paint());

"target"对象是你所要求的

最新更新