2011-11-25 48 views
2

我喜欢调整位图的大小,如果它很大,使它变小并将其放置在表面视图中的特定位置,则需要获取设备宽度和高度,然后获取位图大小并将它们放置在表面视图,然后采取另一个图像调整大小,并将其放置在任何我喜欢的位置。 如何首先知道位置坐标(放置第二个图像 - 如果更大/更小的屏幕布局不应该改变,则应该与设备无关)Android Image Resize basic

以及如何缩放大位图并设置为背景,以便我可以绘制图像在它上面。 我的代码:

final int canvasWidth = getWidth(); 
final int canvasHeight = getHeight(); 

int imageWidth = img.getWidth(); 
int imageHeight = img.getHeight(); 

float scaleFactor = Math.min((float)canvasWidth/imageWidth, 
           (float)canvasHeight/imageHeight); 
Bitmap scaled = Bitmap.createScaledBitmap( img, 
              (int)(scaleFactor * imageWidth), 
              (int)(scaleFactor * imageHeight), 
              true); 
canvas.drawColor(Color.BLACK); 
canvas.drawBitmap(scaled, 10, 10, null); 

这种规模的大图像,但它不适合整个屏幕“IMG - 位图就像是一个背景图像”

有人能帮助我了解调整大小的基础知识(我是新的,因此难以理解调整大小)来调整图像的大小以适应屏幕,并将任何图像调整为较小的图像并将其放置在我喜欢的任何位置。

回答

1

Store中的位图作为操作的来源和使用ImageView的显示出来:

Bitmap realImage = BitmapFactory.decodeFile(filePathFromActivity.toString()); 

Bitmap newBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true); 


imageView.setImageBitmap(newBitmap); 


//scale down method 
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, 
     boolean filter) { 
    float ratio = Math.min(
      (float) maxImageSize/realImage.getWidth(), 
      (float) maxImageSize/realImage.getHeight()); 
    int width = Math.round((float) ratio * realImage.getWidth()); 
    int height = Math.round((float) ratio * realImage.getHeight()); 

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, 
      height, filter); 
    return newBitmap; 
} 

,并设置您的ImageView的宽度和高度,以“fill_parent”。

+0

Thx的帮助,但我得到了ForceQuit消息,因为我试图在表面视图中绘制图像,我已经有几张图像 – optimus