RecyclerView的item使用属性动画。
1.配置使用Java8
在Moudule
的build.gradle
中
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
compileOptions{ //添加
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.szlk.recyclerviewanimation"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
jackOptions{
enabled true //启用jack编译器
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
2.Adapter代码
主要是在onBindViewHolder()
方法中,直接给itemView
中的子控件设置属性动画
public class RvAdapter extends RecyclerView.Adapter {
private List picUrl = new ArrayList<>();
private Context context;
private int mLastPosition = -1;
public RvAdapter() {
}
public void setPicUrl(List picUrl) {
if (picUrl != null) {
this.picUrl.clear();
this.picUrl.addAll(picUrl);
notifyDataSetChanged();
}
}
@Override
public RvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
this.context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
RvViewHolder rvViewHolder = new RvViewHolder(view);
rvViewHolder.setIsRecyclable(true);
return rvViewHolder;
}
@Override
public void onBindViewHolder(RvViewHolder holder, int position) {
if (context == null) return;
holder.iv.post(() ->
Glide.with(context).load(picUrl.get(position))
.override(holder.iv.getMeasuredWidth(), holder.iv.getMeasuredHeight())
.centerCrop()
.into(holder.iv)
);
boolean b = Integer.compare(position,mLastPosition) < 0 ? true : false;
addInAnimation(holder.itemView, b);
mLastPosition = position;
}
/**
* 将动画对象加入集合中 根据左右滑动加入不同
*/
private void addInAnimation(View view, boolean left) {
List list = new ArrayList<>();
if (left) {
list.add(ObjectAnimator.ofFloat(view, "translationX", -view.getMeasuredWidth() * 2, 0));
} else {
list.add(ObjectAnimator.ofFloat(view, "translationX", view.getMeasuredWidth() * 2, 0));
}
list.add(ObjectAnimator.ofFloat(view, "translationY", view.getMeasuredHeight() / 2, 0));
list.add(ObjectAnimator.ofFloat(view, "alpha", 0, 1));
list.add(ObjectAnimator.ofFloat(view, "scaleX", 0.25f, 1));
list.add(ObjectAnimator.ofFloat(view, "scaleY", 0.25f, 1));
startAnimation(list);
}
/**
* 开启动画
*/
private void startAnimation(List list) {
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(list);
animatorSet.setInterpolator(new DecelerateInterpolator());
animatorSet.start();
}
@Override
public int getItemCount() {
return picUrl.size();
}
class RvViewHolder extends RecyclerView.ViewHolder {
private ImageView iv;
public RvViewHolder(View itemView) {
super(itemView);
iv = (ImageView) itemView.findViewById(R.id.iv_item_layout);
}
}
}
3.Activity代码
尝试使用了Lambda
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
RecyclerView rv = (RecyclerView) findViewById(R.id.rv_activity);
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
rv.setLayoutManager(manager);
rv.addItemDecoration(new RvItemDecoration(5));
rv.setItemAnimator(new DefaultItemAnimator());
RvAdapter adapter = new RvAdapter();
rv.setAdapter(adapter);
getPicList(adapter, "image/jpeg,image/png");
}
private void getPicList(RvAdapter adapter, String type) {
Observable
.just(type)
.filter(s -> {//判断内存卡是否可以用
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
ToastUtils.show(MainActivity.this, "no sdCard");//已在主线程
return false;
}
return true;
})
.subscribeOn(AndroidSchedulers.mainThread())//切换到主线程 Toast
.subscribeOn(Schedulers.io())//切换到io线程去执行查找图片路径
.map(t -> {//拿到图片路径的集合
List list = new ArrayList<>();
Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = MainActivity.this.getContentResolver();
//查找类型
String[] types = type.split(",");
String selection = MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=?";
//得到Cursor
Cursor cursor = contentResolver.query(imgUri, null, selection, types, MediaStore.Images.Media.DATE_MODIFIED);
while (cursor.moveToNext()) {
// 拿到每个图片的路径
String picPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
list.add(picPath);
}
cursor.close();
//将集合倒序
Collections.reverse(list);
return list;
})
.observeOn(AndroidSchedulers.mainThread())//切换到主线程
.subscribe(adapter::setPicUrl);//将拿到的路径集合进行回调
}
}