fresco图片框架简单使用

   //fresco基础依赖
    implementation 'com.facebook.fresco:fresco:0.14.1'

    //播放gif图   (如果用不到gif图就不用导入依赖)
    implementation 'com.facebook.fresco:animated-gif:0.14.1'
//fresco 的弊端就是宽和高不能设置为wrap_content;
//主布局



     
//图片复用布局   



    

    
//gif




    

    
//进度条加载图片布局



    
    


.//设置圆形图片



    

    
//初始化继承Application
package com.example.fresco;

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;

 
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

//复用图片.java
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 ClippingActivity 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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clipping);
        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:
                Uri lowUri = Uri.parse("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=91883135,2320948421&fm=111&gp=0.jpg");
                Uri highUri = Uri.parse("https://img5.duitang.com/uploads/item/201312/03/20131203153823_Y4y8F.jpeg");
                //控制加载的图片控制器
                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() + "big.jpg"));
                break;
            case R.id.bt_fresco_multiplexImg:

                break;
        }
    }
}

//gif图片.java
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;

public class GifActivity extends AppCompatActivity implements View.OnClickListener {

    private SimpleDraweeView gif;
    private Button askImg;
    private Button stopAnim;
    private Button startAnim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gif);
        initView();
    }

    private void initView() {
        gif = (SimpleDraweeView) findViewById(R.id.gif);
        askImg = (Button) findViewById(R.id.askImg);
        stopAnim = (Button) findViewById(R.id.stopAnim);
        startAnim = (Button) findViewById(R.id.startAnim);

        askImg.setOnClickListener(this);
        stopAnim.setOnClickListener(this);
        startAnim.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.askImg:
                Uri uri = Uri.parse("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1514259038111&di=312e8557563f428353811cc457f96ee3&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20161116%2Ffc10ee2abef545c7bbd6f46a09c20ed2_th.gif");

                AbstractDraweeController build = Fresco.newDraweeControllerBuilder()
                        .setUri(uri)
                        .setAutoPlayAnimations(true)//是否播放动画
                        .setOldController(gif.getController())//内存优化
                        .build();

                gif.setController(build);
                break;
            case R.id.stopAnim://停止动画
                Animatable animatable1 = gif.getController().getAnimatable();
                animatable1.stop();
                break;
            case R.id.startAnim://开始动画
                if(gif.getController() != null){
                    Animatable animatable = gif.getController().getAnimatable();
                    if(animatable != null && !animatable.isRunning()){
                        animatable.start();
                    }
                }
                break;
        }
    }
}

//进度条展示图片.java
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 ProgresActivity extends AppCompatActivity {

    private SimpleDraweeView simple;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progres);
        initView();

        //
        Uri parse = Uri.parse("http://p0.ifengimg.com/pmop/2018/0706/C8E15071A7A6A62D086340B910F38682EDC95DCD_size183_w1024_h1535.jpeg");

        //创建Build对象
        GenericDraweeHierarchyBuilder genericDraweeHierarchyBuilder = new GenericDraweeHierarchyBuilder(getResources());
        //通过这个对象设置今天的样式
        GenericDraweeHierarchy build = genericDraweeHierarchyBuilder.setProgressBarImage(new ProgressBarDrawable()).build();

        //把样式设置给图片控件
        simple.setHierarchy(build);
        //加载图片成功
        simple.setImageURI(parse);

    }

    private void initView() {
        simple = (SimpleDraweeView) findViewById(R.id.simple);
    }
}

//设置圆形,圆角图片.java
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 RotundityActivity extends AppCompatActivity implements View.OnClickListener {

    private SimpleDraweeView simple;
    private Button circle;
    private Button corner;
    Uri mUri;
    private RoundingParams mParams;
    GenericDraweeHierarchyBuilder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotundity);
        initView();
      mUri = Uri.parse("http://img4q.duitang.com/uploads/item/201304/27/20130427043538_wAfHC.jpeg");

        //创建Builder对象
         builder = new GenericDraweeHierarchyBuilder(getResources());
    }

    private void initView() {
        simple = (SimpleDraweeView) findViewById(R.id.simple);
        circle = (Button) findViewById(R.id.circle);
        corner = (Button) findViewById(R.id.corner);

        circle.setOnClickListener(this);
        corner.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.circle:
                //设置形状对象,形状为圆形
                mParams = RoundingParams.asCircle();
                //把形状设置给参数对象
                GenericDraweeHierarchy rounding = builder.setRoundingParams(mParams).build();
                //设置给图片控件
                simple.setHierarchy(rounding);
                //加载图片控件
                simple.setImageURI(mUri);
                break;
            case R.id.corner:
                RoundingParams roundingParams = RoundingParams.fromCornersRadius(50f);
                GenericDraweeHierarchy build = builder.setRoundingParams(roundingParams).build();
                simple.setHierarchy(build);
                simple.setImageURI(mUri);
                break;
        }
    }
}

//mainactivity.java
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.ClippingActivity;
import com.example.fresco.view.GifActivity;
import com.example.fresco.view.ProgresActivity;
import com.example.fresco.view.RotundityActivity;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button progress;
    private Button clipping;
    private Button gif;
    private Button rotundity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        progress = (Button) findViewById(R.id.progress);
        clipping = (Button) findViewById(R.id.clipping);
        gif = (Button) findViewById(R.id.gif);
        rotundity = (Button) findViewById(R.id.rotundity);
        progress.setOnClickListener(this);
        clipping.setOnClickListener(this);
        gif.setOnClickListener(this);
        rotundity.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.progress:
                Intent intent = new Intent(MainActivity.this,ProgresActivity.class);

                startActivity(intent);

                break;
            case R.id.clipping:
                Intent intent1 = new Intent(MainActivity.this,ClippingActivity.class);

                startActivity(intent1);
                break;
            case R.id.gif:

                Intent intent2 = new Intent(MainActivity.this,GifActivity.class);

                startActivity(intent2);
                break;
            case R.id.rotundity:
                Intent intent3 = new Intent(MainActivity.this,RotundityActivity.class);

                startActivity(intent3);
                break;
        }
    }
}

你可能感兴趣的:(fresco图片框架简单使用)