具有自定义视图的对象动画师
objectanimator with custom view
我想在我的自定义视图上有一个简单的动画。
我用了objectanimator但是自定义视图没有动
当我用另一个对象(比如文本视图)尝试这个时,它起作用了。
下面是我的代码。 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);
}
}
代码没有崩溃,似乎 运行 但自定义视图中的圆和位图没有移动。
我错过了什么?
我在创建 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);
}
}
我想在我的自定义视图上有一个简单的动画。 我用了objectanimator但是自定义视图没有动
当我用另一个对象(比如文本视图)尝试这个时,它起作用了。
下面是我的代码。 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);
}
}
代码没有崩溃,似乎 运行 但自定义视图中的圆和位图没有移动。
我错过了什么?
我在创建 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);
}
}