立方体过渡效果动画不在Tabactivity中工作

时间:2016-03-30 18:45:57

标签: android

我正在研究Cube Transition效果动画,但是当我尝试添加Tab活动时,它会失去立方体过渡效果,它的工作正常没有标签,我使用ToxicBakery / ViewPagerTransforms库来实现此效果

我的代码是

package com.ToxicBakery.viewpager.transforms.example;

import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;

import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;

import java.util.ArrayList;

@SuppressLint("ResourceAsColor")
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
    TabHost tabHost;
    ImageButton ib;
    private ViewPager mPager;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        tabHost = getTabHost();
        setTabs();
    }

    private void setTabs() {
        addTab("", R.drawable.home, HomeActivity.class);
        addTab("", R.drawable.search, ListActivity.class);
        addTab("", R.drawable.camerabg, HomeActivity.class);
        addTab("", R.drawable.like, AlarmActivity.class);
        addTab("", R.drawable.profile, SettingActivity.class);

        ib = (ImageButton) findViewById(R.id.ibHome);
        ib.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_LONG)
                        .show();
            }
        });
    }

    private void addTab(String labelId, int drawableId, Class<?> c) {
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, getTabWidget(), false);

        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);


    }

}

和cube transition activty是

package com.example.turnster;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;

import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;

import java.util.ArrayList;
import java.util.HashMap;

import adapters.SampleAdapter;

public class HomeActivity extends Activity{
    ListView Lv;
    SampleAdapter adapter;
    private PageAdapter mAdapter;
    private ViewPager mPager;
    private static final ArrayList<TransformerItem> TRANSFORM_CLASSES;
    static {
        TRANSFORM_CLASSES = new ArrayList<>();


        TRANSFORM_CLASSES.add(new TransformerItem(CubeOutTransformer.class));
        //TRANSFORM_CLASSES.add(new TransformerItem(CubeInTransformer.class));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.list_home_lv);
        mPager=(ViewPager)findViewById(R.id.container);
//      Lv = (ListView)findViewById(R.id.listView);
//      ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
//      HashMap<String,String> temp4 = new HashMap<String,String>();
//      temp4.put("name", "Korigad");
//      temp4.put("heightt", "3000ft");
//      temp4.put("heightt", "3000ft");
//      temp4.put("heightt", "3000ft");
//      temp4.put("heightt", "3000ft");
//      data.add(temp4);
//      adapter = new SampleAdapter(HomeActivity.this,
//              android.R.layout.simple_list_item_1, data,
//              getApplication());
//      Lv.setAdapter(adapter);
        mAdapter = new PageAdapter(getFragmentManager());

        mPager.setAdapter(mAdapter);


    }
    public static class PlaceholderFragment extends Fragment {

        private static final String EXTRA_POSITION = "EXTRA_POSITION";
        private static final int[] COLORS = new int[] { R.drawable.imgone, R.drawable.imgtwo, R.drawable.imgthree, R.drawable.imgfour };

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            final int position = getArguments().getInt(EXTRA_POSITION);
            final ImageView textViewPosition = (ImageView) inflater.inflate(R.layout.fragment_main, container, false);

            textViewPosition.setBackgroundResource(COLORS[position - 1]);

            return textViewPosition;
        }

    }
    private static final class TransformerItem {

        final String title;
        final Class<? extends ViewPager.PageTransformer> clazz;

        public TransformerItem(Class<? extends ViewPager.PageTransformer> clazz) {
            this.clazz = clazz;
            title = clazz.getSimpleName();
        }

        @Override
        public String toString() {
            return title;
        }

    }
    private static final class PageAdapter extends FragmentStatePagerAdapter {

        public PageAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            final Bundle bundle = new Bundle();
            bundle.putInt(PlaceholderFragment.EXTRA_POSITION, position + 1);

            final PlaceholderFragment fragment = new PlaceholderFragment();
            fragment.setArguments(bundle);

            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用提供的setPageTransformer()类

MainActivity.java

...
viewPager = (ViewPager)findViewById(R.id.view);
viewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
viewPager.setPageTransformer(true,new CubeOutPageTransformer());
...

CubeOutPageTransformer.java

public class CubeOutPageTransformer implements ViewPager.PageTransformer {
    @Override
    public void transformPage(View view, float position) {
        view.setPivotX(position < 0f ? view.getWidth() : 0f);
        view.setPivotY(view.getHeight() * 0.5f);
        view.setRotationY(90f * position);
    }
}

如果您想使用其他不同的动画,请参阅here