gpt4 book ai didi

android - 在 Android 中创建动画

转载 作者:行者123 更新时间:2023-11-30 04:21:28 24 4
gpt4 key购买 nike

请帮忙,我是 Android 开发的新手。我只想动态创建图像动画,换句话说,例如我想创建 3 个图像和动画,然后在某些条件下它应该变成 4 个图像,5 个图像等等。当我用 3 个 Runnables 分别创建 3 个图像时,它工作正常,但是当我通过动态数组创建这 3 个 Runnables 时,它什么都不做。这是包含 3 个单独的 Runnable 的代码

private Runnable run1= new Runnable() {   
public void run() {
if(t1)
{
LayoutParams params1=(LayoutParams) l1.getLayoutParams();
params1.x=x1;
params1.y=y1;
l1.setLayoutParams(params1);
x2=r.nextInt(720-80)+80;
y2=r.nextInt(400-80)+80;

TranslateAnimation ta1 = new TranslateAnimation(0, x2-x1, 0, y2-y1 );
ta1.setDuration(800);
ta1.setFillAfter(true);
l1.startAnimation(ta1);
x1=x2;
y1=y2;

handler.postDelayed(run1, 800);

}
}

Run2 和 Run3 也一样它工作正常,但下面什么都不做

for(j=0;j<c;j++)    
{
run[j]=new Runnable()
{
public void run() {
if(t[j])

{

params[j]=(LayoutParams) images[j].getLayoutParams();
params[j].x=x1[j];
params[j].y=y1[j];
images[j].setLayoutParams(params[j]);

x2[j]=r.nextInt(720-80)+80;
y2[j]=r.nextInt(400-80)+80;

ta[j] = new TranslateAnimation(0, x2[j]-x1[j], 0, y2[j]-y1[j] );
ta[j].setDuration(200);
ta[j].setFillAfter(true);
images[j].startAnimation(ta[j]);
x1[j]=x2[j];
y1[j]=y2[j];

handler.postDelayed(run[j], 200);
}
}

};
for(j=0;j<c;j++)
{
this.runOnUiThread(run[j]);
}

如何解决这个问题,我的意思是如何创建具有动态图像数量的动画。

最佳答案

首先你应该检查你的 t[j] 是否变为“真”,或者它总是保持假(我想你忘记将 t[j] 设置为真)。其次,还有另一种动态创建图像动画的好方法。不要在公共(public)类中使用图像,而应将每个图像呈现为单独类的对象,该类应提供 2 个属性:图像名称和用于决定此对象是否应设置动画的自己的 bool 值 t。此类应实现 Runnable。然后你可以动态创建这个类的对象数组,并为每个对象运行动画。我是一个 objective-c 开发人员,所以这里是你可以轻松传输到 android 的代码。

class Single implement Runnable
{
UIImageView* image;
bool t;
//method run{

//here you should create animation code

}
class common
{
static int c=3;
Single[] arrayOfObj=new Single[3];
//method onCreate
{
//create objects of arrayOfObj with images that you want and then call run method for each object
}
}

关于android - 在 Android 中创建动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9207554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com