可绘制的动画不起作用

时间:2015-05-27 15:20:38

标签: java android listview animationdrawable

我想在我的列表中使用可绘制的动画但是当我启动listActivity时它会崩溃  我从this学到了 ,当我删除这2个动画代码

 AnimationDrawable frameAnimation = (AnimationDrawable) GamePic.getBackground();
        frameAnimation.start();

,它在我的手机中自动制作动画(p7与android 4.4),但在我试过的其他手机中(asus memopad 7,sony c)它没有动画drawables!当我使用这个代码时,所有手机都会崩溃 下面是我的竞速代码,出了什么问题?

public class MyList extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.games_list);

    setListAdapter(new AA());
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    Toast.makeText(MyList.this, "item" + position + "is clicked", Toast.LENGTH_SHORT).show();

}


class AA extends ArrayAdapter<String> {

    public AA() {
        super(MyList.this, R.layout.games_list_layout, Resources2.GameListArray);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.games_list_layout, parent, false);
        }
        TextView GameName = (TextView) row.findViewById(R.id.textView1);
        ImageView GamePic = (ImageView) row.findViewById(R.id.imageView2);
        ImageView Cartridge = (ImageView) row.findViewById(R.id.imageView1);

        GameName.setText(Resources2.GameListArray[position]);
        GamePic.setImageResource(Resources2.GamePicArray[position]);
        Cartridge.setImageResource(Resources2.CartridgeFrame[Resources2.cartridgeColor[position]]);

        AnimationDrawable frameAnimation = (AnimationDrawable) GamePic.getBackground();
        frameAnimation.start();

        return row;

    }
}

}

感谢

2 个答案:

答案 0 :(得分:0)

正如documentation所述,您应该等到视图附加到窗口后再开始动画。因此,您应该将OnAttachStateChangeListener添加到将在附加时执行的视图,并从那里开始动画。

ImageView img = (ImageView)v.findViewById(R.id.image);
img.setBackgroundResource(R.drawable.anim);
img.addOnAttachStateChangeListener(new  View.OnAttachStateChangeListener() {
  @Override
  public void onViewAttachedToWindow(View v) {
  AnimationDrawable animation = (AnimationDrawable) v.getBackground();
  animation .start();
  }

@Override
public void onViewDetachedFromWindow(View v) {
}
});

如果您使用的是GIF,请使用Movie Class。这里有一些学习链接,thisthisthisthis quistion

答案 1 :(得分:0)

我发现了我的问题!

我已经习惯了     GamePic.setImageResource(Resources2.GamePicArray [位置]);

但我应该使用

function hash_password( $password ){
    $md5 = strtoupper( hash( 'md5', time() ) );
    $hash = $md5 . strtoupper( hash( 'sha256', $md5 . $password ) );
    return $hash;
    }

function test_password( $password, $hash ){
    $md5 = substr( $hash, 0, 32 );
    $sha256 = substr( $hash, 32 );

    if( $sha256 == strtoupper( hash( 'sha256', $md5 . $password ) ) )
        return true;
    else
        return false;
    }

现在它运作正常!