Fresco实现图片放大

这里用的是图片框架Fresco

首先加依赖

compile 'com.facebook.fresco:fresco:1.2.0'
compile 'me.relex:photodraweeview:1.1.2'

在清单文件中,加联网权限

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

在MainActivity的xml里面写布局,值得注意的是图片的长高都是需要固定值或者match_parent
 
  
<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/sd_view"
    android:clickable="true"
    android:layout_centerInParent="true"
    android:layout_width="300dp"
    android:layout_height="300dp"/>
下面是在MainActivity类里面写展示图片的代码
onCreate()方法里面
initView();//初始化图片
initData();//将图片和框架绑定
initEvent();//设置点击变大的功能
 
  
 
  
 
  
之后写

private void initView() {

    mImageView = findViewById(R.id.sd_view);
}

private void initData() {
    //绑定
 mImageView.setImageURI(IMG_URL);
}

private void initEvent() {

    mImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this,SuccessActivity.class);
            intent.putExtra("img_url",IMG_URL);
            startActivity(intent);
        }
    });

}

 
  
SuccessActivity类的xml中
<me.relex.photodraweeview.PhotoDraweeView
    android:id="@+id/photoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 
  
 
  
SuccessActivity类
onCreate()方法里面
initView();
initData();
initEvent();
之后写

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

private void initData() {

    img_url = getIntent().getStringExtra("img_url");

    if(!TextUtils.isEmpty(img_url)){
        PipelineDraweeControllerBuilder controller = Fresco.newDraweeControllerBuilder();
        controller.setUri(img_url);//设置图片URL
        controller.setOldController(photoView.getController());
        controller.setControllerListener(new BaseControllerListener(){

            @Override
            public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
                super.onFinalImageSet(id, imageInfo, animatable);
                if(imageInfo == null || photoView == null){
                    return;
                }
                photoView.update(imageInfo.getWidth(),imageInfo.getHeight());
            }
        });
        photoView.setController(controller.build());
    }else{
        Toast.makeText(this, "图片获取失败", Toast.LENGTH_SHORT).show();
    }

}

private void initEvent() {
    //点击事件
    photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
        @Override
        public void onPhotoTap(View view, float x, float y) {
            finish();
        }
    });
}













你可能感兴趣的:(记录)