小弟有个androi动画的问题,困扰我太久了,求大神解惑

我自定义了一个View,在鼠标点击处画一个小球,利用ObjectAnimator和PropertyValuesHolder设置动画效果,为什么设置x,y属性动画正常,小球能沿着坐标轴一定,设置
alpha和scale就不起作用,这玩意是怎么知道我写的X,Y就是让他在坐标轴上移动,但是他为什么不认识alpha和scale,还是我写的有问题,代码如下

package com.example.mrwuchao.newballtest;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private float BALL_SIZE = 50f;
ArrayList arrayList = new ArrayList<>();
// Button button;
/*出现问题

  • 自定义的View可以使用想x,y属性让小球移动,但是使用alpha或者scale就没有效果
  • objectAnimator是怎么识别这些属性的,写出一个字符串,他是怎么判断要执行什么动画的?*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout re = (RelativeLayout)findViewById(R.id.re);
    re.addView(new MyView(this));
    // button = (Button)findViewById(R.id.button);
    }

    public class MyView extends View implements ValueAnimator.AnimatorUpdateListener {

    public MyView(Context context) {
        super(context);
        setBackgroundColor(Color.WHITE);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN &&
                event.getAction() != MotionEvent.ACTION_MOVE) {
            return false;
        }
        ShapeHolder newBall = addBall(event.getX(), event.getY());
    
        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("x",event.getX(),600f,200f);
        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("alpha",1.0f,0f);
        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(newBall,p1,p2);
        oa.setDuration(2000);
       // oa.setInterpolator(new AccelerateInterpolator());
        oa.addUpdateListener(this);
        oa.start();
    

    // ObjectAnimator oa = ObjectAnimator.ofFloat(newBall,"x",600f);
    // ObjectAnimator ob = ObjectAnimator.ofFloat(newBall,"y",600f);
    //

// ob.addUpdateListener(this);
//
// AnimatorSet animatorSet = new AnimatorSet();
// animatorSet.play(oa).with(ob);
// animatorSet.setDuration(2000);
// animatorSet.start();
return true;
}

    private ShapeHolder addBall(float x, float y) {
        OvalShape circle = new OvalShape();
        circle.resize(BALL_SIZE, BALL_SIZE);
        ShapeDrawable drawable = new ShapeDrawable(circle);
        ShapeHolder shapeHolder = new ShapeHolder(drawable);
        shapeHolder.setX(x-BALL_SIZE/2);
        shapeHolder.setY(y-BALL_SIZE/2);
        shapeHolder.setAlpha(1.0f);
        Paint paint = drawable.getPaint();
        int[] color = new int[]{Color.GRAY, Color.BLUE};
        RadialGradient gradient = new RadialGradient(40f, 15f, BALL_SIZE, color, null, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        //paint.setColor(Color.RED);
        shapeHolder.setPaint(paint);
        arrayList.add(shapeHolder);
        return shapeHolder;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (ShapeHolder sh : arrayList) {
            canvas.save();
            canvas.translate(sh.getX(),sh.getY());
            sh.getD().draw(canvas);
            canvas.restore();
        }
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        this.invalidate();
    }
}

}

包装类
package com.example.mrwuchao.newballtest;

import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;

/**

  • Created by Mr.wuchao on 2016/6/8.
    */
    public class ShapeHolder {

    public void setDrawable(ShapeDrawable drawable) {
    this.d = drawable;
    }

    ShapeDrawable d;
    Paint paint;

    float alpha = 1.0f;
    float x,y;

    public ShapeHolder(ShapeDrawable drawable) {
    this.d = drawable;
    }

    public ShapeDrawable getD() {
    return d;
    }

    public void setD(ShapeDrawable d) {
    this.d = d;
    }
    public float getAlpha() {
    return alpha;
    }

    public void setAlpha(float alpha) {
    this.alpha = alpha;
    }
    public float getX() {
    return x;
    }

    public void setX(float x) {
    this.x = x;
    }

    public Paint getPaint() {
    return paint;
    }

    public void setPaint(Paint paint) {
    this.paint = paint;
    }
    public float getY() {
    return y;
    }

    public void setY(float y) {
    this.y = y;
    }
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问