随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

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();
    }
}
复制代码

 

posted on   飘杨......  阅读(4999)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示