Android自定义观点实现叶子飘动旋转效果(四)

  

上一篇实现了叶子飘动功能,《Android自定义叶子飘动》现在实现旋转效果

  

 Android自定义观点实现叶子飘动旋转效果(四)

  

要实现这个效果,要在之前的功能上添加2个功能

  

1,通过矩阵。postTranslate (int x, int y)在添加在y轴上滑动

  

2,通过矩阵。postRotate(浮度,px浮动,浮动py)实现叶子旋转

  

代码实现

  

<强> 1,获取Y坐标

        私人浮动getMatrixY () {   浮动w=(浮动)((浮动)2 *数学。π/宽度);   int y=(int)(18 *数学。罪(w * getMatrixX ())) + (height-mLeafHeight)/2;   返回y;   }   之前      

sin (Math.PI ....)的取值范围貌似是1 ~ 1之间。通过X坐标在整个宽度的比例,获取到Y坐标的比例。

  

这里方法有很多,我这边叶子Y坐标默认在Y轴中间,然后上下+ -18 px实现在Y轴的滑动,18滑动浮动比较大了

        公共LeafView(上下文语境,AttributeSet attrs) {   超级(上下文,attrs);   mResources=getresource ();   bgBitmap=((BitmapDrawable) mResources.getDrawable (R.drawable。leaf_kuang, null)) .getBitmap ();   leafBitmap=((BitmapDrawable) mResources.getDrawable (R.drawable。叶,null))) .getBitmap ();   mLeafHeight=leafBitmap.getWidht ();      bgPaint=new油漆();   bgPaint.setColor (mResources.getColor (R.color.bg_color));   }      @Override   保护无效alt=" Android自定义观点实现叶子飘动旋转效果(四)">

  

<强> 2,实现旋转

  

主要通过矩阵。postRotate(浮度,px浮动,浮动py)

  

度就是角度(0 ~ 360),px, py就是图片的中心点

        私人int getRotate () {   浮动比例=((开始时间- System.currentTimeMillis()))加工机之细加工约%/加工机之细加工约(浮动);   int旋转=(int) * 360(规模);   返回旋转;   }   之前      

同样,通过当前叶子在X轴的比例,来计算出旋转的角度(0 ~ 360)

  

完整代码:

        公开课LeafView扩展视图{   mResources私人资源;   私人位图mLeafBitmap bgBitmap;   私人int宽度、高度;   私人int mLeafWidth mLeafHeight;   私人油漆bgPaint;   私人RectF bgRect;   私人矩形bgDestRect;      公共LeafView(上下文语境,AttributeSet attrs) {   超级(上下文,attrs);   mResources=getresource ();   mLeafBitmap=((BitmapDrawable) mResources.getDrawable (R.drawable。叶,null) .getBitmap ();   mLeafWidth=mLeafBitmap.getWidht ();   mLeafHeight=mLeafBitmap.getHeight ();   bgBitmap=((BitmapDrawable) mResources.getDrawable (R.drawable。leaf_kuang, null)) .getBitmap ();      bgPaint=new油漆();   bgPaint.setColor (mResources.getColor (R.color.bg_color));   }      @Override   保护无效onSizeChanged (w int, int, int oldw int oldh) {   超级。onSizeChanged (w h oldw oldh);   宽度=w;   身高=h;   bgDestRect=new矩形(0,0,宽度、高度);   }      @Override   保护无效onDraw(帆布画布){   super.onDraw(画布);   bgRect=new RectF(0, 0,宽度、高度);//添加黄色白金   画布。绘制矩形(bgRect bgPaint);//添加背景图片   画布。drawBitmap (bgDestRect bgBitmap,空,空);      canvas.save ();   矩阵矩阵=new矩阵();//添加滑动   getMatrixY matrix.postTranslate (getMatrixX () ());//添加旋转   getMatrixX matrix.postRotate (getRotate () () + mLeafWidth/2, getMatrixY () + mLeafHeight/2);   画布。drawBitmap (mLeafBitmap、矩阵、新油漆());   canvas.restore ();   postInvalidate ();      }   加工机之细加工约长=5000;//叶子滑动一周的时间5秒   长时间的开始时间=0;   私人浮动getMatrixX () {   浮动betweenTime=开始时间- System.currentTimeMillis ();//周期结束再加一个。加工机之细加工约   如果(betweenTime & lt;0){   开始时间=System.currentTimeMillis加工机之细加工约()+;   betweenTime加工机之细加工约=;   }//通过时间差计算出叶子的坐标   浮动比例=(浮动)betweenTime加工机之细加工约/;   浮动x=(int)(宽*分数);   返回x;   }   私人浮动getMatrixY () {   浮动w=(浮动)((浮动)2 *数学。π/宽度);   int y=(int)(18 *数学。罪(w * getMatrixX ())) + (height-mLeafHeight)/2;   返回y;   }   私人int getRotate () {   浮动比例=((开始时间- System.currentTimeMillis()))加工机之细加工约%/加工机之细加工约(浮动);   int旋转=(int) * 360(规模);   返回旋转;   }   }         

Android自定义观点实现叶子飘动旋转效果(四)