• Android 绘制一个Loading动画__向图片中缓慢填充颜色,从而形成动画效果


    需求:制作一个加载动画,向一个不规则图片图形中从从下到上依次填充颜色,形成动画效果。

    效果如下:

       

    代码如下:

    LoadingAnimatorView.java

    package cn.yw.lib.animation;
    
    import cn.yw.lib.R;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    
    public class LoadingAnimatorView extends SurfaceView implements
            SurfaceHolder.Callback, Runnable {
        private SurfaceHolder holder;
        private Bitmap bitmap;
        private Paint paint1;
        private Paint paint2;
        public boolean flag = true;
        private int y = 100;
    
        public LoadingAnimatorView(Context context) {
            super(context);
            this.setFocusable(true);
            this.setFocusableInTouchMode(true);
            holder = this.getHolder();
            holder.addCallback(this);
            paint1 = new Paint();
            paint1.setColor(Color.RED);
            paint2 = new Paint();
            paint2.setColor(Color.GRAY);
            Bitmap bitmap1 = BitmapFactory.decodeStream(context.getResources()
                    .openRawResource(R.drawable.ic_launcher));
            bitmap = bitmap1.extractAlpha();// 获取一个透明图片
            y = bitmap.getWidth();//初始化y轴坐标
        }
      //改变裁剪区域
        private void playAnimator() {
            if (y > 0) {
                y-=3;
            }
        }
        
        private void drawLoadingAnimator() {
            Canvas canvas = null;
            try {
                canvas = holder.lockCanvas();
                if(canvas != null){
                    canvas.drawBitmap(bitmap, 100, 100,null);
                    canvas.drawColor(Color.GREEN);
                    canvas.drawBitmap(bitmap, 100, 100, paint2);
                    canvas.save();
              //裁剪 canvas.clipRect(
    100, y+100, bitmap.getWidth()+100, bitmap.getHeight()+100); canvas.drawBitmap(bitmap, 100, 100, paint1); canvas.restore(); } /* * Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); * Rect dst = new Rect(100, 100, bitmap.getWidth()+100, y+100); * canvas.drawBitmap(bitmap, src, dst, paint2); */ } catch (Exception e) { e.printStackTrace(); } finally { try{ if (holder != null) { holder.unlockCanvasAndPost(canvas); } }catch(Exception e){ e.printStackTrace(); } } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start();//开启绘制线程 } @Override public void surfaceDestroyed(SurfaceHolder holder) { }   //绘制动画线程 @Override public void run() { while (flag) { drawLoadingAnimator(); playAnimator(); try { Thread.sleep(200); } catch (Exception e) { e.printStackTrace(); } } } }

    LoadingAnimatorActivity.java

    package cn.yw.lib.animation;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    
    @SuppressLint("NewApi")
    public class LoadingAnimatorActivity extends Activity{
        private LoadingAnimatorView view;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            view = new LoadingAnimatorView(this);
            setContentView(view);
        }
        @Override
        public void onBackPressed() {
            view.flag = false;//结束绘制线程
            super.onBackPressed();
        }
    }
  • 相关阅读:
    2016/3/16 高级查询 ①连接查询 ②联合查询 ③子查询 无关 相关
    2016/3/13 七种查询 (普通查询 条件查询 排序查询 模糊查询 统计查询 分组查询 分页查询 )
    2016/3/13 MySQL 增删查改 CRUD 用代码实现
    2016/3/10 数据库简单操作( 创建数据库 创建表 数值类型 主键 外键 自动递增 )
    2016/3/10 PHP环境搭建 LAMP WAMP
    2016/3/10 PHP (超文本预处理器) 是什么?
    2016/3/1 淘宝 腾讯 网易 css初始化代码 以及最基础的初始化
    判断i在字符串中出现的次数(2016.1.12P141-1)
    2016-1-9作业——输出二维数组的和
    2016-1-8作业
  • 原文地址:https://www.cnblogs.com/tony-yang-flutter/p/3580090.html
Copyright © 2020-2023  润新知