Android處理圖像數(shù)據(jù)轉(zhuǎn)換的各種方法
Android中處理圖像是一件很常見的事情,這里記錄備忘一些親身使用過的處理圖片數(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);
圖片圓角展示
通過對圖片數(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開發(fā)之圖形圖像與動(dòng)畫(三)Animation效果的XML實(shí)現(xiàn)
- Android圖片處理:識別圖像方向并顯示實(shí)例教程
- Android開發(fā)技巧之像QQ一樣輸入文字和表情圖像
- android圖像繪制(二)畫布上放大縮小問題
- android圖像繪制(六)獲取本地圖片或拍照圖片等圖片資源
- android圖像繪制(五)畫布保存為指定格式/大小的圖片
- Android中將View的內(nèi)容保存為圖像的簡單實(shí)例
- Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式
- Android使用API實(shí)現(xiàn)圖像扭曲效果示例
相關(guān)文章
Android TabLayout(選項(xiàng)卡布局)簡單用法實(shí)例分析
這篇文章主要介紹了Android TabLayout(選項(xiàng)卡布局)簡單用法,結(jié)合實(shí)例形式簡單分析了Android選項(xiàng)卡布局的界面布局與功能實(shí)現(xiàn)具體相關(guān)技巧,需要的朋友可以參考下2016-01-01Android開發(fā)中模仿qq列表信息滑動(dòng)刪除功能
這篇文章主要介紹了Android開發(fā)中模仿qq列表信息滑動(dòng)刪除功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android Webview的postUrl與loadUrl加載頁面實(shí)例
這篇文章主要介紹了Android Webview的postUrl與loadUrl加載頁面實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過程實(shí)現(xiàn)App換膚功能
這篇文章主要為大家介紹了Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過程實(shí)現(xiàn)App換膚功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android實(shí)現(xiàn)隱私政策彈窗與鏈接功能
現(xiàn)在幾乎所有的應(yīng)用市場都要求應(yīng)用上架需要用戶協(xié)議/隱私政策,本篇內(nèi)容將介紹如何在APP內(nèi)植入一個(gè)隱私政策彈窗與鏈接,對Android隱私政策彈窗實(shí)現(xiàn)代碼感興趣的朋友跟隨小編一起看看吧2021-07-07