如何在Android中的Activity中为单个片段设置动画?

时间:2014-10-14 11:59:56

标签: android android-fragments

我正在创建一个简单的小册子,其中MainActivity有一个包含多行文本视图和一个按钮的片段。单击该按钮,我将从SQLite数据库加载TextView的新内容。

我想在更改内容(例如翻转)时为片段添加动画。 我搜索了很多教程,但没有一个片段的解决方案。

My Fragment看起来像这样......我想在点击2个imageViews(按钮)时显示一些效果

public class DisplayJokesFragment extends Fragment {

    Activity activity;
    TextView joke;
    ImageView imgview1, imgview2;
    private Cursor cursor;

    public static DisplayJokesFragment createFunnyJokesFragment(Cursor cursor) {
        DisplayJokesFragment fragment = new DisplayJokesFragment();
        fragment.setCursor(cursor);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_funny_jokes,
                container, false);
        joke = (TextView) rootView.findViewById(R.id.textView1);
        imgview1 = (ImageView) rootView.findViewById(R.id.imageView1);
        imgview2 = (ImageView) rootView.findViewById(R.id.imageView2);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    public void init() {
        //MOVE NEXT JOKE
        imgview2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (cursor.moveToNext()) {
                    joke.setText(Html.fromHtml(cursor.getString(1)));
                } else {
                    cursor.moveToFirst();
                    joke.setText(Html.fromHtml(cursor.getString(1)));
                }
            }
        });     

        //MOVE PREVIOUS JOKE
        imgview1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cursor.moveToPrevious()) {
                    joke.setText(Html.fromHtml(cursor.getString(1)));
                } else {
                    cursor.moveToLast();
                    joke.setText(Html.fromHtml(cursor.getString(1)));
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您可以将动画设置为TextView。当您单击按钮时 - 您开始动画并在视图中设置一些文本。

如果需要,可以在xml文件中创建动画。

例如,您可以这样做:

final TextView yourTextView = (TextView)findViewById(R.id.yourTextView);
final AlphaAnimation animateS = new AlphaAnimation(1,0);
final AlphaAnimation animateF = new AlphaAnimation(0,1);
animateS.setDuration(700);
animateF.setDuration(700);
animateS.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}
@Override
            public void onAnimationEnd(Animation animation) {
                yourTextView.setAnimation(animateF);
                animateF.start();
                yourTextView.setText("New Text");
            }
@Override
            public void onAnimationRepeat(Animation animation) {
}
        });
yourTextView.setText(str);
        yourTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                yourTextView.setAnimation(animateS);
                animateS.start();
            }
        });