我正在尝试实现椭圆形路径动画,我想使用图像显示路径动画,我尝试了https://github.com/matthewrkula/AnimatedPathView,但不适用于椭圆形.我也尝试下面的代码为椭圆路径,但它显示圆形,有人有想法吗?提前致谢!!!
MyAnimation.java
public class MyAnimation extends Animation { private VIEw vIEw; private float cx, cy; // center x,y position of circular path private float prevX, prevY; // prevIoUs x,y position of image during animation private float r; // radius of circle private float prevDx, prevDy; /** * @param vIEw - VIEw that will be animated * @param r - radius of circular path */ public MyAnimation(VIEw vIEw, float r){ this.vIEw = vIEw; this.r = r; } @OverrIDe public boolean willChangeBounds() { return true; } @OverrIDe public voID initialize(int wIDth, int height, int parentWIDth, int parentHeight) { // calculate position of image center int cxImage = wIDth / 2; int cyImage = height / 1; cx = vIEw.getleft() + cxImage; cy = vIEw.gettop() + cyImage; // set prevIoUs position to center prevX = cx; prevY = cy; } @OverrIDe protected voID applytransformation(float interpolatedTime, transformation t) { if(interpolatedTime == 0){ t.getMatrix().setTranslate(prevDx, prevDy); return; } float angleDeg = (interpolatedTime * 360f + 90) % 360; float angleRad = (float) Math.toradians(angleDeg); // r = radius, cx and cy = center point, a = angle (radians) float x = (float) (cx + r * Math.cos(angleRad)); float y = (float) (cy + r * Math.sin(angleRad)); float dx = prevX - x; float dy = prevY - y; prevX = x; prevY = y; prevDx = dx; prevDy = dy; t.getMatrix().setTranslate(dx, dy); }}
PathAnimation.java
image = (ImageVIEw) findVIEwByID(R.ID.image); image.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Animation anim = new MyAnimation(image, 300); anim.setDuration(1000); image.startAnimation(anim); } });
解决方法:
经过多次尝试使用此自定义类后,我找到了解决方案
AnimationVIEw.java
public class AnimationVIEw extends VIEw { Paint paint; long animationDuration = 10000; int framesPerSecond = 60; Bitmap bm; int bm_offsetX, bm_offsetY; Path animPath; PathMeasure pathMeasure; float pathLength; float step; //distance each step float distance; //distance moved float[] pos; float[] tan; Matrix matrix; public AnimationVIEw(Context context) { super(context); initMyVIEw(); } public AnimationVIEw(Context context, AttributeSet attrs) { super(context, attrs); initMyVIEw(); } public AnimationVIEw(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initMyVIEw(); } @TargetAPI(Build.VERSION_CODES.LolliPOP) public voID initMyVIEw(){ paint = new Paint(); paint.setcolor(color.RED); paint.setstrokeWIDth(5); paint.setStyle(Paint.Style.stroke); bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); bm_offsetX = bm.getWIDth()/2; bm_offsetY = bm.getHeight()/2; animPath = new Path(); animPath.moveto(100, 100); animPath.addArc(new RectF(1, 100, 300, 600), 1, 800); animPath.close(); pathMeasure = new PathMeasure(animPath, false); pathLength = pathMeasure.getLength(); Toast.makeText(getContext(), "pathLength: " + pathLength, Toast.LENGTH_LONG).show(); step = 1; distance = 0; pos = new float[2]; tan = new float[2]; matrix = new Matrix(); } @OverrIDe protected voID onDraw(Canvas canvas) { canvas.drawPath(animPath, paint); if(distance < pathLength){ pathMeasure.getPosTan(distance, pos, tan); matrix.reset(); float degrees = (float)(Math.atan2(tan[1], tan[0])*180.0/Math.PI); matrix.postRotate(degrees, bm_offsetX, bm_offsetY); matrix.postTranslate(pos[0]-bm_offsetX, pos[1]-bm_offsetY); canvas.drawBitmap(bm, matrix, null); distance += step; }else{ distance = 0; } invalIDate(); }}
并放入xml
<com.example.androID.mydemo.animation.pathanimation.AnimationVIEw androID:layout_wIDth="match_parent" androID:layout_height="450dp" />
总结 以上是内存溢出为你收集整理的的椭圆形动画使用图像全部内容,希望文章能够帮你解决的椭圆形动画使用图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)