2015-01-14 143 views
0

在我的应用程序中,我有一个Activity来显示用户配置文件。用户可以通过从设备相机图片或Facebook图片等中选择它来设置其个人资料图片。但是这些图片可能非常大,所以如果它们太大,我需要以某种方式自动缩放pcba。根据显示器尺寸自动调整图像大小

编辑

由于回答我的问题,我试过的东西。我创建了一些辅助方法:

public static int getDisplayWidth(Activity activity) { 
    DisplayMetrics displaymetrics = new DisplayMetrics(); 

    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 

    return displaymetrics.widthPixels; 
} 

public static int getDisplayHeight(Activity activity) { 
    DisplayMetrics displaymetrics = new DisplayMetrics(); 

    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 

    return displaymetrics.widthPixels; 
} 

public static int getDrawableWith(Context context, int id) { 
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id); 

    return bitmap.getWidth(); 
} 

public static int getDrawableHeight(Context context, int id) { 
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), id); 

    return bitmap.getHeight(); 
} 

public static int getHeightOfImage(int originalWidth, int originalHeight, int targetWidth) { 
    double ratio = ((double)originalWidth/(double)originalHeight); 

    return (int)(targetWidth/ratio); 
} 

,我现在尝试创建一个调整大小绘制并将其设置为我的ImageView:

RelativeLayout.LayoutParams layoutParamsView = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 

this.setLayoutParams(layoutParamsView); 

int originalWidth = Helper.getDrawableWith(this.getContext(), R.drawable.background_wave); 
int originalHeight = Helper.getDrawableHeight(this.getContext(), R.drawable.background_wave); 

int targetWidth = Helper.getDisplayWidth((Activity)this.getContext()); 
int targetHeight = Helper.getHeightOfImage(originalWidth, originalHeight, targetWidth); 

RelativeLayout.LayoutParams layoutParamsImageview = new RelativeLayout.LayoutParams(targetWidth, targetHeight); 
layoutParamsImageview.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
layoutParamsImageview.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 

Drawable drawable = Helper.getScaledDrawable(this.getContext(), R.drawable.background_wave, targetWidth, targetHeight); 

this.imageViewBackgroundWave = new ImageView(this.getContext()); 
this.imageViewBackgroundWave.setLayoutParams(layoutParamsImageview); 
this.imageViewBackgroundWave.setImageDrawable(drawable); 

this.addView(this.imageViewBackgroundWave); 

这只是正常工作依赖于显示尺寸调整图像大小。

回答

1

最简单的方法是将Bitmap转换为Drawable用于缩放目的。

How to convert a Bitmap to Drawable in android?

虽然牢记内存(这是你的第一个“解决方案”) - 大图像,即使在“缩放”作为绘制会消耗大量的内存。

+0

谢谢。这是正确的答案,我更新了我的帖子以显示解决方案。 – Mulgard

相关问题