Search code examples
androidandroid-animationandroid-custom-view

why is the following code not working?


i am trying to make my own custom control in which i want to draw a view which has text rotating in it.The control is basically a cirle with text. The following is my custom view class:

    package com.helios;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

public class MyGraphicsView extends View {
    int height;
    int width;
    Animation anim;
    Context context;

    public MyGraphicsView(Context context, AttributeSet attrs) {
        super(context);

        this.context = context;
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.mygraphicview);
        height = ta.getDimensionPixelSize(R.styleable.mygraphicview_cellHeight,
                1);
        width = ta
                .getDimensionPixelSize(R.styleable.mygraphicview_cellWidth, 1);
    }

    public void startanim(Canvas c) {
        anim = new RotateAnimation(0, 360, height/2, width/2);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setInterpolator(new AccelerateInterpolator());
        startAnimation(anim);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(resolveSize(width, widthMeasureSpec),
                resolveSize(height, heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if (anim == null) {
            startanim(canvas);
        }
        Path circle = new Path();
        circle.addCircle(200, 200, width, Direction.CW);
        Paint canvaspaint = new Paint();
        canvaspaint.setColor(Color.BLUE);
        canvas.drawPath(circle, canvaspaint);
        Paint textpaint = new Paint();
        textpaint.setColor(Color.GREEN);
        canvas.drawTextOnPath("I am rohan Joshi, this is my animation", circle,
                0, 0, textpaint);
    }
}

The following is my main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mycontrol="http://schemas.android.com/apk/res/com.helios"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="@string/hello" />
<com.helios.MyGraphicsView mycontrol:cellWidth="40dip"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    mycontrol:cellHeight="40dip"></com.helios.MyGraphicsView>
</LinearLayout>

The custom view is not showing up anywhere. What am i doing wrong?


Solution

  • Make sure in your startanim(Canvas c) method you call anim.setDuration(10000); (with whatever value you want, of course). The animation you currently have is set to have a duration of 0 so it technically is working, it's just doing it in 0 seconds so it's instant.

    You should change this as well:

    circle.addCircle(width/2, height/2, width / 2.5f, Direction.CW);
    

    You were creating a circle offset 200 from 0, when you have a canvas width of 40. So, if I'm correct, you're drawing this off the canvas. This will draw the circle in the center of the canvas.