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
中的子控件設(shè)置屬性動畫
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvViewHolder> {
private List<String> picUrl = new ArrayList<>();
private Context context;
private int mLastPosition = -1;
public RvAdapter() {
}
public void setPicUrl(List<String> 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;
}
/**
* 將動畫對象加入集合中 根據(jù)左右滑動加入不同
*/
private void addInAnimation(View view, boolean left) {
List<Animator> 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<Animator> 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 -> {//判斷內(nèi)存卡是否可以用
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線程去執(zhí)行查找圖片路徑
.map(t -> {//拿到圖片路徑的集合
List<String> 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);//將拿到的路徑集合進行回調(diào)
}
}