第三方框架--加载网络图片
2016-05-22 10:57
471 查看
第三方框架都需要导入相关的jar包,我们用as的搜索功能就可以从网上下载自动导入了。
要使用ImageLoader必须先实现全局的实例化,这样可以避免每次使用都要重复代码
我们自定义一个MyApplication继承自Application,代码如下
然后需要在Manifest文件中写上代码:
然后再代码中使用就可以了
效果图
使用这个框架需要导入两个包
同时我们在布局中引入这个框架中自带的SimpleDraweeView控件
注意这个使用的时候不要将它的宽高设置为包含内容,它不支持这个属性,我们必须设置具体的宽高才可以
关于SimpleDraweeView 的相关属性
fresco是自定义的属性标签
如果用此框架加载静态图片的话,直接使用此框架的空间setImageUri的方法就可以了
https://github.com/chrisbanes/PhotoView
这里需要注意
也可以参考这个博客的写法,
https://github.com/bm-x/PhotoView
使用这个博客的写法,必须导入下面这个包。具体的使用参见上面的网址介绍。
compile ‘com.bm.photoview:library:1.4.0’
ImageLoader
要使用ImageLoader必须先实现全局的实例化,这样可以避免每次使用都要重复代码
我们自定义一个MyApplication继承自Application,代码如下
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration=ImageLoaderConfiguration.createDefault(getApplicationContext()); ImageLoader.getInstance().init(configuration); } }
然后需要在Manifest文件中写上代码:
//就是这一行代码,要加在application标签下 android:name=".MyApplication"
然后再代码中使用就可以了
String urls = "http://p0.qhimg.com/t015f3654b694ad2f8a.jpg"; DisplayImageOptions options = new DisplayImageOptions.Builder() .cacheInMemory(true)//在内存中缓存 .cacheOnDisc(true)//存储卡的缓存,需要添加相应的存储卡读写权限 .showImageOnFail(R.mipmap.ic_launcher)//加载失败的显示的图片 .showImageOnLoading(R.mipmap.ic_launcher)//加载过程中显示的图片 .build(); ImageLoader.getInstance().displayImage(urls, imageView, options);//加载图片
Picasso
Picasso.with(getApplicationContext()) // .load("http://p0.qhimg.com/t015f3654b694ad2f8a.jpg") //加载地址 .placeholder(R.mipmap.ic_launcher) //占位图 .error(R.mipmap.ic_launcher) //加载失败的图 .fit() //充满 .tag(getApplicationContext()) //标记 .into(imageView);//加载到的ImageView
Glide
Glide.with(this).load("http://p0.qhimg.com/t015f3654b694ad2f8a.jpg").into(imageView);
效果图
Fresco
这个框架可以用于加载网络上的gif图片使用这个框架需要导入两个包
同时我们在布局中引入这个框架中自带的SimpleDraweeView控件
注意这个使用的时候不要将它的宽高设置为包含内容,它不支持这个属性,我们必须设置具体的宽高才可以
public class MainActivity extends AppCompatActivity implements View.OnClickListener { //注意导的哪一个包名,有恨多个,别混乱了 //一共导入了两个包啊,千万记住了啊,用来加载gif动图的框架 private Button button, button2; private SimpleDraweeView simpleDraweeView; //simpleDraweeView控件不支持包含内容的宽高设置,必须设置明确的大小 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //由于我们在布局中使用了第三方框架的控件,所以我们必须在设置布局之前进行第三方框架的initialize()方法 Fresco.initialize(this); setContentView(R.layout.activity_main); init(); } private void init() { button = (Button) findViewById(R.id.button); button.setOnClickListener(this); simpleDraweeView = (SimpleDraweeView) findViewById(R.id.simpleDraweeView); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: DraweeController animatedGifController = Fresco.newDraweeControllerBuilder() //设置使其支持动画 .setAutoPlayAnimations(true) .setUri(Uri.parse("http://img4.duitang.com/uploads/item/201306/10/20130610100354_3WrWN.gif")) .build(); simpleDraweeView.setController(animatedGifController); break; } } }
关于SimpleDraweeView 的相关属性
fresco是自定义的属性标签
xmlns:fresco="http://schemas.android.com/apk/res-auto"
<com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="20dp" android:layout_height="20dp" fresco:fadeDuration="300" fresco:actualImageScaleType="focusCrop" fresco:placeholderImage="@color/wait_color" fresco:placeholderImageScaleType="fitCenter" fresco:failureImage="@drawable/error" fresco:failureImageScaleType="centerInside" fresco:retryImage="@drawable/retrying" fresco:retryImageScaleType="centerCrop" fresco:progressBarImage="@drawable/progress_bar" fresco:progressBarImageScaleType="centerInside" fresco:progressBarAutoRotateInterval="1000" fresco:backgroundImage="@color/blue" fresco:overlayImage="@drawable/watermark" fresco:pressedStateOverlayImage="@color/red" fresco:roundAsCircle="false" fresco:roundedCornerRadius="1dp" fresco:roundTopLeft="true" fresco:roundTopRight="false" fresco:roundBottomLeft="false" fresco:roundBottomRight="true" fresco:roundWithOverlayColor="@color/corner_color" fresco:roundingBorderWidth="2dp" fresco:roundingBorderColor="@color/border_color" />
fadeDuration 淡入淡出的时间 actualImageScaleType 图片的填充方式 placeholderImage 预加载图片 placeholderImageScaleType 预加载图片填充方式 failureImage 加载失败图片 failureImageScaleType 失败图片填充方式 retryImage 重试图片 retryImageScaleType 重试图片填充方式 progressBarImage 加载进度图片 progressBarImageScaleType 进度图片填充方式 progressBarAutoRotateInterval 自动旋转间隔 backgroundImage 背景图片 overlayImage 覆盖图片 pressedStateOverlayImage 按下时覆盖图 roundAsCircle 是否为圆形图 roundedCornerRadius 圆角弧度 roundTopLeft 左上角弧度 roundWithOverlayColor 圆角的覆盖色 roundingBorderWidth 圆角的边框宽度 roundingBorderColor 圆角边框颜色
如果用此框架加载静态图片的话,直接使用此框架的空间setImageUri的方法就可以了
PhotoView
关于这个框架的使用在这里https://github.com/chrisbanes/PhotoView
Drawable bitmap = getResources().getDrawable(R.mipmap.ic_launcher); imageView.setImageDrawable(bitmap); PhotoViewAttacher mAttacher = new PhotoViewAttacher(imageView);
这里需要注意
// If you later call mImageView.setImageDrawable/setImageBitmap/setImageResource/etc then you just need to call mAttacher.update();
也可以参考这个博客的写法,
https://github.com/bm-x/PhotoView
使用这个博客的写法,必须导入下面这个包。具体的使用参见上面的网址介绍。
compile ‘com.bm.photoview:library:1.4.0’