如何在弹出时为片段内的textview设置不透明度?

时间:2015-05-22 05:13:20

标签: android android-fragments android-viewpager

如何为片段内的textview提供不透明度?

最初我有一个水平的圆形ViewPager,我的片段里面的TextView在聚焦到屏幕并分别离开屏幕时应淡入并淡出。

我在onTouchListener上做了一些实验,但我没有到达任何地方。我也尝试设置一个硬编码的alpha值,但它没有按预期工作。

float prev = 0;
@Override
public void getPositionValue(float x1,float x2){
    AppUtils.alphaCalculator(x2,prev,deviceWidth);
    prev = x2;
    int index = viewPager.getCurrentItem();
    CustomViewPagerAdapter customViewPagerAdapter = (CustomViewPagerAdapter) viewPagerAdapter;
    ImageSliderFragment fragment = customViewPagerAdapter.getFragment(index);
    fragment.getView().findViewById(R.id.head_data).setAlpha(50);
    fragment.getView().findViewById(R.id.head_data).invalidate();
    ImageSliderFragment fragment2 = customViewPagerAdapter.getFragment(index+1);
    fragment2.getView().findViewById(R.id.head_data).setAlpha(50);
    fragment2.getView().findViewById(R.id.head_data).invalidate();
}

任何人都可以就如何实现这一目标提出一些想法......

据kartheek说,我能得到偏见,下面是我的代码

if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.findViewById(R.id.head_data).setAlpha(0.10f);
        } else if (position <= 1) { // [-1,1]

            if(position<0.1f){
                view.findViewById(R.id.head_data).setAlpha(1.0f);
            }else if(position<0.2f && position>0.1f){
                view.findViewById(R.id.head_data).setAlpha(0.68f);
            }else if(position<0.3f && position>0.2f){
                view.findViewById(R.id.head_data).setAlpha(0.55f);
            }else if(position<0.4f && position>0.3f){
                view.findViewById(R.id.head_data).setAlpha(0.45f);
            }else if(position<0.5f && position>0.4f){
                view.findViewById(R.id.head_data).setAlpha(0.38f);
            }else if(position<0.6f && position>0.5f){
                view.findViewById(R.id.head_data).setAlpha(0.30f);
            }else if(position<0.7f && position>0.6f){
                view.findViewById(R.id.head_data).setAlpha(0.22f);
            }else if(position<0.8f && position>0.7f){
                view.findViewById(R.id.head_data).setAlpha(0.15f);
            }else if(position<0.9f && position>0.8f){
                view.findViewById(R.id.head_data).setAlpha(0.10f);
            }else if(position<1.0f && position>0.9f){
                view.findViewById(R.id.head_data).setAlpha(0.05f);
            }

            if(position<-0.1f){
                view.findViewById(R.id.head_data).setAlpha(1.0f);
            }else if(position<-0.2f && position>-0.1f){
                view.findViewById(R.id.head_data).setAlpha(0.68f);
            }else if(position<-0.3f && position>-0.2f){
                view.findViewById(R.id.head_data).setAlpha(0.55f);
            }else if(position<-0.4f && position>-0.3f){
                view.findViewById(R.id.head_data).setAlpha(0.45f);
            }else if(position<-0.5f && position>-0.4f){
                view.findViewById(R.id.head_data).setAlpha(0.38f);
            }else if(position<-0.6f && position>-0.5f){
                view.findViewById(R.id.head_data).setAlpha(0.30f);
            }else if(position<-0.7f && position>-0.6f){
                view.findViewById(R.id.head_data).setAlpha(0.22f);
            }else if(position<-0.8f && position>-0.7f){
                view.findViewById(R.id.head_data).setAlpha(0.15f);
            }else if(position<-0.9f && position>-0.8f){
                view.findViewById(R.id.head_data).setAlpha(0.10f);
            }else if(position<-1.0f && position>-0.9f){
                view.findViewById(R.id.head_data).setAlpha(0.05f);
            }

        } else { // (1,+Infinity]
            // This page is way off-screen to the right
            view.findViewById(R.id.head_data).setAlpha(0.10f);
        }

图像从右侧聚焦,从左侧到右侧正常工作。

但不是以任何一种方式工作

1 个答案:

答案 0 :(得分:1)

这可以通过ViewPager.PageTransformer实现,它会在fragmentsViewPager滑动时提供回调。这是一个小例子

public class SimplePageTransformer implements ViewPager.PageTransformer {

        public void transformPage(View view, float position) {

            int pageWidth = view.getWidth();


             if (position < -1) { // [-Infinity,-1)
                    // This page is way off-screen to the left.
                } else if (position <= 1) { // [-1,1]

                } else { // (1,+Infinity]
                    // This page is way off-screen to the right
                }
        }
    }

您可以从此链接中找到detail example