Android 自定义View以及ValueAnimator学习
生活随笔
收集整理的這篇文章主要介紹了
Android 自定义View以及ValueAnimator学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
? ?? 看了ApiDemo里面的BoucingBall,覺得挺好的,所以特地學習了一下,將代碼注釋后貼到這里,以便以后學習。
class BallView extends View implements ValueAnimator.AnimatorUpdateListener{private static final int RED = 0xffFF8080;private static final int BLUE = 0xff8080FF;private static final int CYAN = 0xff80ffff;private static final int GREEN = 0xff80ff80;private ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();public BallView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public BallView(Context context) {this(context, null);}public BallView(Context context, AttributeSet attrs) {super(context, attrs);//自定義View的背景切換動畫ValueAnimator colorAnim = ObjectAnimator.ofInt(this,"backgroundColor", RED, BLUE,CYAN,GREEN);colorAnim.setDuration(3000L);colorAnim.setEvaluator(new ArgbEvaluator());colorAnim.setRepeatCount(ValueAnimator.INFINITE);colorAnim.setRepeatMode(ValueAnimator.REVERSE);colorAnim.start();}@Overrideprotected void onDraw(Canvas canvas) {//在畫布上繪制圖形for(ShapeHolder holder:balls){canvas.save();canvas.translate(holder.getX(), holder.getY());holder.getmShapeDrawable().draw(canvas);canvas.restore();}}@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() != MotionEvent.ACTION_DOWN&& event.getAction() != MotionEvent.ACTION_MOVE) {return false;}ShapeHolder newBall = addBall(event.getX(), event.getY());float startY = newBall.getY();float endY = getHeight() - 50f;float h = getHeight();float eventy = event.getY();int duration = (int) (500 * ((h - eventy) / h));//設置加速掉落的效果ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y",startY, endY);bounceAnim.setDuration(duration);bounceAnim.setInterpolator(new AccelerateInterpolator());//當掉落到底部的時候,球體壓扁,高度降低ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "x",newBall.getX(), newBall.getX() - 25);squashAnim1.setDuration(duration /4);squashAnim1.setRepeatCount(1);squashAnim1.setRepeatMode(ValueAnimator.REVERSE);squashAnim1.setInterpolator(new DecelerateInterpolator());ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall,"width", newBall.getWidth(), newBall.getWidth() + 50);squashAnim2.setDuration(duration /4);squashAnim2.setRepeatCount(1);squashAnim2.setRepeatMode(ValueAnimator.REVERSE);squashAnim2.setInterpolator(new DecelerateInterpolator());ValueAnimator strechAnim1 = ObjectAnimator.ofFloat(newBall, "y",endY, endY + 25);strechAnim1.setDuration(duration /4);strechAnim1.setRepeatCount(1);strechAnim1.setRepeatMode(ValueAnimator.REVERSE);strechAnim1.setInterpolator(new DecelerateInterpolator());ValueAnimator strechAnim2 = ObjectAnimator.ofFloat(newBall,"height",newBall.getHeight(), newBall.getHeight() - 25);strechAnim2.setDuration(duration /4);strechAnim2.setRepeatCount(1);strechAnim2.setRepeatMode(ValueAnimator.REVERSE);strechAnim2.setInterpolator(new DecelerateInterpolator());ValueAnimator bounceBack = ObjectAnimator.ofFloat(newBall, "y",endY, startY+25);bounceBack.setDuration(duration);bounceAnim.setInterpolator(new DecelerateInterpolator());AnimatorSet set = new AnimatorSet();set.play(bounceAnim).before(squashAnim1);set.play(squashAnim1).with(squashAnim2);set.play(squashAnim1).with(strechAnim1);set.play(squashAnim1).with(strechAnim2);set.play(bounceBack).after(strechAnim2);//逐漸消失ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(newBall,"alpha", 1F, 0F);fadeAnimator.setDuration(600L);fadeAnimator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {// TODO Auto-generated method stubballs.remove(((ObjectAnimator) animation).getTarget());}});AnimatorSet animationSet = new AnimatorSet();animationSet.play(set).before(fadeAnimator);animationSet.start();return true;}/*** 生成一個球* @param x* @param y* @return*/public ShapeHolder addBall(float x, float y) {// 構造一個圓形的圖案OvalShape oval = new OvalShape();oval.resize(50f, 50f);ShapeDrawable draw = new ShapeDrawable(oval);ShapeHolder holder = new ShapeHolder(draw);holder.setX(x - 25);holder.setY(y - 25);int red = (int) (Math.random() * 255);int green = (int) (Math.random() * 255);int blue = (int) (Math.random() * 255);int color = 0xff000000 | red << 16 | green << 8 | blue;int darkColor = 0xff000000 | red / 4 << 16 | green / 4 << 8 | blue/ 4;Paint paint = draw.getPaint();RadialGradient gradient = new RadialGradient(12.5f, 12.5f, 50f,color, darkColor, Shader.TileMode.CLAMP);paint.setShader(gradient);holder.setmRadialGradient(gradient);holder.setmPaint(paint);balls.add(holder);return holder;}@Overridepublic void onAnimationUpdate(ValueAnimator animation) {// TODO Auto-generated method stubinvalidate();} }
轉載于:https://my.oschina.net/fengcunhan/blog/52763
總結
以上是生活随笔為你收集整理的Android 自定义View以及ValueAnimator学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 由马化腾谈“微博修改功能”,看什么是优秀
- 下一篇: Eclipse开发Android程序如何