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(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。