Fresco載入圖片
簡介:
由Facebook最新推出的一款用於Android應用中展示圖片的強大圖片庫,採用MVC設計模式。(被譽為最好的圖片處理框架,facebook出品,必出精品)
優勢:
底層直接用的是C語言,所以對於記憶體的管理特別強大,一張圖片如果 picasso 來處理要用80%的話,那麼fresco只佔40%,必須使用它的自定義控制元件(這一點也是弊端,導致如果後期更換圖片處理框架,不用Fresco,會特別麻煩),設定src屬性,可以直接引用GIF…,各方面都不用管的.
•Fresco中的image pipeline模組。負責從網路,從本地檔案系統,本地資源載入圖片。為了最大限度節省空間和CPU時間,它含有3級快取設計(2級記憶體,1級檔案)。
•Fresco中的Drawees 模組,顯示loading圖,當圖片不再顯示在螢幕上時,及時地釋放記憶體和空間佔用。
五大特點:
記憶體管理:在5.0以下系統,Fresco將圖片放到一個特別的記憶體區域。然而,在圖片不顯示的時候,佔用的記憶體會自動被釋放。這會使得APP更加流暢,減少因圖片記憶體佔用而引發的OOM.(記憶體分配採用:系統匿名共享記憶體)
漸進式呈現圖片:漸進式圖片格式先呈現大致的圖片輪廓,然後隨著圖片下載的繼續,呈現逐漸清晰的圖片,在網速慢的情況下有極大的優越性,可帶來更好的使用者體驗。 支援載入Gif圖,支援WebP格式。PNG,JPG,Webp 影象的呈現: (1)自定義居中焦點(對人臉等圖片顯示非常有幫助)。 (2)圓角圖,當然圓圈也行。 (3)下載失敗之後,點選重新下載。 (4)自定義佔點陣圖,自定義overlay, 或者進度條。 (5)指定使用者按壓時的overlay。 影象的載入: (1)為同一個圖片指定不同的遠端路徑,或者使用已經存在本地快取中的圖片。 (2)先顯示一個低解析度的圖片,等高清圖下載完畢再顯示高清圖。 (3)載入完成回撥通知。 (4)對於本地圖,如有EXIF縮圖,在大圖載入完成之前,可先顯示縮圖。 (5)縮放或者旋轉圖片。 (6)處理已下載的圖片。
**
作用:
**帶進度條的圖片、圖片的不同裁剪、圓形和圓角圖片、漸進式展示圖片、Gif動畫圖片、多圖請求及圖片複用、圖片載入監聽、圖片縮放和旋轉、修改圖片和動態展示.
好了接下來 我們今天一起來看一下通過Fresco可以對圖片進行哪些操作:(我們今天先列舉一部分)
首先:
我們要想使用Fresco,我們需要匯入依賴,這個就根據我們所需要的配置相應的依賴就可以
dependencies {
/*Fresco的基礎依賴*/
implementation 'com.facebook.fresco:fresco:0.14.1'
/*Fresco的GIF功能的依賴*/
implementation 'com.facebook.fresco:animated-gif:0.14.1'
/*支援 WebP (靜態圖+動圖),需要新增*/
compile 'com.facebook.fresco:animated-webp:0.12.0'
compile 'com.facebook.fresco:webpsupport:0.12.0'
/* 僅支援 WebP 靜態圖,需要新增*/
compile 'com.facebook.fresco:webpsupport:0.12.0'
}
其次:
建立一個自定義類MyApplication,繼承Application
package com.example.fresco;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
/**
* date:2018/11/27
* author:李壯(HUAWEI)
* function:
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
既然我們自定義類那我們必須在(AndroidManifest.xml)清單檔案中進行註冊,並且我們載入圖片的過程中需要訪問網路,所以我們需要在此配置網路許可權
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
</application>
好到這裡我們就將fresc所需的環境搭建完畢
接下來我們就開始具體功能的操作:
MainActivity:
初始化控制元件id,獲取相應的資源控制元件,實現頁面跳轉至具體功能的頁面
package com.example.fresco;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.fresco.view.AutoSizeActivity;
import com.example.fresco.view.CircleAndCornerActivity;
import com.example.fresco.view.GifActivity;
import com.example.fresco.view.JpegActivity;
import com.example.fresco.view.MultiActivity;
import com.example.fresco.view.ProgressBarActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_progressBar_icon;
private Button btn_crop_icon;
private Button btn_circleAndCorner_icon;
private Button btn_jpeg_icon;
private Button btn_gif_icon;
private Button btn_multi_icon;
private Button btn_listener_icon;
private Button btn_resize_icon;
private Button btn_update_icon;
private Button btn_autoSize_icon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控制元件資源
initView();
}
//獲取資源id
private void initView() {
btn_progressBar_icon = (Button) findViewById(R.id.btn_progressBar_icon);
btn_crop_icon = (Button) findViewById(R.id.btn_crop_icon);
btn_circleAndCorner_icon = (Button) findViewById(R.id.btn_circleAndCorner_icon);
btn_jpeg_icon = (Button) findViewById(R.id.btn_jpeg_icon);
btn_gif_icon = (Button) findViewById(R.id.btn_gif_icon);
btn_multi_icon = (Button) findViewById(R.id.btn_multi_icon);
btn_listener_icon = (Button) findViewById(R.id.btn_listener_icon);
btn_resize_icon = (Button) findViewById(R.id.btn_resize_icon);
btn_update_icon = (Button) findViewById(R.id.btn_update_icon);
btn_autoSize_icon = (Button) findViewById(R.id.btn_autoSize_icon);
btn_progressBar_icon.setOnClickListener(this);
btn_crop_icon.setOnClickListener(this);
btn_circleAndCorner_icon.setOnClickListener(this);
btn_jpeg_icon.setOnClickListener(this);
btn_gif_icon.setOnClickListener(this);
btn_multi_icon.setOnClickListener(this);
btn_listener_icon.setOnClickListener(this);
btn_resize_icon.setOnClickListener(this);
btn_update_icon.setOnClickListener(this);
btn_autoSize_icon.setOnClickListener(this);
}
//根據相應的點選事件跳轉至相應的功能頁面
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_progressBar_icon://帶進度條的圖片
Intent ProgressBarIntent = new Intent(this,ProgressBarActivity.class);
startActivity(ProgressBarIntent);
finish();
break;
case R.id.btn_crop_icon://圖片的不同裁剪
break;
case R.id.btn_circleAndCorner_icon://圓形和圓角的圖片
Intent CircleAndCornerIntent = new Intent(this,CircleAndCornerActivity.class);
startActivity(CircleAndCornerIntent);
finish();
break;
case R.id.btn_jpeg_icon://漸進式展示圖片
Intent JpegIntent = new Intent(this,JpegActivity.class);
startActivity(JpegIntent);
finish();
break;
case R.id.btn_gif_icon://Gif動畫圖片
Intent GifIntent = new Intent(this,GifActivity.class);
startActivity(GifIntent);
finish();
break;
case R.id.btn_multi_icon://多圖請求及圖片複用
Intent MultiIntent = new Intent(this,MultiActivity.class);
startActivity(MultiIntent);
finish();
break;
case R.id.btn_listener_icon://圖片載入監聽
break;
case R.id.btn_resize_icon://圖片縮放和旋轉
break;
case R.id.btn_update_icon://修改圖片
break;
case R.id.btn_autoSize_icon://動態展示圖片
Intent AutoSizeIntent = new Intent(this,AutoSizeActivity.class);
startActivity(AutoSizeIntent);
finish();
break;
}
}
}
主頁的佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_progressBar_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="帶進度條的圖片"/>
<Button
android:id="@+id/btn_crop_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圖片的不同裁剪"/>
<Button
android:id="@+id/btn_circleAndCorner_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圓形和圓角的圖片"/>
<Button
android:id="@+id/btn_jpeg_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="漸進式展示圖片"/>
<Button
android:id="@+id/btn_gif_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gif動畫圖片"/>
<Button
android:id="@+id/btn_multi_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多圖請求及圖片複用"/>
<Button
android:id="@+id/btn_listener_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圖片載入監聽"/>
<Button
android:id="@+id/btn_resize_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圖片縮放和旋轉"/>
<Button
android:id="@+id/btn_update_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改圖片"/>
<Button
android:id="@+id/btn_autoSize_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="動態展示圖片"/>
</LinearLayout>
1.展示帶進度條樣式的圖片:
ProgressBarActivity:
package com.example.fresco.view;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.fresco.R;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
/**
* 載入一張網路圖片,且載入過程中,有進度條的顯示,載入完畢進度條消失
*/
public class ProgressBarActivity extends AppCompatActivity {
private SimpleDraweeView sdv_progressBar_icon;
private String path = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543329113431&di=551287617c4e82f27f19f82d7af4da57&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimage%2Fc0%253Dshijue1%252C0%252C0%252C294%252C40%2Fsign%3D5bdd825b5b0fd9f9b41a5d2a4d44be5f%2Fd62a6059252dd42a9811ac8c093b5bb5c9eab8f9.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
initView();
//1.顯示所要載入的圖片
Uri parse = Uri.parse(path);
//2.建立Build物件
GenericDraweeHierarchyBuilder genericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder(getResources());
//3.通過這個物件設定進度條的樣式
GenericDraweeHierarchy build = genericDraweeHierarchyBuilder.setProgressBarImage(new ProgressBarDrawable()).build();
//4.把樣式設定給圖片控制元件
sdv_progressBar_icon.setHierarchy(build);
//5.載入圖片成功
sdv_progressBar_icon.setImageURI(parse);
}
private void initView() {
sdv_progressBar_icon = (SimpleDraweeView) findViewById(R.id.sdv_progressBar_icon);
}
}
相應佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
tools:context=".view.ProgressBarActivity">
<!--fresco:placeholderImage="@drawable/my_drawable":預設佔位圖片-->
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv_progressBar_icon"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center"
fresco:placeholderImage="@mipmap/ic_launcher" />
</android.support.constraint.ConstraintLayout>
2.圓形和圓角樣式的圖片:
CircleAndCornerActivity:
package com.example.fresco.view;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.fresco.R;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.view.SimpleDraweeView;
public class CircleAndCornerActivity extends AppCompatActivity implements View.OnClickListener {
private SimpleDraweeView sdv_circleandcorner_icon;
private Button bt_fresco_circle;
private Button bt_fresco_corner;
private GenericDraweeHierarchyBuilder mBuilder;
private Uri mUri;
private RoundingParams mParams;
private String Path = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543329113431&di=551287617c4e82f27f19f82d7af4da57&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimage%2Fc0%253Dshijue1%252C0%252C0%252C294%252C40%2Fsign%3D5bdd825b5b0fd9f9b41a5d2a4d44be5f%2Fd62a6059252dd42a9811ac8c093b5bb5c9eab8f9.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circle_and_corner);
initView();
mUri = Uri.parse(Path);
//1.建立Build物件
mBuilder = new GenericDraweeHierarchyBuilder(getResources());
}
private void initView() {
sdv_circleandcorner_icon = (SimpleDraweeView) findViewById(R.id.sdv_circleandcorner_icon);
bt_fresco_circle = (Button) findViewById(R.id.bt_fresco_circle);
bt_fresco_corner = (Button) findViewById(R.id.bt_fresco_corner);
bt_fresco_circle.setOnClickListener(this);
bt_fresco_corner.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_fresco_circle://1.設定圓形
//1.1設定形狀物件,形狀為圓形
mParams = RoundingParams.asCircle();
//1.2把形狀設定給引數物件
GenericDraweeHierarchy rounding = mBuilder.setRoundingParams(mParams).build();
//1.3設定圖片給控制元件
sdv_circleandcorner_icon.setHierarchy(rounding);
//1.4載入圖片控制元件
sdv_circleandcorner_icon.setImageURI(mUri);
break;
case R.id.bt_fresco_corner://2.設定圓角
//2.1設定形狀物件,形狀為圓角
RoundingParams roundingParams = RoundingParams.fromCornersRadius(50f);
//2.2把形狀設定給引數物件
GenericDraweeHierarchy build = mBuilder.setRoundingParams(roundingParams).build();
//2.3設定圖片給控制元件
sdv_circleandcorner_icon.setHierarchy(build);
//2.4載入圖片控制元件
sdv_circleandcorner_icon.setImageURI(mUri);
break;
}
}
}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
tools:context=".view.CircleAndCornerActivity">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv_circleandcorner_icon"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
fresco:placeholderImage="@mipmap/ic_launcher" />
<Button
android:id="@+id/bt_fresco_circle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="設定圓形圖片" />
<Button
android:id="@+id/bt_fresco_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="設定圓角圖片" />
</LinearLayout>
3.漸進式展示圖片:
JpegActivity:
package com.example.fresco.view;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.fresco.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.decoder.ProgressiveJpegConfig;
import com.facebook.imagepipeline.image.ImmutableQualityInfo;
import com.facebook.imagepipeline.image.QualityInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
public class JpegActivity extends AppCompatActivity implements View.OnClickListener {
private SimpleDraweeView sdv_fresco_jpeg;
private Button sdv_fresco_askImg;
private String Path = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543329113431&di=551287617c4e82f27f19f82d7af4da57&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimage%2Fc0%253Dshijue1%252C0%252C0%252C294%252C40%2Fsign%3D5bdd825b5b0fd9f9b41a5d2a4d44be5f%2Fd62a6059252dd42a9811ac8c093b5bb5c9eab8f9.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jpeg);
initView();
}
private void initView() {
sdv_fresco_jpeg = (SimpleDraweeView) findViewById(R.id.sdv_fresco_jpeg);
sdv_fresco_askImg = (Button) findViewById(R.id.sdv_fresco_askImg);
sdv_fresco_askImg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sdv_fresco_askImg:
//1.獲取圖片地址
Uri parse = Uri.parse(Path);
//2.載入質量配置,為了實現節省GPU,隨著圖片的下載,下完的掃描序列:1,4,5,10
ProgressiveJpegConfig jpegConfig = new ProgressiveJpegConfig() {
@Override
public int getNextScanNumberToDecode(int scanNumber) {
return scanNumber + 2;
}
@Override
public QualityInfo getQualityInfo(int scanNumber) {
boolean isGoodEnough = (scanNumber >= 5);
return ImmutableQualityInfo.of(scanNumber,isGoodEnough,false);
}
};
//固定程式碼,直接複製使用即可,不用記憶
ImagePipelineConfig.newBuilder(this).setProgressiveJpegConfig(jpegConfig).build();
//建立ImageRequest物件
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(parse)
.setProgressiveRenderingEnabled(true)//設定開啟漸進模式
.build();
AbstractDraweeController draweeController = Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setTapToRetryEnabled(true)
.setOldController(sdv_fresco_jpeg.getController())//記憶體優化
.build();
sdv_fresco_jpeg.setController(draweeController);
break;
}
}
}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_gravity="center"
android:id="@+id/sdv_fresco_jpeg"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="48dp"
fresco:placeholderImage="@mipmap/ic_launcher" />
<Button
android:id="@+id/sdv_fresco_askImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="請求網路圖片" />
</LinearLayout>
4.Gif動畫圖片:
GifActivity:
package com.example.fresco.view;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.fresco.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
/**
* 實現GIF的效果記得新增GIF的依賴
*/
public class GifActivity extends AppCompatActivity implements View.OnClickListener {
private SimpleDraweeView sdv_fresco_gif;
private Button bt_fresco_askImg;
private Button bt_fresco_stopAnim;
private Button bt_fresco_startAnim;
private String GifPath = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543330405675&di=876ac3318ec9a21454e12a4f563cc04e&imgtype=0&src=http%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_gif%2FXIbbJFPiamFAvjwicjXdEAPScWaQVdXicH8exiaD6vXibQU6c4jJiciaebibhUQQdib93wXLRIicHIrtPqvIU8OzleZpPK3g%2F640%3Fwx_fmt%3Dgif";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gif);
initView();
}
private void initView() {
sdv_fresco_gif = (SimpleDraweeView) findViewById(R.id.sdv_fresco_gif);
bt_fresco_askImg = (Button) findViewById(R.id.bt_fresco_askImg);
bt_fresco_stopAnim = (Button) findViewById(R.id.bt_fresco_stopAnim);
bt_fresco_startAnim = (Button) findViewById(R.id.bt_fresco_startAnim);
bt_fresco_askImg.setOnClickListener(this);
bt_fresco_stopAnim.setOnClickListener(this);
bt_fresco_startAnim.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_fresco_askImg:
//1.首先載入Gif動畫地址,這個載入過程需要一段時間
Uri uri = Uri.parse(GifPath);
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setUri(uri)
.setAutoPlayAnimations(false)//設定是否自動播放,true為播放,false為不播放
.setOldController(sdv_fresco_gif.getController())//記憶體優化
.build();
sdv_fresco_gif.setController(controller);
break;
case R.id.bt_fresco_stopAnim:
if (sdv_fresco_gif.getController() != null){
Animatable animatable = sdv_fresco_gif.getController().getAnimatable();
//非空判斷
if(animatable != null){
//停止播放
animatable.stop();
}
}
break;
case R.id.bt_fresco_startAnim:
if (sdv_fresco_gif.getController() != null){
Animatable animatable = sdv_fresco_gif.getController().getAnimatable();
//非空判斷同時判斷有沒有播放
if(animatable != null && !animatable.isRunning()){
//開始播放
animatable.start();
}
}
break;
}
}
}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv_fresco_gif"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="48dp"
android:layout_gravity="center"
fresco:placeholderImage="@mipmap/ic_launcher" />
<Button
android:id="@+id/bt_fresco_askImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="請求gif圖片" />
<Button
android:id="@+id/bt_fresco_stopAnim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="動畫停止" />
<Button
android:id="@+id/bt_fresco_startAnim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="動畫開始" />
</LinearLayout>
5.多圖請求及圖片複用:
MultiActivity :
package com.example.fresco.view;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.example.fresco.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import java.io.File;
public class MultiActivity extends AppCompatActivity implements View.OnClickListener {
private SimpleDraweeView sdv_fresco_multi;
private Button bt_fresco_multiImg;
private Button bt_fresco_thumbnailImg;
private Button bt_fresco_multiplexImg;
private String lowPath = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543333820854&di=6d3a116b3dce45c9a475b2f11cacc947&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171226%2Fb877aac2da3e489bad8638c5136f5e88.jpeg";
private String highPath = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543329113431&di=551287617c4e82f27f19f82d7af4da57&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimage%2Fc0%253Dshijue1%252C0%252C0%252C294%252C40%2Fsign%3D5bdd825b5b0fd9f9b41a5d2a4d44be5f%2Fd62a6059252dd42a9811ac8c093b5bb5c9eab8f9.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi);
initView();
}
private void initView() {
sdv_fresco_multi = (SimpleDraweeView) findViewById(R.id.sdv_fresco_multi);
bt_fresco_multiImg = (Button) findViewById(R.id.bt_fresco_multiImg);
bt_fresco_thumbnailImg = (Button) findViewById(R.id.bt_fresco_thumbnailImg);
bt_fresco_multiplexImg = (Button) findViewById(R.id.bt_fresco_multiplexImg);
bt_fresco_multiImg.setOnClickListener(this);
bt_fresco_thumbnailImg.setOnClickListener(this);
bt_fresco_multiplexImg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_fresco_multiImg://先顯示低解析度的圖,然後是高解析度的圖,Fresco就是
Uri lowUri = Uri.parse(lowPath);
Uri highUri = Uri.parse(highPath);
//控制載入的圖片控制器
AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
.setLowResImageRequest(ImageRequest.fromUri(lowUri))
.setImageRequest(ImageRequest.fromUri(highUri))
.build();
//載入圖片
sdv_fresco_multi.setController(controller);
break;
case R.id.bt_fresco_thumbnailImg://本地縮圖預覽
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "lz.jpg"));
break;
case R.id.bt_fresco_multiplexImg://本地圖片複用
break;
}
}
}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/sdv_fresco_multi"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginTop="48dp"
fresco:placeholderImage="@mipmap/ic_launcher" />
<Button
android:id="@+id/bt_fresco_multiImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="先顯示低解析度的圖,然後是高解析度的圖" />
<Button
android:id="@+id/bt_fresco_thumbnailImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="本地縮圖預覽" />
<Button
android:id="@+id/bt_fresco_multiplexImg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="本地圖片複用" />
</LinearLayout>
6.動態展示圖片:
AutoSizeActivity
package com.example.fresco.view;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.example.fresco.R;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
public class AutoSizeActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt_fresco_loadsmall;
private LinearLayout ll_fresco_icon;
private SimpleDraweeView mSimpleDraweeView;
private String Path = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1543329113431&di=551287617c4e82f27f19f82d7af4da57&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimage%2Fc0%253Dshijue1%252C0%252C0%252C294%252C40%2Fsign%3D5bdd825b5b0fd9f9b41a5d2a4d44be5f%2Fd62a6059252dd42a9811ac8c093b5bb5c9eab8f9.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auto_size);
initView();
mSimpleDraweeView = new SimpleDraweeView(this);
//設定圖片的寬高比,引數如果大於1,則寬度是高度的幾倍
mSimpleDraweeView.setAspectRatio(0.5f);
}
private void initView() {
bt_fresco_loadsmall = (Button) findViewById(R.id.bt_fresco_loadsmall);
ll_fresco_icon = (LinearLayout) findViewById(R.id.ll_fresco_icon);
bt_fresco_loadsmall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_fresco_loadsmall:
//1.設定圖片的地址
Uri parse = Uri.parse(Path);
//2.圖片請求
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(parse).build();
//3.載入圖片的控制
AbstractDraweeController build = Fresco.newDraweeControllerBuilder()
.setOldController(mSimpleDraweeView.getController())//記憶體優化
.setImageRequest(request)
.build();
//載入圖片
mSimpleDraweeView.setController(build);
ll_fresco_icon.addView(mSimpleDraweeView);
break;
}
}
}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/bt_fresco_loadsmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="載入圖片" />
<LinearLayout
android:id="@+id/ll_fresco_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
好了 到這裡就告一段落了,稍後我會為大家持續更新,希望能給大家帶來幫助,下面給大家分享一下Fresco的網址:
https://www.fresco-cn.org/