Android 繪制一個(gè)Loading動(dòng)畫__向圖片中緩慢填充顏色,從而形成動(dòng)畫效果 需求:制作一個(gè)加載動(dòng)畫,向一個(gè)不規(guī)則圖片圖形中從從下到上依次填充顏色,形成動(dòng)畫效果。 效果如下:
代碼如下: 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();// 獲取一個(gè)透明圖片 y = bitmap.getWidth();//初始化y軸坐標(biāo) } //改變裁剪區(qū)域 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(); 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;//結(jié)束繪制線程 super.onBackPressed(); } } |
|
來(lái)自: mandrave > 《Graphics》