AdjustBitmap

public class AdjustBitmap {

/**

? ?? * 將bitmap調(diào)整到指定大小

? ?? */

? ? public static BitmapsizeBitmap(Bitmap origin, int newWidth, int newHeight) {

if (origin ==null) {

return null;

? ? ? ? }

int height = origin.getHeight();

? ? ? ? int width = origin.getWidth();

? ? ? ? float scaleWidth = ((float) newWidth) / width;

? ? ? ? float scaleHeight = ((float) newHeight) / height;

? ? ? ? Matrix matrix =new Matrix();

? ? ? ? // 使用后乘

? ? ? ? matrix.postScale(scaleWidth, scaleHeight);

? ? ? ? Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);

? ? ? ? if (!origin.isRecycled()) {

//這時候origin還有嗎档插?

? ? ? ? ? ? origin.recycle();

? ? ? ? }

return newBM;

? ? }

/**

*

? ?? * @param origin

? ?? * @param scale

? ?? * @return 按比例縮放

? ?? */

? ? public static BitmapscaleBitmap(Bitmap origin, float scale) {

if (origin ==null) {

return null;

? ? ? ? }

int width = origin.getWidth();

? ? ? ? int height = origin.getWidth();

? ? ? ? Matrix matrix =new Matrix();

? ? ? ? matrix.preScale(scale, scale);

? ? ? ? Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);

? ? ? ? if (newBM.equals(origin)) {

return newBM;

? ? ? ? }

origin.recycle();

? ? ? ? return newBM;

? ? }

/**

*

? ?? * @param bitmap

? ?? * @return 從中間截取一個正方形

? ?? */

? ? public static BitmapcropBitmap(Bitmap bitmap) {

// 得到圖片的寬,高

? ? ? ? int w = bitmap.getWidth();

? ? ? ? int h = bitmap.getHeight();

? ? ? ? // 裁切后所取的正方形區(qū)域邊長

? ? ? ? int cropWidth = w >= h ? h : w;

? ? ? ? return Bitmap.createBitmap(bitmap, 0,

? ? ? ? ? ? ? ? 0, cropWidth, cropWidth);

? ? }

/**

*

? ?? * @param bitmap

? ?? * @param sx 2

? ?? * @param sy 3

? ?? * @return 返回一個截取的圖形

? ?? */

? ? public static BitmapcropBitmap(Bitmap bitmap,int sx,int sy) {

// 得到圖片的寬,高

? ? ? ? int w = bitmap.getWidth();//600 900

? ? ? ? int h = bitmap.getHeight();//900 600

? ? ? ? int startWidth=0;

? ? ? ? int startHeight=0;

? ? ? ? int x=0;

? ? ? ? int y=0;

? ? ? ? int z=0;

? ? ? ? // 裁切后所取的正方形區(qū)域邊長

? ? ? ? if (sx==sy) {

x = w >= h ? h : w;

? ? ? ? ? ? y=x;

? ? ? ? }else if (sx>sy){

//橫 長方型

? ? ? ? ? ? if (w>h){//331:240 3:2

? ? ? ? ? ? ?? z=h/sx;

? ? ? ? ? ? }else {//240:331? 3:2

? ? ? ? ? ? ? ? z=x/sx;

? ? ? ? ? ? }

x=sx*z;

? ? ? ? ? ? y=sy*z;

? ? ? ? }else {

//豎型 長方型

? ? ? ? ? ? if (w>h){//331:240 2:3

? ? ? ? ? ? ? ? z=h/sy;

? ? ? ? ? ? }else {//240:331? 2:3

? ? ? ? ? ? ? ? z=x/sy;

? ? ? ? ? ? }

x=sx*z;

? ? ? ? ? ? y=sy*z;

? ? ? ? }

return Bitmap.createBitmap(bitmap, startWidth,

? ? ? ? ? ? ? ? startHeight, x, y);

? ? }

/**

*

? ?? * @param bitmap

? ?? * @return 把圖片裁剪成圓形

? ?? */

? ? public static BitmapgetCircleBitmap(Bitmap bitmap) {

if (bitmap ==null) {

return null;

? ? ? ? }

//裁剪成正方形

? ? ? ? bitmap =cropBitmap(bitmap);

? ? ? ? try {

Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),

? ? ? ? ? ? ? ? ? ? bitmap.getHeight(), Bitmap.Config.ARGB_8888);

? ? ? ? ? ? Canvas canvas =new Canvas(circleBitmap);

? ? ? ? ? ? final Paint paint =new Paint();

? ? ? ? ? ? final Rect rect =new Rect(0, 0, bitmap.getWidth(),

? ? ? ? ? ? ? ? ? ? bitmap.getHeight());

? ? ? ? ? ? final RectF rectF =new RectF(new Rect(0, 0, bitmap.getWidth(),

? ? ? ? ? ? ? ? ? ? bitmap.getHeight()));

? ? ? ? ? ? float roundPx =0.0f;

? ? ? ? ? ? roundPx = bitmap.getWidth();

? ? ? ? ? ? paint.setAntiAlias(true);

? ? ? ? ? ? canvas.drawARGB(0, 0, 0, 0);

? ? ? ? ? ? paint.setColor(Color.WHITE);

? ? ? ? ? ? canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

? ? ? ? ? ? paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

? ? ? ? ? ? final Rect src =new Rect(0, 0, bitmap.getWidth(),

? ? ? ? ? ? ? ? ? ? bitmap.getHeight());

? ? ? ? ? ? canvas.drawBitmap(bitmap, src, rect, paint);

? ? ? ? ? ? return circleBitmap;

? ? ? ? }catch (Exception e) {

return bitmap;

? ? ? ? }

}

public static BitmapdrawableToBitmap(Drawable drawable) {

// 取 drawable 的長寬

? ? ? ? int w = drawable.getIntrinsicWidth();

? ? ? ? int h = drawable.getIntrinsicHeight();

? ? ? ? // 取 drawable 的顏色格式

? ? ? ? Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888

? ? ? ? ? ? ? ? : Bitmap.Config.RGB_565;

? ? ? ? // 建立對應(yīng)bitmap

? ? ? ? Bitmap bitmap = Bitmap.createBitmap(w, h, config);

? ? ? ? // 建立對應(yīng) bitmap 的畫布

? ? ? ? Canvas canvas =new Canvas(bitmap);

? ? ? ? drawable.setBounds(0, 0, w, h);

? ? ? ? // 把 drawable 內(nèi)容畫到畫布中

? ? ? ? drawable.draw(canvas);

? ? ? ? return bitmap;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子肢专,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件芥丧,死亡現(xiàn)場離奇詭異,居然都是意外死亡坊罢,警方通過查閱死者的電腦和手機娄柳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來艘绍,“玉大人赤拒,你說我怎么就攤上這事∮站希” “怎么了挎挖?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長航夺。 經(jīng)常有香客問我蕉朵,道長,這世上最難降的妖魔是什么阳掐? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任始衅,我火速辦了婚禮冷蚂,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘汛闸。我一直安慰自己蝙茶,他們只是感情好,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布诸老。 她就那樣靜靜地躺著隆夯,像睡著了一般。 火紅的嫁衣襯著肌膚如雪别伏。 梳的紋絲不亂的頭發(fā)上蹄衷,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機與錄音厘肮,去河邊找鬼愧口。 笑死,一個胖子當著我的面吹牛类茂,可吹牛的內(nèi)容都是我干的调卑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼大咱,長吁一口氣:“原來是場噩夢啊……” “哼恬涧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起碴巾,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤溯捆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后厦瓢,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體提揍,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年煮仇,在試婚紗的時候發(fā)現(xiàn)自己被綠了劳跃。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡浙垫,死狀恐怖刨仑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情夹姥,我是刑警寧澤杉武,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站辙售,受9級特大地震影響轻抱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜旦部,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一祈搜、第九天 我趴在偏房一處隱蔽的房頂上張望较店。 院中可真熱鬧,春花似錦容燕、人聲如沸梁呈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽捧杉。三九已至陕见,卻和暖如春秘血,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背评甜。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工灰粮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人忍坷。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓粘舟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親佩研。 傳聞我的和親對象是個殘疾皇子柑肴,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容