在线直播系统源码,多图加载成动画的形式如何实现
作者:互联网
在线直播系统源码,多图加载成动画的形式如何实现
1、设置
animationDrawable.addFrame(drawable, 1000);//添加图片生成的drawable,时间为1000ms
animationDrawable.setOneShot(false);//设置是否播放一次,false表示会一直循环播放
animationDrawable.setEnterFadeDuration(200);//进入时动画前摇时间,时间为200ms
animationDrawable.setExitFadeDuration(200);//退出时动画后摇时间,时间为200ms
初始化和图片资源设置完成后
animationDrawable.start();
2、停止播放
animationDrawable.stop();
3、全部代码
package com.mz.demo.ui.home;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mz.demo.databinding.FragmentHomeBinding;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
public class HomeFragment extends Fragment {
private Activity activity;
private FragmentHomeBinding binding;
private AnimationDrawable animationDrawable;
private final List<String> uriList = new ArrayList<>();
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
activity = getActivity();
binding = FragmentHomeBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onStart() {
super.onStart();
requestPermission();
}
@Override
public void onStop() {
super.onStop();
if (animationDrawable != null) {
animationDrawable.stop();
animationDrawable = null;//置空释放资源
}
}
private void requestPermission() {
//读取SD权限申请
if (activity.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
readImagePath();
}
private void readImagePath() {
uriList.clear();
//获取该路径下所有文件
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/demo");
String[] paths = file.list();
if (paths == null)
return;
uriList.addAll(Arrays.asList(paths));
animationDrawable = new AnimationDrawable();
for (String uri : uriList) {
Bitmap bitmap = BitmapFactory.decodeFile(file + "/" + uri);
Drawable drawable = new BitmapDrawable(null, bitmap);
animationDrawable.addFrame(drawable, 1000);
animationDrawable.setOneShot(false);
animationDrawable.setEnterFadeDuration(200);
animationDrawable.setExitFadeDuration(200);
binding.imageView.setImageDrawable(animationDrawable);
animationDrawable.start();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="16:9"
android:layout_margin="30dp"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
这里的图片资源是是从SD卡的文件夹下加载的,也可以换成网络图片。
以上就是在线直播系统源码,多图加载成动画的形式如何实现, 更多内容欢迎关注之后的文章
标签:动画,layout,animationDrawable,app,多图,源码,import,android,drawable 来源: https://www.cnblogs.com/yunbaomengnan/p/16284540.html