public BoWenView(Context context, AttributeSet attrs) { super(context, attrs); init(); }
private void init() { // 画笔初始化 mPaint = new Paint(); mPaint.setAntiAlias(true); // 发送消息,开始动画循环 mHandler.sendEmptyMessage(0); }
protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 计算半径=较长边的1/12加1 radius = w > h ? w / 12 + 1 : h / 12 + 1; // 计算中心坐标 cX = w / 2; cY = h / 2; // 初始化环形着色器 radialGradient = new RadialGradient(w / 2, h / 2, radius, 0X88FFFFFF, 0XAAFFFFFF, TileMode.REPEAT); mPaint.setShader(radialGradient); // 初始化中心图像的位置 bitmapRectF = new RectF(cX - radius, cY - radius, cX + radius, cY + radius); }
private Bitmap centerBitmap; public void setCenterBitmap(Bitmap bitmap) { centerBitmap = bitmap; invalidate(); }
Handler mHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { Log.e("Bowen", "handleMessage"); if (scale >= 6) { scale = 0; } scale++; invalidate(); mHandler.sendEmptyMessageDelayed(0, 500); return false; } });
protected void onDraw(Canvas canvas) { //画圆,根据sacle来控制大小 canvas.drawCircle(cX, cY, cX > cY ? cX * scale / 6 : cY * scale / 6, mPaint); //有设置图像的话就在中间画出来 if (centerBitmap != null) { canvas.drawBitmap(centerBitmap, null, bitmapRectF, null); } }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //设置中心图像 ((BoWenView) findViewById(R.id.bowen)).setCenterBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.dun)); }
protected void onWindowVisibilityChanged(int visibility) { super.onWindowVisibilityChanged(visibility); if (visibility == View.GONE) { Log.e("Bowen", "Window-GONE"); mHandler.removeMessages(0); } else if (visibility == View.VISIBLE) { Log.e("Bowen", "Window-VISIBLE"); mHandler.sendEmptyMessage(0); } }
private void init() { // 画笔初始化 mPaint = new Paint(); mPaint.setAntiAlias(true); // 发送消息,开始动画循环 //mHandler.sendEmptyMessage(0); }
//我就是传说中的 //略...
原文地址:http://blog.csdn.net/aqswde35025/article/details/43561977