Android如何实现自定义View图片按Path运动和旋转效果
导读:本文共1738字符,通常情况下阅读需要6分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 具体内容如下View:/***author:stone*email:aa86799@163.com*time:16/5/291529*/publicclassEarthPathViewextendsView{privatePathmPath;privatePaintmPaint;privateBitmapmBitmap;privatePath... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。具体内容如下
View:
/***author:stone*email:aa86799@163.com*time:16/5/291529*/publicclassEarthPathViewextendsView{privatePathmPath;privatePaintmPaint;privateBitmapmBitmap;privatePathMeasuremPathMeasure;privatefloat[]mPoint;privatefloat[]mTan;privatefloatmDdegrees;publicEarthPathView(Contextcontext){this(context,null);}publicEarthPathView(Contextcontext,AttributeSetattrs){this(context,attrs,0);}publicEarthPathView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);mPaint=newPaint();mPaint.setColor(Color.RED);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(10);InputStreamis=getResources().openRawResource(R.drawable.earth);mBitmap=BitmapFactory.decodeStream(is);}publicvoidsetPath(Pathpath){mPath=path;mPathMeasure=newPathMeasure(path,false);mPoint=newfloat[2];mTan=newfloat[2];}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);if(mPath==null){return;}canvas.rotate(mDdegrees+=2,getWidth()/2,getHeight()/2);canvas.drawPath(mPath,mPaint);floatdegress=(float)Math.toDegrees(Math.atan2(mTan[1],mTan[0]));Matrixmatrix=newMatrix();matrix.postRotate(degress,mBitmap.getWidth()/2,mBitmap.getHeight()/2);matrix.postTranslate(mPoint[0]-mBitmap.getWidth()/2,mPoint[1]-mBitmap.getHeight()/2);canvas.drawBitmap(mBitmap,matrix,null);}@TargetApi(Build.VERSION_CODES.HONEYCOMB)publicvoidstartAnim(){ValueAnimatoranimator=ValueAnimator.ofFloat(0,mPathMeasure.getLength());animator.setDuration(2000);animator.setInterpolator(newLinearInterpolator());//插值器animator.setRepeatCount(ValueAnimator.INFINITE);animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){@OverridepublicvoidonAnimationUpdate(ValueAnimatoranimation){floatdistance=(float)animation.getAnimatedValue();mPathMeasure.getPosTan(distance,mPoint,mTan);invalidate();}});animator.start();}}
Activity
packagecom.stone.canvaspath;importandroid.app.Activity;importandroid.graphics.Path;importandroid.os.Bundle;importcom.stone.canvaspath.earth.EarthPathView;/***author:stone*email:aa86799@163.com*time:16/5/291527*/publicclassEarthActivityextendsActivity{privateEarthPathViewmPathView;privatePathmPath;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);intw=getResources().getDisplayMetrics().widthPixels;inth=getResources().getDisplayMetrics().heightPixels;mPathView=newEarthPathView(this);setContentView(mPathView);intmin=Math.min(w,h);buildPath(w/2+100,h/2+100,min/4);mPathView.setPath(mPath);mPathView.startAnim();}privatevoidbuildPath(floatx,floaty,floatradius){mPath=newPath();mPath.addCircle(x,y,radius,Path.Direction.CW);}}
以上是“Android如何实现自定义View图片按Path运动和旋转效果”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
Android如何实现自定义View图片按Path运动和旋转效果的详细内容,希望对您有所帮助,信息来源于网络。