android imageview 获取bitmap缩放大小,android – Imageview缩放方法“centercrop”作为代码...
我試圖找出當它縮放圖像時,
android正在做什么,特別是“centercrop”類型.所以要找到答案,我搜索了
ImageView源代碼,并找到它為
here.
所以我試過的是這段代碼:
public Bitmap buildBluredBoxBackground () {
int [] screenSize = Utilities.getScreenSize(mainActivityContext); //screensize[0] = x and [1] is y
Matrix mDrawMatrix = new Matrix();
Bitmap bitmap = ((BitmapDrawable)fullscreenViewHolder.imageViewArt.getDrawable()).getBitmap();
float scale;
float dx = 0, dy = 0;
if (bitmap.getWidth() * screenSize[1] > screenSize[0] * bitmap.getHeight()) {
scale = (float) screenSize[1] / (float) bitmap.getHeight();
dx = (screenSize[0] - bitmap.getWidth() * scale) * 0.5f;
} else {
scale = (float) screenSize[0] / (float) bitmap.getWidth();
dy = (screenSize[1] - bitmap.getHeight() * scale) * 0.5f;
}
mDrawMatrix.setScale(scale, scale);
mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy));
result = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),mDrawMatrix,true);
... //Some processing work
return result;
}
但是它并沒有給我相同的結果.我究竟做錯了什么 ?
舉個例子:
原始圖片
原始ImageView中心裁剪
試用代碼
編輯:
ImageView的XML
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewFullscreenArt"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewFullscreenArtBluredBox"/>
所以我的ImageView是全屏的.這就是為什么Im使用screenSize來處理它.
代碼如何應用它
Bitmap bluredBoxBackground = buildBluredBoxBackground();
imageViewBluredBox.setImageDrawable(new BitmapDrawable(getResources(),bluredBoxBackground));
詳細說明:
我只是試圖獲得與ImageView.setScaleType(ScaleType.CENTER_CROP)相同的效果.所以我的代碼應該像原來的setScaleType方法一樣.為什么我需要它作為代碼?因為在我的情況下,我無法獲取我的ImageView的繪圖,但我必須處理&以某種方式編輯它
總結
以上是生活随笔為你收集整理的android imageview 获取bitmap缩放大小,android – Imageview缩放方法“centercrop”作为代码...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [html] 页面刷新时session
- 下一篇: 保持危机感和紧迫感_什么是紧迫的:您需要