Android Glide識(shí)別圖像是不是一個(gè)正圓

需求如果網(wǎng)絡(luò)下載的圖片是圓形就正常顯示,不是的話做處理。

思路:Glide下載下來,獲取寬高,拿到中心點(diǎn),根據(jù)半徑取像素點(diǎn),判斷是否相等,全部相等就是圓。下面是實(shí)現(xiàn),精度一般,勉強(qiáng)能用。

Glide.with(mContext).load(item.getVedioPicture()).asBitmap().into(new SimpleTarget() {

@Override

? ? public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {

int width=resource.getWidth();

? ? ? ? int height=resource.getHeight();

? ? ? ? int r= (int) (height/1.9);//半徑比真正半徑大,預(yù)防圖片不是一個(gè)非常正規(guī)的圓。

? ? ? ? Map map=new HashMap<>();

//看精度這是循環(huán)36次沒十度一次

? ? ? ? for (int theta =0; theta <360; theta=theta+10) {

double t = (theta *3.14159265) /180; // 角度值0 ~ 2*PI

? ? ? ? ? ? int? x0 = (int) Math.round(width/2 - r * Math.cos(t));

? ? ? ? ? ? int y0 = (int) Math.round(height/2 - r * Math.sin(t));

//判斷是否超出邊界。

? ? ? ? ? ? if (x0 < width && x0 >0 && y0 < height && y0 >0) {

Log.e("嗚嗚嗚亢","這是一個(gè)像素"+resource.getPixel(x0,y0));

? ? ? ? ? ? ? ? map.put(resource.getPixel(x0,y0),1);

? ? ? ? ? ? }

}

//根據(jù)map特性同樣的key值只會(huì)才在一個(gè)判斷是否小于2,小于2表示這個(gè)圓環(huán)上都是同樣的像素

if(map.size()<2){

Log.e("啊啊啊啊","這是一個(gè)圓啊啊啊啊啊啊啊");

? ? ? ? ? ? ((ImageView)helper.getView(R.id.res_img)).setImageBitmap(resource);

? ? ? ? }else {

}

}

});

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容