具有自定义视图的objectanimator

时间:2018-06-15 11:39:47

标签: android android-custom-view objectanimator

我希望在自定义视图上有一个简单的动画。 我使用了objectanimator,但自定义视图没有移动。

当我尝试使用另一个对象(如textview)时,它可以工作。

以下是我的代码。 z1是我想移动的自定义视图。

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


    }

代码不会崩溃并且似乎在运行,但自定义视图中的圆圈和位图不会移动。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

当您创建myviewx

的构造函数时,我发现了一个问题

请编辑

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