一、imageview設置圖片位置
在實際項目中,有時候需要把圖片放置在指定的位置,此時我們可以使用RelativeLayout或FrameLayout等Layout進行布局,再通過設置imageview控件的布局參數來實現位置的設定。
首先,在布局文件中定義好imageview控件,並設置它的id:
<ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image" />
然後,在代碼中獲取該控件,在設置布局參數時通過Margin來指定圖片位置。
ImageView imageView = findViewById(R.id.image_view); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); imageView.setLayoutParams(layoutParams);
其中leftMargin、topMargin、rightMargin、bottomMargin為要設置的上下左右邊距。
二、imageview設置圖片大小
有時候我們需要對顯示的圖片進行縮放,此時可以設置圖片控件的布局參數來實現。
首先,在布局文件中定義好imageview控件,並設置它的id:
<ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image" />
然後,在代碼中獲取該控件,在設置布局參數時通過width和height來指定圖片大小。
ImageView imageView = findViewById(R.id.image_view); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWidth, imageHeight); imageView.setLayoutParams(layoutParams);
其中imageWidth和imageHeight為要設置的圖片寬度和高度值。
三、imageview保存圖片標尺
有時候我們需要對選中的圖片進行保存,此時可以利用圖片控件的getDrawingCache()方法來獲取圖片,並進行保存操作。
首先,在布局文件中定義好imageview控件,並設置它的id:
<ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image" />
然後,在代碼中獲取該控件,設置允許獲取DrawingCache,並通過ByteArrayOutputStream將DrawingCache轉化為byte數組。
ImageView imageView = findViewById(R.id.image_view); imageView.setDrawingCacheEnabled(true); imageView.buildDrawingCache(); Bitmap bitmap = imageView.getDrawingCache(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray();
最後,可以將byte數組轉化為輸出流,進行文件保存操作。
四、imagej設置切片選取
有時候我們需要對一張大圖進行分割顯示,此時可以使用imagej進行選取切片的操作。
首先,將需要進行分割的大圖導入到imagej中,選擇”Image”->”Type”->”8-bit”,轉化圖像類型為8-bit。
然後,通過selection tool進行選擇切片的區域,選擇完畢後通過「Edit」菜單中的「Copy」或「Ctrl+C」將選中區域複製。
最後,選擇「File」菜單中的「New」->「Image」->”From Clipboard”,即可將選中切片區域導出成為一個新的圖像。
完整代碼示例
1. imageview設置圖片位置:
ImageView imageView = findViewById(R.id.image_view); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin); imageView.setLayoutParams(layoutParams);
2. imageview設置圖片大小:
ImageView imageView = findViewById(R.id.image_view); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWidth, imageHeight); imageView.setLayoutParams(layoutParams);
3. imageview保存圖片標尺:
ImageView imageView = findViewById(R.id.image_view); imageView.setDrawingCacheEnabled(true); imageView.buildDrawingCache(); Bitmap bitmap = imageView.getDrawingCache(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray();
4. imagej設置切片選取:
詳見imagej官網的相關文檔和教程。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/154720.html