Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法
Android中處理圖像是一件很常見(jiàn)的事情,這里記錄備忘一些親身使用過(guò)的處理圖片數(shù)據(jù)的方法。
轉(zhuǎn)為Bitmap
RGB值轉(zhuǎn)Bitmap
private Bitmap createColorBitmap(String rgb, int width, int height) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
int color = Color.parseColor(rgb);
bmp.eraseColor(color);
return bmp;
}
//Usage
Bitmap bmp = createColorBitmap("#cce8cf", 200, 50);
Color值轉(zhuǎn)Bitmap
private Bitmap createColorBitmap(int color, int width, int height) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.eraseColor(color);
return bmp;
}
//Usage
Bitmap bmp = createColorBitmap(Color.BLUE, 200, 50);
字節(jié)數(shù)組轉(zhuǎn)Bitmap
private Bitmap getBitmapFromByteArray(byte[] array) {
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
讀取文件轉(zhuǎn)Bitmap
private Bitmap getBitmapFromFile(String pathName) {
return BitmapFactory.decodeFile(pathName);
}
讀取資源轉(zhuǎn)Bitmap
private Bitmap getBitmapFromResource(Resources res, int resId) {
return BitmapFactory.decodeResource(res, resId);
}
輸入流轉(zhuǎn)Bitmap
private Bitmap getBitmapFromStream(InputStream inputStream) {
return BitmapFactory.decodeStream(inputStream);
}
Drawable轉(zhuǎn)Bitmap
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
轉(zhuǎn)為Drawable
資源轉(zhuǎn)Drawable
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
Bitmap轉(zhuǎn)Drawable
Drawable d = new BitmapDrawable(getResources(),bitmap);
圖片圓角展示
通過(guò)對(duì)圖片數(shù)據(jù)bitmap進(jìn)行處理即可,其中pixels為邊角的半徑。
public static Bitmap getRoundedCornerBitmap(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);
return output;
}
- Android圖像處理之霓虹濾鏡效果
- Android編程滑動(dòng)效果之Gallery仿圖像集瀏覽實(shí)現(xiàn)方法
- Android RichText 讓Textview輕松的支持富文本(圖像ImageSpan、點(diǎn)擊效果等等類似QQ微信聊天)
- Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(三)Animation效果的XML實(shí)現(xiàn)
- Android圖片處理:識(shí)別圖像方向并顯示實(shí)例教程
- Android開(kāi)發(fā)技巧之像QQ一樣輸入文字和表情圖像
- android圖像繪制(二)畫(huà)布上放大縮小問(wèn)題
- android圖像繪制(六)獲取本地圖片或拍照?qǐng)D片等圖片資源
- android圖像繪制(五)畫(huà)布保存為指定格式/大小的圖片
- Android中將View的內(nèi)容保存為圖像的簡(jiǎn)單實(shí)例
- Android開(kāi)發(fā)學(xué)習(xí)筆記之通過(guò)API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式
- Android使用API實(shí)現(xiàn)圖像扭曲效果示例
相關(guān)文章
Okhttp3實(shí)現(xiàn)爬取驗(yàn)證碼及獲取Cookie的示例
本篇文章主要介紹了Okhttp3實(shí)現(xiàn)爬取驗(yàn)證碼及獲取Cookie的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Android開(kāi)發(fā)實(shí)現(xiàn)仿京東商品搜索選項(xiàng)卡彈窗功能
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)仿京東商品搜索選項(xiàng)卡彈窗功能,涉及Android布局及事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android實(shí)現(xiàn)qq列表式的分類懸浮提示
工作中遇到了一個(gè)需求,讓?xiě)?yīng)用中的一個(gè)列表按照分類顯示,并且能提示當(dāng)前是在哪個(gè)分類,度娘了一番,參考了前輩們的博客后實(shí)現(xiàn)了,現(xiàn)在分享給大家,有需要的可以參考借鑒。2016-09-09
android在異步任務(wù)中關(guān)閉Cursor的代碼方法
android在異步任務(wù)中如何關(guān)閉Cursor?在我們開(kāi)發(fā)應(yīng)用的時(shí)候,很多時(shí)候會(huì)遇到這種問(wèn)題,下面我們就看看代碼如何實(shí)現(xiàn)2013-11-11
Android自定義有限制區(qū)域的圖例角度自識(shí)別涂鴉工具類完結(jié)篇
這篇文章主要為大家介紹了Android自定義有限制區(qū)域的圖例角度自識(shí)別涂鴉工具類完結(jié)篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android Canvas和Bitmap結(jié)合繪圖詳解流程
在 Android Canvas 上繪圖非常難,在繪圖時(shí)需要理解許多不同的類和概念。這篇文章中,將介紹 Android 框架中可用的一些類,它們可以讓畫(huà)布使用時(shí)更輕松2021-11-11
Android入門之在SharedPreference中使用加密
這篇文章主要為大家詳細(xì)介紹了Android如何使在SharedPreference中使用加密,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12

