Android 闪图三种实现方式 动态闪

闪图作用不用说了:广告啊 公司标识之类的

1、静态图片放大动画

1)在activity中先插入动画效果方法

//放大图片
    public void animateImage() {

        //设置缩放动画
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(splash, "scaleX", 1f,
                1.5f);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(splash, "scaleY", 1f,
                1.5f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(2000).play(animatorX).with(animatorY);
        animatorSet.start();

        //动画完成监听
        animatorSet.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {

                //动画完成后跳转首页
                startActivity(new Intent(FlashActivity.this,MainActivity.class));
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
    }

2)在activity中开启子线程,延迟调用

2、Glide加载动态GIF图片

1)先在build.gradle添加依赖

implementation 'com.github.bumptech.glide:glide:3.7.0'

2)在activity中调用Glide方法加载,再延迟跳转

Glide.with(this).load(R.drawable.flash_gif).into(splash);

效果图:

3、VideoView加载asserts视频资源

1)先插入视频播放代码。

有小伙伴可能有疑惑,VideoView不是有进度条吗,只需要设置

mediaController.setVisibility(View.INVISIBLE);//隐藏播放进度条
  //视频播放
    private void playVideo(){
        mVideoView = (VideoView)this.findViewById(R.id.vid_splash);
        //获得下载目录,不推荐使用,api29已弃用,返回的是公共下载目录,需要声明存储权限
        //File directory_doc = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        //获得下载目录,返回的是应用下载目录
        String filePath=getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()+"/video";
        Log.d("播放地址",filePath);
        Deposit(filePath,"video_splash.mp4");
        String path=filePath+"/video_splash.mp4";
        Log.d("播放地址",path);
        mVideoView.setVideoPath(path);
        //创建MediaController对象
        MediaController mediaController = new MediaController(this);
        mediaController.setVisibility(View.INVISIBLE);//隐藏播放进度条
        //绑定mediaController
        mVideoView.setMediaController(mediaController);
        mVideoView.requestFocus();
        mVideoView.start();
    }

2)插入asserts资源写进应用目录代码

asserts要有视频资源

 代码中视频文件名改为自己的

 /**
     * byte数组的初始化,数组的长度为1024, 从你的代码看来表达的是每次从文件读取1024个字节。
     * 8bit(位)是1byte(字节)
     * 1024byte(字节)是1kb
     * byte[] bytes = new byte[1024]是1kb
     * */
    //将assets内文件存储到本地下载目录
    public Boolean Deposit(String path,String fileName){
        InputStream inputStream;
        try {
            //判断文件是否存在
            File file1=new File(path + "/" + fileName);

            if(!file1.exists()){
                inputStream=getAssets().open(fileName);
                File file = new File(path);
                //当目录不存在时创建目录
                if(!file.exists()){
                    file.mkdirs();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(path + "/" + fileName);// 构建FileOutputStream对象,文件不存在会自动新建
                byte[] buffer = new byte[1024];//每次从文件读取1024个字节。
                int count = 0;
                while ((count = inputStream.read(buffer)) > 0) {
                    fileOutputStream.write(buffer, 0, count);//write(byte[] b, int off, int len) 将byte数组中的 索引位置在off到len之间的数据  写出到流通道
                }
                fileOutputStream.flush();
                fileOutputStream.close();
                inputStream.close();

            }else {
                Log.d("adam:","视频资源已存在");
            }
            return true;

        }catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }

3)activity中调用。

效果如下:

 细心的小伙伴可能发现效果图都是灰色的,因为最近某伟人去世了,各大app都灰朦朦的,这里浅试了下。

采用的方式是View的硬件加速方案,view.setLayerType(layerType,null),第二个参数刚好是Paint

实现代码:

/**
 * 硬件加速方案,设置黑灰
 * view.setLayerType(layerType, null);
 * 第二个参数是Paint
 * Activity 可以拿到 decorView,一般在BaseActivity中的onCreate()方法里调用即可
 * getWindow().getDecorView()即可拿到decorView
 * */
public class GrayManager {

    private static GrayManager sInstance;
    private Paint mGrayPaint;
    private ColorMatrix mGrayMatrix;

    public static GrayManager getInstance() {
        if (sInstance == null) {
            synchronized (GrayManager.class) {
                if (sInstance == null) {
                    sInstance = new GrayManager();
                }
            }
        }
        return sInstance;
    }

    //初始化
    public void init() {
        mGrayMatrix = new ColorMatrix();
        mGrayPaint = new Paint();
        mGrayMatrix.setSaturation(0);
        mGrayPaint.setColorFilter(new ColorMatrixColorFilter(mGrayMatrix));
    }

    //硬件加速置灰方法
    public void setLayerGrayType(View view) {
        if (mGrayMatrix == null || mGrayPaint == null) {
            init();
        }
        view.setLayerType(View.LAYER_TYPE_HARDWARE, mGrayPaint);
    }
}

1)一般在BaseActivity里的onCreate()方法调用即可,其它activity再继承于BaseActivity。你该不会没有吧

 2)除非你没有,也可以在Application中的ActivityLifecycleCallbacks 实现监听。

 首先实现这个接口Application.ActivityLifecycleCallbacks对activity生命周期进行监听,在下面方法调用硬件加速方法

 然后在application初始化的時候注冊监听

效果如下:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值