Android編程之圖片相關(guān)代碼集錦
本文實(shí)例總結(jié)了Android編程之圖片相關(guān)代碼。分享給大家供大家參考,具體如下:
1. Bitmap轉(zhuǎn)化為字符串:
/**
* @param 位圖
* @return 轉(zhuǎn)化成的字符串
*/
public static String bitmapToString(Bitmap bitmap) {
// 將Bitmap轉(zhuǎn)換成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
2.字符串轉(zhuǎn)化為Bitmap:
/**
* @param string 字符串
* @return 轉(zhuǎn)化成的位圖
*/
public static Bitmap stringToBitmap(String string) {
// 將字符串轉(zhuǎn)換成Bitmap類型
Bitmap bitmap = null;
try {
byte[] bitmapArray;
bitmapArray = Base64.decode(string, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
3.Bitmap轉(zhuǎn)化為Drawable:
/**
* @param bitmap Bitmap位圖圖像
* @return Drawable 轉(zhuǎn)換后的Drawable對(duì)象
*/
public static Drawable bitmapToDrawable(Bitmap bitmap) {
if (bitmap == null)
return null;
if (160 != bitmap.getDensity()) {
bitmap.setDensity(160);
}
return new BitmapDrawable(bitmap);
}
根據(jù)圖片資源ID獲取Drawable對(duì)象:
/**
* @param context 上下文
* @param id 圖片的資源ID
* @return Drawable對(duì)象
*/
public static Drawable resourceToDrawable(Context context,int id) {
return null == context ? null : bitmapToDrawable(BitmapFactory.decodeResource(context.getResources(), id));
}
byte數(shù)組轉(zhuǎn)換Drawble對(duì)象:
/**
* @param bytes byte數(shù)組
* @return drawble對(duì)象
*/
public static Drawable byteArrayToDrawable(byte[] bytes) {
return null == bytes ? null : bitmapToDrawable(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}
4.Drawable轉(zhuǎn)化為bitmap:
/**
* Drawble對(duì)象轉(zhuǎn)Bitmap對(duì)象
* @param drawable drawble對(duì)象
* @return bitmap對(duì)象
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
return null == drawable ? null : ((BitmapDrawable) drawable).getBitmap();
}
5.byte數(shù)組轉(zhuǎn)換Bitmap對(duì)象:
/**
* @param bytes byte數(shù)組
* @return bitmap對(duì)象
*/
public static Bitmap byteArrayToBitmap(byte[] bytes) {
return null == bytes ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
6.圖片去色,返回灰度圖片(老式圖片):
/**
* @param bitmap 傳入的bitmap
* @return 去色后的圖片Bitmap對(duì)象
*/
public static Bitmap toGrayscale(Bitmap bitmap) {
int width,height;
height = bitmap.getHeight();
width = bitmap.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
return bmpGrayscale;
}
7.對(duì)圖片進(jìn)行縮放:
/**
* @param url 圖片的路徑
* @param requireSize 縮放的尺寸
* @return 縮放后的圖片Bitmap對(duì)象
*/
public static Bitmap getScaleImage(String url,int requireSize) {
BitmapFactory.Options o = new BitmapFactory.Options();
// 此屬性表示圖片不加載到內(nèi)存,只是讀取圖片的屬性,包括圖片的高寬
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(url, o);
int width_tmp = o.outWidth,height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < requireSize || height_tmp / 2 < requireSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bmp = BitmapFactory.decodeFile(url, o2);
return bmp;
}
8.獲得圖片的倒影,同時(shí)倒影漸變效果:
/**
* @param bitmap 圖片源
* @return 處理后的圖片Bitmap對(duì)象
*/
public static Bitmap createMirro(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int shadow_height = 15;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
// shadow effect
int alpha = 0x00000000;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int index = y * width + x;
int r = (pixels[index] >> 16) & 0xff;
int g = (pixels[index] >> 8) & 0xff;
int b = pixels[index] & 0xff;
pixels[index] = alpha | (r << 16) | (g << 8) | b;
}
if (y >= (height - shadow_height)) {
alpha = alpha + 0x1F000000;
}
}
// invert effect
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int y = 0; y < height; y++) {
bm.setPixels(pixels, y * width, width, 0, height - y - 1, width, 1);
}
return Bitmap.createBitmap(bm, 0, 0, width, shadow_height);
}
9.保存圖片到SDCard:
/**
* @param imagePath 圖片保存路徑
* @param bm 被保存的bitmap對(duì)象
*/
public static void saveImgToLocal(String imagePath, Bitmap bm) {
if (bm == null || imagePath == null || "".equals(imagePath)) {
return;
}
File f = new File(imagePath);
if (f.exists()) {
return;
} else {
try {
File parentFile = f.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
f.createNewFile();
FileOutputStream fos;
fos = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
f.delete();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
f.delete();
}
}
}
10.從SDCard中獲取圖片:
/**
* @param imagePath 圖片在SDCard中保存的路徑
* @return 返回保存的bitmap對(duì)象
*/
public static Bitmap getImageFromLocal(String imagePath) {
File file = new File(imagePath);
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
file.setLastModified(System.currentTimeMillis());
return bitmap;
}
return null;
}
11.圖片壓縮處理:
/**
* 對(duì)圖片進(jìn)行壓縮,主要是為了解決控件顯示過(guò)大圖片占用內(nèi)存造成OOM問(wèn)題。
* 一般壓縮后的圖片大小應(yīng)該和用來(lái)展示它的控件大小相近。
* @param context 上下文
* @param resId 圖片資源Id
* @param reqWidth 期望壓縮的寬度
* @param reqHeight 期望壓縮的高度
* @return 壓縮后的圖片
*/
public static Bitmap compressBitmapFromResourse(Context context, int resId, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
/*
* 第一次解析時(shí),inJustDecodeBounds設(shè)置為true,
* 禁止為bitmap分配內(nèi)存,雖然bitmap返回值為空,但可以獲取圖片大小
*/
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(context.getResources(), resId, options);
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
options.inSampleSize = inSampleSize;
//使用計(jì)算得到的inSampleSize值再次解析圖片
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(context.getResources(), resId, options);
}
12. 獲取可用內(nèi)存的最大值(App使用內(nèi)存超出這個(gè)值會(huì)引起OutOfMemory異常):
private int getMaxMemoryForApp() {
int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
return maxMemory;
}
13.將圖片裁剪成圓圈:
/**
* 將Bitmap處理為圓形的圖片
* @param bitmap 處理之前的位圖
* @return 處理之后的位圖
*/
public static Bitmap circlePic(Bitmap bitmap){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int r = width < height ? width/2:height/2;//圓的半徑,取寬和高中較小的,以便于顯示沒(méi)有空白
Bitmap outBitmap = Bitmap.createBitmap(r*2, r*2, Bitmap.Config.ARGB_8888);//創(chuàng)建一個(gè)剛好2r大小的Bitmap
Canvas canvas = new Canvas(outBitmap);
final int color =0xff424242;
final Paint paint = new Paint();
/**
* 截取圖像的中心的一個(gè)正方形,用于在原圖中截取
* 坐標(biāo)如下:
* 1.如果 w < h , 左上坐標(biāo)(0, (h-w)/2) , 右上坐標(biāo)(w, (h+w)/2) 偏移10
* 2.如果 w > h , 左上坐標(biāo)((w-h)/2, 0) , 右上坐標(biāo)((w+h)/2, h) 偏移10
*/
final Rect rect = new Rect( width < height ? 0 : (width-height)/2, width < height ? (height-width)/2 - 10 : -10,
width < height ? width : (width+height)/2, (width < height ? (height+width)/2 - 10: height - 10));
//創(chuàng)建一個(gè)直徑大小的正方形,用于設(shè)置canvas的顯示與設(shè)置畫(huà)布截取
final Rect rect2 = new Rect( 0, 0, r*2, r*2);
//提高精度,用于消除鋸齒
final RectF rectF = new RectF(rect2);
//下面是設(shè)置畫(huà)筆和canvas
paint.setAntiAlias(true);
canvas.drawARGB(0,0,0,0);
paint.setColor(color);
//設(shè)置圓角,半徑都為r,大小為rect2
canvas.drawRoundRect(rectF, r, r, paint);
//設(shè)置圖像重疊時(shí)的顯示方式
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//繪制圖像到canvas
canvas.drawBitmap(bitmap, rect, rect2, paint);
return outBitmap;
}
}
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法
- Android代碼實(shí)現(xiàn)圖片和文字上下布局
- android編程實(shí)現(xiàn)系統(tǒng)圖片剪裁的方法
- android編程實(shí)現(xiàn)圖片庫(kù)的封裝方法
- Android編程中圖片特效處理方法小結(jié)
- Android圖片轉(zhuǎn)換器代碼分享
- Android啟動(dòng)相機(jī)拍照并返回圖片
- Android編程之圖片顏色處理方法
- Android編程學(xué)習(xí)之異步加載圖片的方法
- android圖片類型之間相互轉(zhuǎn)換實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)ListView異步加載圖片的方法
- Android開(kāi)發(fā)從相機(jī)或相冊(cè)獲取圖片裁剪
相關(guān)文章
解決VSCode調(diào)試react-native android項(xiàng)目錯(cuò)誤問(wèn)題
這篇文章主要介紹了VSCode調(diào)試react-native android項(xiàng)目錯(cuò)誤解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Android入門(mén)之使用eclipse進(jìn)行源碼開(kāi)發(fā)的方法
這篇文章主要介紹了Android入門(mén)之使用eclipse進(jìn)行源碼開(kāi)發(fā)的方法,較為詳細(xì)的分析了使用eclipse進(jìn)行Android源碼開(kāi)發(fā)的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02
Android IPC機(jī)制Messenger實(shí)例詳解
這篇文章主要介紹了 Android IPC機(jī)制Messenger實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Kotlin開(kāi)發(fā)中open關(guān)鍵字與類名函數(shù)名和變量名的使用方法淺析
這篇文檔中,我們將解釋如何以及為什么將 open 關(guān)鍵字與類名、函數(shù)名和變量名一起使用,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的2023-02-02
詳細(xì)介紹Android中回調(diào)函數(shù)機(jī)制
這篇文章主要介紹了Android中回調(diào)函數(shù)機(jī)制,有需要的朋友可以參考一下2014-01-01
Android實(shí)現(xiàn)加載狀態(tài)視圖切換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)加載狀態(tài)視圖切換效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android利用Gson解析嵌套多層的Json的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇Android利用Gson解析嵌套多層的Json的簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08

