设置imageView正方形高宽

时间:2023-12-25 11:47:25
private void initWidth() {
int screenWidth = ((MyApplication)getApplication()).screenWidth;
if(0 == screenWidth){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
((MyApplication)this.getApplication()).screenWidth = dm.widthPixels;
screenWidth = dm.widthPixels; //得到宽度
}
imageViewWidth = (screenWidth - (3*CommonUtil.dip2px(ProductListActivity.this, 10)))/2;
}
//重新设置图像宽高
ViewGroup.LayoutParams params = holder.imgIconUrl0.getLayoutParams();
params.width = imageViewWidth;
params.height = imageViewWidth;
holder.imgIconUrl0.setLayoutParams(params); ImageLoaderUtil.displayBigImage(products.get(0).getIconUrl(),
holder.imgIconUrl0, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {} @Override
public void onLoadingFailed(String imageUri, View view,FailReason failReason) {} @Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {} @Override
public void onLoadingCancelled(String imageUri, View view) {}
});

原问题描述:我想在activity中改变图片视图的宽高属性,但是我用了一个方法后没有实现,应该要怎么实现呢?

解决方案:

在这段代码中,创建了一个新的实例并且设置了尺寸:

  1. // SET THE IMAGEVIEW DIMENSIONS
  2. int dimens = 120;
  3. float density = activity.getResources().getDisplayMetrics().density;
  4. int finalDimens = (int)(dimens * density);
  5. LinearLayout.LayoutParams imgvwDimens =
  6. new LinearLayout.LayoutParams(finalDimens, finalDimens);
  7. imgAlbumPhoto.setLayoutParams(imgvwDimens);
  8. // SET SCALETYPE
  9. imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
  10. // SET THE MARGIN
  11. int dimensMargin = 5;
  12. float densityMargin = activity.getResources().getDisplayMetrics().density;
  13. int finalDimensMargin = (int)(dimensMargin * densityMargin);
  14. LinearLayout.LayoutParams imgvwMargin =
  15. new LinearLayout.LayoutParams(finalDimens, finalDimens);
  16. imgvwMargin.setMargins
  17. (finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDim
  18. ensMargin);

改变已存在的ImageView尺寸,使用这个:

    1. coupon_img.setLayoutParams(new LayoutParams(100, 100));