停止查看寻呼机在看到之前启动下一个寻呼机

时间:2016-02-17 10:01:28

标签: android android-viewpager gif animated-gif

现在我有一个包含多个片段的视图寻呼机。

实际上我在一个片段中替换视图,但是我遇到了问题。

我在每个视图中显示gif图像。所以,当我在第1页时,图像2中的gif开始动画。

我需要保持gif没有动画,直到它在屏幕上可见。

我正在使用setUserVisibleHint,但它没有添加任何改进或解决方案。

那么有什么建议可以解决这个问题吗?

提前致谢。

这是我的片段代码

public class WalkThroughFragment extends Fragment implements View.OnClickListener {

public CacheClass cacheClass;
public View view;
private int pageNumber = 1;
private TextView doneWalkThrough;
private LinearLayout linearLayout;
private GifImageView gifImageView, gifImageView2, gifImageView3, gifImageView4, gifImageView5;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.item_walkthrough, container, false);
    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    this.view = view;
    Bundle bundle = this.getArguments();
    if (bundle != null) {
        if (bundle.getInt("pageNumber") != 0) {
            pageNumber = bundle.getInt("pageNumber");
        }
    }
    cacheClass = new CacheClass(getActivity());

    gifImageView = (GifImageView) view.findViewById(R.id.imageViewGif);
    gifImageView.setFreezesAnimation(true);
    gifImageView.setVisibility(View.GONE);

    gifImageView2 = (GifImageView) view.findViewById(R.id.imageViewGif);
    gifImageView2.setFreezesAnimation(true);
    gifImageView2.setVisibility(View.GONE);

    gifImageView3 = (GifImageView) view.findViewById(R.id.imageViewGif);
    gifImageView3.setFreezesAnimation(true);
    gifImageView3.setVisibility(View.GONE);

    gifImageView4 = (GifImageView) view.findViewById(R.id.imageViewGif);
    gifImageView4.setFreezesAnimation(true);
    gifImageView4.setVisibility(View.GONE);

    gifImageView5 = (GifImageView) view.findViewById(R.id.imageViewGif);
    gifImageView5.setFreezesAnimation(true);
    gifImageView5.setVisibility(View.GONE);

    linearLayout = (LinearLayout) view.findViewById(R.id.walkThroughLayOut);
    doneWalkThrough = (TextView) view.findViewById(R.id.doneWalkThrough);
    doneWalkThrough.setOnClickListener(this);

    if (this.getUserVisibleHint()) {
        setUserVisibleHint(true);
    } else {
        linearLayout.setBackgroundColor(Color.BLUE);
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    gifImageView.destroyDrawingCache();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    if (isVisibleToUser) {

        if (getView() != null) {
            switch (pageNumber) {
                case 1:
                    gifImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                    gifImageView.setBackgroundResource(R.drawable.first_gif);
                    gifImageView.setVisibility(View.VISIBLE);
                    linearLayout.setBackgroundColor(Color.GREEN);
                    break;

                case 2:
                    gifImageView2.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                    gifImageView2.setBackgroundResource(R.drawable.second_gif);
                    gifImageView2.setVisibility(View.VISIBLE);
                    linearLayout.setBackgroundColor(Color.BLUE);
                    break;

                case 3:
                    gifImageView3.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                    gifImageView3.setBackgroundResource(R.drawable.third_gif);
                    gifImageView3.setVisibility(View.VISIBLE);
                    linearLayout.setBackgroundColor(getResources().getColor(R.color.light_blue));
                    break;

                case 4:
                    gifImageView4.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                    gifImageView4.setBackgroundResource(R.drawable.forth_gif);
                    gifImageView4.setVisibility(View.VISIBLE);
                    linearLayout.setBackgroundColor(Color.YELLOW);
                    break;

                case 5:
                    gifImageView5 = (GifImageView) view.findViewById(R.id.imageViewGif);
                    gifImageView5.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                    gifImageView5.setVisibility(View.VISIBLE);
                    gifImageView5.setBackgroundResource(R.drawable.fifth_gif);
                    try {
                        GifDrawable gifFromResource = new GifDrawable(getResources(), R.drawable.fifth_gif);
                        int duration = gifFromResource.getDuration();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                doneWalkThrough.setVisibility(View.VISIBLE);
                            }

                        }, duration);
                        gifFromResource.stop();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

}

@Override
public void onClick(View v) {
    ((WalkThroughActivity) getActivity()).switchIntent();
}

}

1 个答案:

答案 0 :(得分:1)

您似乎忘记在setUserVisibleHint方法上调用super方法。尝试执行以下操作,看看是否有帮助

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
   super.setUserVisibleHint(isVisibleToUser); // call to super added
   if(isVisibleToUser){
       // do your stuff here
   }
}