怎么在Android中使用Picasso加載網(wǎng)絡(luò)圖片

怎么在Android中使用Picasso加載網(wǎng)絡(luò)圖片?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)山東,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18980820575

通過(guò)Picasso來(lái)縮放

其實(shí)picasso提供了這樣的方法。具體是顯示Transformation 的 transform 方法。

(1) 先獲取網(wǎng)絡(luò)或本地圖片的寬高
(2) 獲取需要的目標(biāo)寬
(3) 按比例得到目標(biāo)的高度
(4) 按照目標(biāo)的寬高創(chuàng)建新圖

Transformation transformation = new Transformation() {
    @Override
    public Bitmap transform(Bitmap source) {
      int targetWidth = mImg.getWidth();
      LogCat.i("source.getHeight()="+source.getHeight());
       LogCat.i("source.getWidth()="+source.getWidth());
       LogCat.i("targetWidth="+targetWidth);
      if(source.getWidth()==0){
        return source;
      }
      //如果圖片小于設(shè)置的寬度,則返回原圖
      if(source.getWidth()<targetWidth){
        return source;
      }else{
        //如果圖片大小大于等于設(shè)置的寬度,則按照設(shè)置的寬度比例來(lái)縮放
        double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
        int targetHeight = (int) (targetWidth * aspectRatio);
        if (targetHeight != 0 && targetWidth != 0) {
          Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
          if (result != source) {
            // Same bitmap is returned if sizes are the same
            source.recycle();
          }
          return result;
        } else {
          return source;
        }
      }
    }
    @Override
    public String key() {
      return "transformation" + " desiredWidth";
    }
  };

之后在Picasso設(shè)置transform

Picasso.with(mContext)
      .load(imageUrl)
      .placeholder(R.mipmap.zhanwei)
      .error(R.mipmap.zhanwei)
      .transform(transformation)
      .into(viewHolder.mImageView);

Transformation 這是Picasso的一個(gè)非常強(qiáng)大的功能了,它允許你在load圖片 -> into ImageView 中間這個(gè)過(guò)成對(duì)圖片做一系列的變換。比如你要做圖片高斯模糊、添加圓角、做度灰處理、圓形圖片等等都可以通過(guò)Transformation來(lái)完成。

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

標(biāo)題名稱:怎么在Android中使用Picasso加載網(wǎng)絡(luò)圖片
文章網(wǎng)址:http://muchs.cn/article22/isjgcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、定制開(kāi)發(fā)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)App設(shè)計(jì)、品牌網(wǎng)站制作、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司