使用ImageView來實作,
不過使用下面的程式碼,
圖片始終沒有達到放大的效果:
Bitmap image = null;
Bitmap resizedBitmap= null;
int w = imageW;
int h = imageH;
image = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size());
Matrix matrix = new Matrix();
matrix.postScale(2.0f, 2.0f);
resizedBitmap = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
//imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageBitmap(resizedBitmap);
xml大致如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scale_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView android:id="@+id/spotlight"
android:layout_width="320dp"
android:layout_height="180dp"
android:scaleType="fitCenter" //這行也要拿掉,不必要.
android:adjustViewBounds="true"
android:layout_alignLeft="@id/mainView"
/>
</RelativeLayout>
最後發現需要加上:
imageView.setScaleType(ImageView.ScaleType.CENTER);
如此才會按圖片的size居中顯示,
當圖片長/寬超過View的長/寬,
則截取圖片的居中部分顯示.
(圖晚點補上好了
其他相關的scale方式還有:
Enum Values | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ImageView.ScaleType | CENTER | Center the image in the view, but perform no scaling. | |||||||||
ImageView.ScaleType | CENTER_CROP | Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). | |||||||||
ImageView.ScaleType | CENTER_INSIDE | Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). | |||||||||
ImageView.ScaleType | FIT_CENTER | Scale the image using CENTER . | |||||||||
ImageView.ScaleType | FIT_END | Scale the image using END . | |||||||||
ImageView.ScaleType | FIT_START | Scale the image using START . | |||||||||
ImageView.ScaleType | FIT_XY | Scale the image using FILL . | |||||||||
ImageView.ScaleType | MATRIX | Scale using the image matrix when drawing. |
Ref:ImageView.ScaleType