嗨,我在自定义textview中遇到问题。我正在尝试以编程方式将文本添加到自定义textview中,但是,它没有出现

MainActiviy:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout container1 = (LinearLayout) findViewById(R.id.container1);
        LinearLayout container2 = (LinearLayout) findViewById(R.id.container2);

        animView = new MyAnimationView(this);
        animView.setMinimumWidth(200);
        animView.setMinimumHeight(200);
        animView.setText("XXXXx");
        animView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1f));
        container1.addView(animView);


和我的CustomView类,它是mainactivity的内部类

public class MyAnimationView extends TextView implements ValueAnimator.AnimatorUpdateListener {

    public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
    public final ArrayList<RectHolder> Rects = new ArrayList<RectHolder>();
    AnimatorSet animation = null;
    AnimatorSet ClickAnimation=null;
    private float mDensity;
    public Canvas can=new Canvas();
    public MyAnimationView(Context context) {
        super(context);
        mDensity = getContext().getResources().getDisplayMetrics().density;
        ShapeHolder ball0 = addBall(50f, 200f);
        RectHolder newline = addLine(40f,150f);

    }

    public void BeginAnimation(int pos) {
        createSelectionAnimation(pos);
        ClickAnimation.start();
    }

    private void createSelectionAnimation(int pos) {
        ClickAnimation=new AnimatorSet();
        Animation anim=AnimationUtils.loadAnimation(getBaseContext(), R.animator.scale);
        if(pos==1)
            animView.startAnimation(anim);
        else if(pos==2)
            animView1.startAnimation(anim);
        else if(pos==3)
            animView2.startAnimation(anim);
        else
            animView3.startAnimation(anim);
        ObjectAnimator animalpha = ObjectAnimator.ofFloat(balls.get(0), "Alpha",
                1.0f, 0.0f).setDuration(500);
        ObjectAnimator animrect = ObjectAnimator.ofFloat(Rects.get(0), "Alpha",
                1.0f, 0.0f).setDuration(500);
        ClickAnimation.playTogether(animrect,animalpha);
        animalpha.addUpdateListener(this);
        animrect.addUpdateListener(this);
    }

    private void createAnimation() {
        if (animation == null) {
            ObjectAnimator anim1 = ObjectAnimator.ofFloat(balls.get(0), "y",
            balls.get(0).getY(), balls.get(0).getY()-200f).setDuration(2000);
            animation = new AnimatorSet();
            animation.playTogether(anim1);
            anim1.addUpdateListener(this);
        }
    }

    private ShapeHolder addBall(float x, float y) {
        OvalShape circle = new OvalShape();
        circle.resize(90f * mDensity, 150f * mDensity);
        ShapeDrawable drawable = new ShapeDrawable(circle);
        ShapeHolder shapeHolder = new ShapeHolder(drawable);
        shapeHolder.setX(x);
        shapeHolder.setY(y);
        int red = (int)(100 + Math.random() * 155);
        int green = (int)(100 + Math.random() * 155);
        int blue = (int)(100 + Math.random() * 155);
        int color = 0xff000000 | red << 16 | green << 8 | blue;
        Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
        int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
        gradient = new RadialGradient(37.5f, 32.5f,
                110f, color, darkColor, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        shapeHolder.setPaint(paint);
        balls.add(shapeHolder);
        return shapeHolder;
    }

    private RectHolder addLine(float x,float y) {
        RectShape rect=new RectShape();
        rect.resize(10f * mDensity, 15f * mDensity);
        ShapeDrawable drawable = new ShapeDrawable(rect);
        RectHolder rectHolder = new RectHolder(drawable);
        rectHolder.setX(x);
        rectHolder.setY(y);
        //can.drawLine(45, 150f,45f,160f, paint1);
        int red = (int)(100 + Math.random() * 155);
        int green = (int)(100 + Math.random() * 155);
        int blue = (int)(100 + Math.random() * 155);
        int color = 0xff000000 | red << 16 | green << 8 | blue;
        Paint paint = drawable.getPaint(); //new Paint(Paint.ANTI_ALIAS_FLAG);
        int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
        gradient = new RadialGradient(37.5f, 32.5f,
                110f, color, darkColor, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        rectHolder.setPaint(paint);
        Rects.add(rectHolder);
        return rectHolder;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        ShapeHolder shapeHolder = balls.get(0);
        RectHolder rectHolder=Rects.get(0);
        Paint paint =getPaint();
        paint.setTextSize(20f);
        paint.setColor(Color.parseColor("#aaffee"));
        paint.setStrokeWidth(10f);
        paint.setShader(gradient);
            canvas.save();
            canvas.translate(shapeHolder.getX(), shapeHolder.getY());
            shapeHolder.getShape().draw(canvas);
            canvas.translate(rectHolder.getX(), rectHolder.getY());
            rectHolder.getShape().draw(canvas);
            canvas.restore();

         }

    public void startAnimation() {
        createAnimation();
        animation.start();
    }

    public void onAnimationUpdate(ValueAnimator animation) {
        invalidate();
    }

}


请帮助我。

最佳答案

首先在MyAnimationView.onDraw()方法中调用super.onDraw()。

关于android - 将文本添加到自定义TextView中不可见,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16354660/

10-12 00:12