AndroidFresco和ButterKnife注解

Fresco

Fresco 是是Facebook开发的一个强大的图片加载组件和Picasso,Glide比较,它在内存管理方面有很大优势。它的图片是存储在Native层的,可避免OOM

一. 首先把组装武器的材料准备好,去我的百度网盘下载吧
https://pan.baidu.com/s/1xbVlZSO7gwCVmN_vGeDp3A
提取密码 asat

  1. AndroidStudio
    是一个Android集成开发工具,基于IntelliJ IDEA.。
    类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工具用于开发和调试。
  2. SDK (software development kit)软件开发工具包。
  3. gradle4.4 Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具。它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置。面向Java应用为主。

使用Fresco的步骤

第一步:添加依赖:
implementation 'com.facebook.fresco:fresco:1.11.0’
第二步:自定义一个Aplication 的子类 例如 DemoApplication//初始化Fresco
Fresco.initialize(this);
第三步:将DemoApplication注册到AndroidManifest.xml中。

第四步:在layout文件声明组件,使用框架中的SimpleDraweeView。
使用fresco的控件,需要在layout文件中添加上:
xmlns:fresco=“http://schemas.android.com/apk/res-auto”
注意: SimpleDraweeView的宽高不能为wrap_content,需要使用match_parent或者一个固定值。
第五步:在Activity中拿到控件。
第六步:产生图片的Uri。图片可以来自网络,asset目录,res目录,sd卡目录等。

Freso控件的一些默认的属性:

<com.facebook.drawee.view.SimpleDraweeView
android:layout_width=“20dp”
android:layout_height=“20dp”
fresco:fadeDuration=“300”// 淡出时间,毫秒。
fresco:actualImageScaleType=“focusCrop”// 等同于android:scaleType。
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"// 背景图,不会被View遮挡。

fresco:roundAsCircle=“false”// 是否是圆形图片。
fresco:roundedCornerRadius=“1dp”// 四角圆角度数,如果是圆形图片,这个属性被忽略。
fresco:roundTopLeft=“true”// 左上角是否圆角。
fresco:roundTopRight=“false”// 右上角是否圆角。
fresco:roundBottomLeft=“false”// 左下角是否圆角。
fresco:roundBottomRight=“true”// 左下角是否圆角。
fresco:roundingBorderWidth=“2dp”// 描边的宽度。
fresco:roundingBorderColor="@color/border_color" 描边的颜色。
/>
Freso的使用Demo。

布局页面
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:id="@+id/sdv"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:roundedCornerRadius=“20dp”
fresco:roundTopLeft=“true”
fresco:roundTopRight=“true”
fresco:roundBottomLeft=“true”
fresco:roundBottomRight=“true”
fresco:roundWithOverlayColor="@color/colorAccent"
fresco:roundingBorderWidth=“10dp”
fresco:roundingBorderColor="@color/colorPrimary"
/>

//加载图片
String imgPath = “https://img-my.csdn.net/uploads/201407/26/1406383299_1976.jpg”;

Uri uri = Uri.parse(imgPath);
mSdv.setImageURI(uri);

ButterKnife

地址 https://www.cnblogs.com/whoislcj/p/5620128.html
通过注解的方式 , 减少了代码中 findViewById以及setOnClickListener等代码量。
优势:
1,ButterKnife优势相比起xutils来,ButterKnife更受欢迎,在性能方面xutils完全是利用的反射,butterknife是轻量级的反射使用的注解都是编译时注解.
2,强大的View绑定和Click事件处理功能,简化代码,提升开发效率
3,方便的处理Adapter里的ViewHolder绑定问题
4,运行时不会影响APP效率,使用配置方便
代码清晰,可读性强

使用步骤:

第一步: 导入依赖

implementation ‘com.jakewharton:butterknife:8.8.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:8.8.1’

第二步: Activity的 onCreate方法中
unbinder = ButterKnife.bind(this);

onDestroy方法中
unbinder.unbind();

Fragment 在 onCreateView 中
unbinder = ButterKnife.bind(this,rootView);
onDestroy方法中
unbinder.unbind();
注意:Activity和Fragment的区别.
ButterKnife.bind()的调用在Activity中必须在setContentView之后
属性布局不能用private or static 修饰,否则会报错

第三步:使用
注解
– View的注解—
@BindView(布局页面中控件的ID)
TextView tv;

@BindViews({控件的id1,控件的id2})
List data;
—Resource注入 —
@BindBitmap(图片的id)//Bitmap注解使用
Bitmap bitmap;

@BindDrawable(图片的id)//Drawable注解使用
Drawable drawable;
//监听的使用

@OnClick(R.id.but_id)
public void onClick(View view)
{
Toast.makeText(this, “你点击了按钮”, Toast.LENGTH_SHORT).show();
}

@BindString(R.string.msg)//string注解使用
String message;

@BindArray(R.array.names)
String weeks[];//数组

@BindColor(R.color.colorPrimary)
int colorPrimary;//color注解使用

----事件的注解----
@OnClick(R.id.but_id)
public void click(){ }

• 可以写一个BaseActivity来调用ButterKnife.bind()方法,子类则不需要再进行bind操作
• 在8.4中ButterKnife移除了unBind方法,使用ButterKnife.bind(this)返回一个Unbinder的引用,通过Unbinder的unbind()方法进行解除绑定.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值