自定義各種裁剪框 掃瞄框

2021-07-04 10:03:19 字數 3094 閱讀 2884

由於專案的需要,我們需要三種裁剪框,矩形裁剪框相信大家都不陌生,矩形的比較簡單,思路:例如:正方形的裁剪框,我們只需要設定畫筆的顏色為半透明的,然後繪製上下左右四個矩形即可組成乙個正方形,那麼圓形的怎麼畫呢?如果是同樣的思路,那麼我們就需要畫兩個不規則的圖形,來組成圓,關鍵的地方就是那個半圓弧怎麼畫?我想到了用path,下面大家看一下效果:

正方形:

圓形:

帶角標的矩形:

下面看實現**:

/**

* 繪製裁剪框

** @param canvas

*/private void drawshape(canvas canvas)

}

如果對path不是很理解的:這裡有一篇文章寫的超級贊:

path繪製

**

* 獲得螢幕相關的輔助類

*/public

class

screenutils

/*** 獲得螢幕寬度

**@param context

*@return

*/public

static

intgetscreenwidth(context context)

/*** 獲得螢幕高度

**@param context

*@return

*/public

static

intgetscreenheight(context context)

/*** 獲得狀態列的高度

**@param context

*@return

*/public

static

intgetstatusheight(context context) catch (exception e)

return statusheight;

}/**

* 獲取當前螢幕截圖,包含狀態列

**@param activity

*@return

*/public

static bitmap snapshotwithstatusbar(activity activity)

/*** 獲取當前螢幕截圖,不包含狀態列

**@param activity

*@return

*/public

static bitmap snapshotwithoutstatusbar(activity activity)

}

下面的具體的實現:使用到bitmap的creatbitmap:

/**

* 獲取裁剪

**@param activity

*@return

*/public

static bitmap takescreenshot(activity activity)

return comp(b);

}

public

static bitmap comp(bitmap image)

bytearrayinputstream isbm = new bytearrayinputstream(baos.tobytearray());

bitmapfactory.options newopts = new bitmapfactory.options();

//開始讀入,此時把options.injustdecodebounds 設回true了

newopts.injustdecodebounds = true;

bitmap bitmap = bitmapfactory.decodestream(isbm, null, newopts);

newopts.injustdecodebounds = false;

int w = newopts.outwidth;

int h = newopts.outheight;

//現在主流手機比較多是800*480解析度,所以高和寬我們設定為

float hh = 800f;//這裡設定高度為800f

float ww = 480f;//這裡設定寬度為480f

//縮放比。由於是固定比例縮放,只用高或者寬其中乙個資料進行計算即可

int be = 1;//be=1表示不縮放

if (w > h && w > ww) else

if (w < h && h > hh)

if (be <= 0)

be = 1;

newopts.insamplesize = be;//設定縮放比例

//重新讀入,注意此時已經把options.injustdecodebounds 設回false了

isbm = new bytearrayinputstream(baos.tobytearray());

bitmap = bitmapfactory.decodestream(isbm, null, newopts);

return compressimage(bitmap);//壓縮好比例大小後再進行質量壓縮

}private

static bitmap compressimage(bitmap image)

bytearrayinputstream isbm = new bytearrayinputstream(baos.tobytearray());//把壓縮後的資料baos存放到bytearrayinputstream中

bitmap bitmap = bitmapfactory.decodestream(isbm, null, null);//把bytearrayinputstream資料生成

return bitmap;

}

就這樣吧。~

自定義搜尋框

自定義搜尋框 1 自定義自定義的文字框必須在 void layoutsubviews中才能自定義 即子控制項必須在layoursubviews中設定,而searchbar可以直接設定 placeholder,外背景 2 storyboard隨意設定即可 3 純 字型顏色和大小無法設定,但是外邊框可以...

自定義彈框

1.警告框 設定乙個negativebutton builder.setnegativebutton 取消 new dialoginte ce.onclicklistener 設定乙個neutralbutton builder.setneutralbutton 忽略 new dialoginte c...

自定義彈出框alert

有時候由於某種需求,我們需要改變彈出框的樣式。例子一 js 如下 function alert txt 例子二 可以正常確定返回。js 如下 window.alert function str alertfram.focus document.body.onselectstart function ...