Android 圖片Bitmap的剪切的示例代碼
一、什么是Android中的Bitmap
Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進(jìn)行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件。
二、Bitmap的剪切基本操作
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
從原始位圖剪切圖像,這是一種高級(jí)的方式。可以用Matrix(矩陣)來實(shí)現(xiàn)旋轉(zhuǎn)等高級(jí)方式截圖
參數(shù)說明:
Bitmap source:要從中截圖的原始位圖
int x:起始x坐標(biāo)
int y:起始y坐標(biāo)
int width:要截的圖的寬度
int height:要截的圖的寬度
Bitmap.Config config:一個(gè)枚舉類型的配置,可以定義截到的新位圖的質(zhì)量
返回值:返回一個(gè)剪切好的Bitmap
三、Bitmap剪切的封裝
實(shí)際使用中,因?yàn)轫?xiàng)目需要時(shí)常需要對(duì)基本功能進(jìn)行封裝,下面是一段封裝的代碼,僅供參考。
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
public class BitmapCut
{
/**
* 通過資源id轉(zhuǎn)化成Bitmap
*
* @param context
* @param resId
* @return
*/
public static Bitmap ReadBitmapById(Context context, int resId)
{
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
/**
* 設(shè)置背景為圓角
*
* @param bitmap
* @param pixels
* @return
*/
public static Bitmap removeYuanjiao(Bitmap bitmap, int pixels)
{
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap creBitmap = Bitmap.createBitmap(width, height,
android.graphics.Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(creBitmap);
Paint paint = new Paint();
float roundPx = pixels;
RectF rectF = new RectF(0, 0, bitmap.getWidth() - pixels,
bitmap.getHeight() - pixels);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
if (!bitmap.isRecycled())
bitmap.recycle();
return creBitmap;
}
/**
* 按正方形裁切圖片
*/
public static Bitmap ImageCrop(Bitmap bitmap, boolean isRecycled)
{
if (bitmap == null)
{
return null;
}
int w = bitmap.getWidth(); // 得到圖片的寬,高
int h = bitmap.getHeight();
int wh = w > h ? h : w;// 裁切后所取的正方形區(qū)域邊長
int retX = w > h ? (w - h) / 2 : 0;// 基于原圖,取正方形左上角x坐標(biāo)
int retY = w > h ? 0 : (h - w) / 2;
Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null,
false);
if (isRecycled && bitmap != null && !bitmap.equals(bmp)
&& !bitmap.isRecycled())
{
bitmap.recycle();
bitmap = null;
}
// 下面這句是關(guān)鍵
return bmp;// Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null,
// false);
}
/**
* 按長方形裁切圖片
*
* @param bitmap
* @return
*/
public static Bitmap ImageCropWithRect(Bitmap bitmap)
{
if (bitmap == null)
{
return null;
}
int w = bitmap.getWidth(); // 得到圖片的寬,高
int h = bitmap.getHeight();
int nw, nh, retX, retY;
if (w > h)
{
nw = h / 2;
nh = h;
retX = (w - nw) / 2;
retY = 0;
} else
{
nw = w / 2;
nh = w;
retX = w / 4;
retY = (h - w) / 2;
}
// 下面這句是關(guān)鍵
Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,
false);
if (bitmap != null && !bitmap.equals(bmp) && !bitmap.isRecycled())
{
bitmap.recycle();
bitmap = null;
}
return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,
// false);
}
/**
* Bitmap --> byte[]
*
* @param bmp
* @return
*/
public static byte[] readBitmap(Bitmap bmp)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 60, baos);
try
{
baos.flush();
baos.close();
} catch (IOException e)
{
e.printStackTrace();
}
return baos.toByteArray();
}
/**
* 將圖像裁剪成圓形
*
* @param bitmap
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap)
{
if (bitmap == null)
{
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height)
{
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else
{
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
if (bitmap != null && !bitmap.isRecycled())
{
bitmap.recycle();
bitmap = null;
}
return output;
}
// 將圖片變成帶圓邊的圓形圖片
public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height)
{
if (bitmap == null)
{
return null;
}
// 將圖片變成圓角
Bitmap roundBitmap = Bitmap.createBitmap(width, height,
Config.ARGB_8888);
Canvas canvas = new Canvas(roundBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
int len = (width > height) ? height : width;
canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true);
canvas.drawBitmap(scaledBitmap, 0, 0, paint);
// 將圖片加圓邊
Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
canvas = new Canvas(outBitmap);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xffffffff);
canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
canvas.drawBitmap(roundBitmap, 0, 0, paint);
bitmap.recycle();
bitmap = null;
roundBitmap.recycle();
roundBitmap = null;
scaledBitmap.recycle();
scaledBitmap = null;
return outBitmap;
}
// 將圖片變成帶圓邊的圓形圖片
public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height,
int color)
{
if (bitmap == null)
{
return null;
}
// 將圖片變成圓角
Bitmap roundBitmap = Bitmap.createBitmap(width, height,
Config.ARGB_8888);
Canvas canvas = new Canvas(roundBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
int len = (width > height) ? height : width;
canvas.drawCircle(width / 2, height / 2, len / 2 - 8, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len, true);
canvas.drawBitmap(scaledBitmap, 0, 0, paint);
// 將圖片加圓邊
Bitmap outBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
canvas = new Canvas(outBitmap);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
canvas.drawCircle(width / 2, height / 2, len / 2 - 4, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
canvas.drawBitmap(roundBitmap, 0, 0, paint);
bitmap.recycle();
bitmap = null;
roundBitmap.recycle();
roundBitmap = null;
scaledBitmap.recycle();
scaledBitmap = null;
return outBitmap;
}
/**
* function:圖片轉(zhuǎn)圓角
*
* @param bitmap
* 需要轉(zhuǎn)的bitmap
* @param pixels
* 轉(zhuǎn)圓角的弧度
* @return 轉(zhuǎn)圓角的bitmap
*/
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels)
{
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
if (bitmap != null && !bitmap.isRecycled())
{
bitmap.recycle();
}
return output;
}
/**
* 獲取指定的圓角圖片
*
* @param bitmap
* @return
*/
public static Bitmap getRadiusBitmap(Bitmap bitmap)
{
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xffffffff);
Bitmap radiusBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(radiusBitmap);
RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawRoundRect(rectF, 7, 7, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
if (bitmap != null && !bitmap.isRecycled())
{
bitmap.recycle();
}
return radiusBitmap;
}
// 獲得指定大小的圓邊的bitmap數(shù)組
public static ArrayList<Bitmap> getRadiusBitmapList(String[] pathArray,
int size, int len, float radius, int color)
{
Bitmap canvasBitmap = null;
Canvas canvas = null;
Paint paint = null;
RectF rectF = new RectF(0, 0, len - radius, len - radius);
File file = null;
FileInputStream fis = null;
Bitmap bitmap = null;
Bitmap scaledBitmap = null;
ArrayList<Bitmap> list = new ArrayList<Bitmap>();
for (int i = 0; i < pathArray.length; i++)
{
file = new File(pathArray[i]);
if (!file.exists())
continue;
try
{
fis = new FileInputStream(file);
bitmap = BitmapFactory.decodeStream(fis);
if (bitmap != null)
{
canvasBitmap = Bitmap.createBitmap(len, len,
Config.ARGB_8888);
canvas = new Canvas(canvasBitmap);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
canvas.drawRoundRect(rectF, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
scaledBitmap = Bitmap.createScaledBitmap(bitmap, len, len,
true);
canvas.drawBitmap(scaledBitmap, 0, 0, paint);
list.add(canvasBitmap);
}
} catch (FileNotFoundException e)
{
} finally
{
if (fis != null)
{
try
{
fis.close();
} catch (IOException e1)
{
}
}
}
if (list.size() == size)
break;
}
if (scaledBitmap != null && !scaledBitmap.isRecycled())
{
scaledBitmap.recycle();
scaledBitmap = null;
}
if (bitmap != null && !bitmap.isRecycled())
{
bitmap.recycle();
bitmap = null;
}
return list;
}
/**
* 按照一定的寬高比例裁剪圖片
*
* @param bitmap
* @param num1
* 長邊的比例
* @param num2
* 短邊的比例
* @return
*/
public static Bitmap ImageCrop(Bitmap bitmap, int num1, int num2,
boolean isRecycled)
{
if (bitmap == null)
{
return null;
}
int w = bitmap.getWidth(); // 得到圖片的寬,高
int h = bitmap.getHeight();
int retX, retY;
int nw, nh;
if (w > h)
{
if (h > w * num2 / num1)
{
nw = w;
nh = w * num2 / num1;
retX = 0;
retY = (h - nh) / 2;
} else
{
nw = h * num1 / num2;
nh = h;
retX = (w - nw) / 2;
retY = 0;
}
} else
{
if (w > h * num2 / num1)
{
nh = h;
nw = h * num2 / num1;
retY = 0;
retX = (w - nw) / 2;
} else
{
nh = w * num1 / num2;
nw = w;
retY = (h - nh) / 2;
retX = 0;
}
}
Bitmap bmp = Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,
false);
if (isRecycled && bitmap != null && !bitmap.equals(bmp)
&& !bitmap.isRecycled())
{
bitmap.recycle();
bitmap = null;
}
return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,
// false);
}
}
示例代碼:Bitmaptest_jb51.rar
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android完美實(shí)現(xiàn) 拍照 選擇圖片 剪裁等代碼分享
本文給大家分享了2個(gè)安卓實(shí)現(xiàn)實(shí)現(xiàn) 拍照 選擇圖片 剪裁等的代碼,都是從正式項(xiàng)目中提取出來了,非常實(shí)用,有需要的小伙伴可以參考下。2016-01-01
Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android自定義滑動(dòng)接聽電話控件組實(shí)例
這篇文章主要介紹了Android自定義滑動(dòng)接聽電話控件組,接聽電話可以左右滑動(dòng),感興趣的小伙伴們可以參考一下。2016-10-10
Android將圖片上傳到php服務(wù)器的實(shí)例代碼
這篇文章主要介紹了Android將圖片上傳到php服務(wù)器的實(shí)例代碼,需要的朋友可以參考下2017-07-07
Android之RecycleView實(shí)現(xiàn)指定范圍的拖動(dòng)效果
這篇文章主要介紹了Android之RecycleView實(shí)現(xiàn)指定范圍的拖動(dòng)效果的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Flutter質(zhì)感設(shè)計(jì)之列表項(xiàng)
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之列表項(xiàng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
安卓 獲取手機(jī)IP地址的實(shí)現(xiàn)代碼
本篇文章主要介紹 Android 4.0 獲取手機(jī)IP地址的方法,附有實(shí)現(xiàn)代碼,具有參考價(jià)值,希望對(duì)有需要的小伙伴有幫助2016-07-07
Android中實(shí)現(xiàn)圓角圖片的幾種方法
本篇文章主要介紹了Android中實(shí)現(xiàn)圓角圖片的幾種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Android ListView用EditText實(shí)現(xiàn)搜索功能效果
本篇文章主要介紹了Android ListView用EditText實(shí)現(xiàn)搜索功能效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

