Android从setImageBitmap缩放imageview

Tom*_*Tom 10 android android-image android-imageview

我有这个代码:

<ImageView
android:id="@+id/listitem_logo"
android:layout_width="match_parent"                                   
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

imageview_logo.setImageBitmap(bit); // comes from assets
imageview_logo.setAdjustViewBounds(true);        
imageview_logo.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview_logo.setBackgroundColor(0x00000000); 
imageview_logo.setPadding(0, 0, 0, 0);
imageview_logo.setVisibility(v.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

以这种方式加载图像时,似乎没有进行缩放.但是,如果我通过setImageDrawable()r.res加载图像.里面的图像ImageView调整大小.但是,我需要使用setImageBitmap(),因为我通过assets文件夹加载我的图像.

我在这里错过了一些设置吗?这将使Android调整大小并缩放位图,因此它使用ImageView的全宽度?我想我可以自己在代码中做到这一点,但是我会想通过设置一些属性来支持我想做的事情.

Aas*_*gar 13

不能信任系统,特别是有时候表现得非常不同的android.您可以调整位图的大小.

height = (Screenwidth*originalHeight)/originalWidth;
Run Code Online (Sandbox Code Playgroud)

这将产生适当的高度.如上所述,width等于屏幕宽度.

Bitmap pq=Bitmap.createScaledBitmap(pq,Screenwidth,height, true);
Run Code Online (Sandbox Code Playgroud)


nar*_*rko 7

由于我需要一些时间来弄清楚完成这项工作的完整代码,我在这里依靠前面的答案发布了一个完整的例子:

   ImageView bikeImage = (ImageView) view.findViewById(R.id.bikeImage);
   AssetLoader assetLoader = new AssetLoader(getActivity());
   Bitmap bitmap = assetLoader.getBitmapFromAssets(
                Constants.BIKES_FOLDER + "/" + bike.getName().toLowerCase()
                        .replace(' ', '_').replace('-', '_') + ".png");
   int width = getActivity().getResources().getDisplayMetrics().widthPixels;
   int height = (width*bitmap.getHeight())/bitmap.getWidth();
   bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
   bikeImage.setImageBitmap(bitmap);
Run Code Online (Sandbox Code Playgroud)