android loaging 动画,Android 绘制一个Loading动画
packagecn.yw.lib.animation;importcn.yw.lib.R;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Color
packagecn.yw.lib.animation;importcn.yw.lib.R;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.view.SurfaceHolder;importandroid.view.SurfaceView;public class LoadingAnimatorView extends SurfaceView implementsSurfaceHolder.Callback, Runnable {privateSurfaceHolder holder;privateBitmap bitmap;privatePaint paint1;privatePaint paint2;public boolean flag = true;private int y = 100;publicLoadingAnimatorView(Context context) {super(context);this.setFocusable(true);this.setFocusableInTouchMode(true);
holder= this.getHolder();
holder.addCallback(this);
paint1= newPaint();
paint1.setColor(Color.RED);
paint2= newPaint();
paint2.setColor(Color.GRAY);
Bitmap bitmap1=BitmapFactory.decodeStream(context.getResources()
.openRawResource(R.drawable.ic_launcher));
bitmap= bitmap1.extractAlpha();//获取一个透明图片
y =bitmap.getWidth();//初始化y轴坐标
}
//改变裁剪区域private voidplayAnimator() {if (y > 0) {
y-=3;
}
}private voiddrawLoadingAnimator() {
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();
}
}
}
@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, intwidth,intheight) {
}
@Overridepublic voidsurfaceCreated(SurfaceHolder holder) {new Thread(this).start();//开启绘制线程
}
@Overridepublic voidsurfaceDestroyed(SurfaceHolder holder) {
}
//绘制动画线程
@Overridepublic voidrun() {while(flag) {
drawLoadingAnimator();
playAnimator();try{
Thread.sleep(200);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
更多推荐
所有评论(0)