android使用第三方架構fresco展示網狀圖片

來源:互聯網
上載者:User

標籤:

資源:https://github.com/facebook/fresco](https://github.com/facebook/fresco

01:優點和不足:

  載入的圖片多時,推薦使用,比如ListView條目中的圖片.

02:添加依賴庫:在app/build.gradle中添加如下代碼,點擊同步:

compile ‘com.facebook.fresco:fresco:0.9.0+‘

03.布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="www.itcast.com.testfresco.MainActivity"><!--寬高一定要寫死-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
fresco:placeholderImage="@mipmap/ic_launcher" />
<Button android:id="@+id/btn" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="fresco展示網狀圖片" /> </RelativeLayout>

04.訪問網路當然添加許可權必不可少:

<uses-permission android:name="android.permission.INTERNET" />

05.初始化要注意:

在Application初始化或在Activity 的setContentView()方法之前,進行初始化Fresco.initialize(this);

06:核心代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private SimpleDraweeView iv;    private Button btn;    private String NET_URI="http://img4.duitang.com/uploads/item/201412/11/20141211152834_eAfSi.jpeg";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //初始化的位置        Fresco.initialize(this);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        iv = (SimpleDraweeView) findViewById(R.id.iv);        btn = (Button) findViewById(R.id.btn);        btn.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn:                Uri uri=Uri.parse(NET_URI);                iv.setImageURI(uri);                break;        }    }}

07:圖片特殊顯示參數:

fresco:actualImageScaleType="focusCrop"// 圖片的縮放方式.        fresco:backgroundImage="@color/blue" //背景圖.不支援縮放.XML僅能指定一張背景圖.如果使用Java代碼指定的話,可以指定多個背景,顯示方式類似FrameLayout,多個背景圖按照順序一級一級層疊上去.        fresco:fadeDuration="300" // 漸顯圖片的時間        fresco:failureImage="@drawable/error" // 圖片載入失敗顯示的圖片        fresco:failureImageScaleType="centerInside" //// 圖片載入失敗顯示的圖片的縮放類型        fresco:overlayImage="@drawable/watermark" // 層疊圖,最後疊加在圖片之上.不支援縮放.XML僅能指定一張.如果使用Java代碼指定的話,可以指定多個,顯示方式類似FrameLayout,多個圖按照順序一級一級層疊上去.        fresco:placeholderImage="@color/wait_color"  // 圖片載入成功之前顯示的佔位圖        fresco:placeholderImageScaleType="fitCenter" // 圖片載入成功之前顯示的佔位圖的縮放類型        fresco:pressedStateOverlayImage="@color/red" // 設定按壓狀態下的層疊圖.不支援縮放.        fresco:progressBarAutoRotateInterval="1000" // 進度條圖片旋轉顯示時間長度        fresco:progressBarImage="@drawable/progress_bar" // 進度條圖片        fresco:progressBarImageScaleType="centerInside" //進度條圖片的縮放類型        fresco:retryImage="@drawable/retrying" // 當圖片載入失敗的時候,顯示該圖片提示使用者點擊重新載入圖片        fresco:retryImageScaleType="centerCrop" // 提示圖片的縮放類型        fresco:roundAsCircle="false" // 顯示圓形圖片        fresco:roundBottomLeft="false" // roundedCornerRadius屬性設定後,四個角都會有圓角,如果左下角不需要設定為false.        fresco:roundBottomRight="true" // roundedCornerRadius屬性設定後,四個角都會有圓角,如果右下角不需要設定為false.        fresco:roundTopLeft="true" // roundedCornerRadius屬性設定後,四個角都會有圓角,如果左上方不需要設定為false.        fresco:roundTopRight="false" // roundedCornerRadius屬性設定後,四個角都會有圓角,如果右上方不需要設定為false.        fresco:roundWithOverlayColor="@color/corner_color" // 設定圖片圓角後空出地區的顏色.如樣本圖中的紅色部分        fresco:roundedCornerRadius="1dp" // 設定圖片圓角角度,設定該屬性後四個角都會生效        fresco:roundingBorderColor="@color/border_color" // 設定圓角後,邊框的顏色.        fresco:roundingBorderWidth="2dp" /> // 設定圓角後,外邊框的寬高

08.圖片特殊顯示java代碼:

GenericDraweeHierarchy hierarchy = GenericDraweeHierarchyBuilder                .newInstance(getResources())                .setRetryImage(getResources().getDrawable(R.mipmap.ic_launcher))                .build();        imageivew.setHierarchy(hierarchy);

09.漸進式顯示圖片(下載多少就顯示多少,沒下載完之前是模糊的):

 /*漸進式顯示圖片                ProgressiveJpegConfig pjpegConfig = new ProgressiveJpegConfig() {                    @Override                    // 返回下一個需要解碼的掃描次數                    public int getNextScanNumberToDecode(int scanNumber) {                        return scanNumber + 2;                    }                    // 確定多少個掃描次數之後的圖片才能開始顯示                    public QualityInfo getQualityInfo(int scanNumber) {                        boolean isGoodEnough = (scanNumber >= 5);                        return ImmutableQualityInfo.of(scanNumber, isGoodEnough, false);                    }                };                // ImagePipelineConfig配置如何載入映像                ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)                        .setProgressiveJpegConfig(pjpegConfig)                        .build();                //  顯式地指定允許漸進式JPEG圖片載入                ImageRequest request = ImageRequestBuilder                        .newBuilderWithSource(uri)                        .setProgressiveRenderingEnabled(true)                        .build();                // 構建顯示圖片所用到的DraweeController                DraweeController controller = Fresco.newDraweeControllerBuilder()                        .setImageRequest(request)                        .setOldController(iv.getController())                        .build();                iv.setController(controller);                */

10.顯示Gif圖片:

/*顯示GIF圖片.Fresco 支援 GIF 和 WebP 格式的動畫圖片.如果你希望圖片下載完之後自動播放,同時,當View從螢幕移除時,停止播放,只需要在 image request 中簡單設定                Uri urlGif = Uri.parse("http://img0.imgtn.bdimg.com/it/u=4040253614,2927360696&fm=21&gp=0.jpg");                DraweeController controller = Fresco.newDraweeControllerBuilder()                        .setUri(urlGif)                        .setAutoPlayAnimations(true)                        .build();                iv.setController(controller);                */

11:代碼資源:

http://download.csdn.net/detail/tom91/9631011

 

android使用第三方架構fresco展示網狀圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.