Search code examples
androidandroid-custom-viewobjectanimator

objectanimator with custom view


I want to have a simple animation on my custom view. I used the objectanimator but the custom view did not move.

When I try this with another object (like a textview) it works.

Below is my code. z1 is the custom view that I'd like to move.

public class FinalActivity extends Activity {
    MyViewX z1;
    TextView t1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_final);

        z1 = (MyViewX) findViewById(R.id.xview);
        t1 = (TextView) findViewById(R.id.textView1);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if(hasFocus){
            ObjectAnimator animation = ObjectAnimator.ofFloat(z1, "translationX", 100f);
            animation.setDuration(5000);
            animation.start();
            }   
    }
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5sp"
    tools:context=".FrmActivity" >

      <com.test.game.myviewx
        android:id="@+id/xview"
        android:layout_width="200dp"
        android:layout_height="200dp"
    />

      <TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:layout_marginLeft="72dp"
          android:layout_marginTop="173dp"
          android:text="TextView" />

</RelativeLayout>

public class MyViewX extends View
    {

        public MyViewX(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        public MyViewX(Context context, AttributeSet attributeset) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
            Resources res = getResources();
            Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.base1);


            int x=80;
            int y=80;
            int radius=40;
            Paint paint=new Paint();
            // Use Color.parseColor to define HTML colors
            paint.setColor(Color.parseColor("#CD5C5C"));
            canvas.drawCircle(x,x, radius, paint);
            canvas.drawBitmap(bitmap, 0, 0, paint);
        }


    }

the code does not crash and seems to run but the circle and the bitmap in the custom view does not move.

what am I missing ?


Solution

  • I see one issue when you create constructor of your myviewx

    please edit it

    public class myviewx extends View {
    
        public myviewx(Context context) {
            super(context);
        }
    
        public myviewx(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public myviewx(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Resources res = getResources();
            Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.base1);
    
            int x = 80;
            int y = 80;
            int radius = 40;
            Paint paint = new Paint();
            // Use Color.parseColor to define HTML colors
            paint.setColor(Color.parseColor("#CD5C5C"));
            canvas.drawCircle(x, x, radius, paint);
            canvas.drawBitmap(bitmap, 0, 0, paint);
        }
    }