0

很长的问题可能是一个简短的答案。

我有一系列图像(显示在网格视图中),当按下按钮时,我使用AnimationDrawable类对其进行动画处理。
动画代码片段;

AnimationDrawable mAnimation;
view.setImageDrawable(null);

Random randomGenerator = new Random();
int rand = randomGenerator.nextInt(6);

BitmapDrawable frame0 = (BitmapDrawable)context.getResources().getDrawable(spinImages.get(0));
BitmapDrawable frame1 = (BitmapDrawable)context.getResources().getDrawable(spinImages.get(1));
BitmapDrawable last = (BitmapDrawable)context.getResources().getDrawable(endImages.get(rand));

mAnimation = new AnimationDrawable();
mAnimation.isOneShot();

for (int i=0; i < spinNumber; i++) {
    mAnimation.addFrame(frame0, spinDuration);
    mAnimation.addFrame(frame1, spinDuration);
}
mAnimation.addFrame(last, spinDuration);

view.setBackgroundDrawable(mAnimation);
view.setTag(rand);

mAnimation.start();

还有一个微调器将根据所选值过滤图像,将其中一些设置为不可见(到目前为止一切正常)。

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    int index = arg0.getSelectedItemPosition();
    String[] filterOptions;
    filterOptions = getResources().getStringArray(R.array.spn_options);

    // hide all below filter value
    GridView gridView = (GridView) findViewById(R.id.grid_view);
    for (int i=0; i < ((ViewGroup)gridView).getChildCount(); ++i) {
        View nextChild = ((ViewGroup)gridView).getChildAt(i);
        nextChild.setVisibility(View.VISIBLE);
        if (nextChild instanceof ImageView) {
            // get tag
            if (Integer.parseInt(nextChild.getTag().toString()) < arg2) {
                nextChild.setVisibility(View.INVISIBLE);
            }
            nextChild.getTag();
        }
    }
}

The problem is when the spinner to show all is selected I apply setVisibility(View.VISIBLE)on the image; 这使图像重新出现但再次触发动画。我希望图像重新出现,仅显示动画的最终状态。

有任何想法吗?

4

1 回答 1

0

我通过在使其不可见之前设置图像资源来解决它。

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    int index = arg0.getSelectedItemPosition();
    String[] filterOptions;
    filterOptions = getResources().getStringArray(R.array.spn_options);

    // hide all below filter value
    GridView gridView = (GridView) findViewById(R.id.grid_view);
    for (int i=0; i < ((ViewGroup)gridView).getChildCount(); ++i) {
        View nextChild = ((ViewGroup)gridView).getChildAt(i);
        nextChild.setVisibility(View.VISIBLE);
        if (nextChild instanceof ImageView) {
            // get tag
            if (Integer.parseInt(nextChild.getTag().toString()) < arg2) {
                ImageView iv = (ImageView) nextChild;
                iv.setImageResource(R.drawable.one);
                iv.setTag(nextChild.getTag().toString());
                iv.setVisibility(View.INVISIBLE);
            }
            nextChild.getTag();
        }
    }
}

我仍然欢迎其他/更好的解决方案,

于 2013-03-22T14:08:36.717 回答