Question

Hi I have problem with custom textview.I am trying to add text into a custom textview programmatically,but, It does not appear

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);

and my CustomView class which an inner class of mainactivity is

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();
    }

}

please help me in this.

Was it helpful?

Solution

Call super.onDraw() first in the MyAnimationView.onDraw() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top