viewPager背景颜色效果对我的图像

时间:2015-12-27 22:43:29

标签: android android-fragments android-viewpager android-inflate

我不知道怎么能说出我的问题, 我在CoordinatorLayout中有一个带ViewPager的xml文件,

在另一个xml布局(片段布局)中我有ImageView, 我膨胀我的片段布局并在viewpager中显示它,

问题在于:

当我更改(或甚至设置)ViewPager(或者还有CoordinatorLayout)背景颜色时, 颜色对我的图像有影响(看起来图像在ViewPager / CoordinatorLayout下)

如何禁用此效果? 谢谢你的关注

这是一个截图: enter image description here

你看到图像变成绿色,但它应该在顶部并保存为原始模式

这是我的代码:

fragment_pager

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
>


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/section_img"
        android:layout_width="192dp"
        android:layout_height="192dp"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:alpha="0.30"
        android:background="@drawable/fab_shadow"
        android:scaleType="fitCenter" />


</FrameLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="@dimen/activity_horizontal_margin">

    <TextView
        android:id="@+id/section_label"
        style="@style/TextAppearance.AppCompat.Headline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        tools:text="Page One" />

    <TextView
        style="@style/TextAppearance.AppCompat.Body1"
        android:id="@+id/describe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:alpha="0.7"
        android:gravity="center"
        android:text="Add a long description here about your app's
        important feature."
        android:textColor="@android:color/white" />


</LinearLayout>

</FrameLayout>

activity_pager

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5fccff"
>


<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="?attr/actionBarSize"

    />


<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_gravity="bottom"
    android:layout_marginBottom="?attr/actionBarSize"
    android:alpha="0.12"
    android:background="@android:color/white" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_gravity="bottom"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingStart="@dimen/activity_horizontal_margin">


    <Button
        android:id="@+id/intro_btn_skip"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start|center"
        android:text="Skip"
        android:textColor="@android:color/white" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/intro_indicator_0"
            android:layout_width="8dp"
            android:layout_height="8dp"
            android:layout_marginEnd="@dimen/activity_margin_half"
            android:layout_marginRight="@dimen/activity_margin_half"
            android:background="@drawable/in" />

        <ImageView
            android:id="@+id/intro_indicator_1"
            android:layout_width="8dp"
            android:layout_height="8dp"
            android:layout_marginEnd="@dimen/activity_margin_half"
            android:layout_marginRight="@dimen/activity_margin_half"
            android:background="@drawable/out" />

        <ImageView
            android:id="@+id/intro_indicator_2"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="8dp"
            android:layout_height="8dp"
            android:background="@drawable/out" />

    </LinearLayout>


    <Button
        android:id="@+id/intro_btn_finish"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|center"
        android:text="Finish"
        android:textColor="@android:color/white"
        android:visibility="gone" />

    <ImageButton
        android:id="@+id/intro_btn_next"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|center"
        android:padding="@dimen/activity_horizontal_margin"
        android:src="@drawable/ic_action_add"
        android:tint="@android:color/white" />


</FrameLayout>


</android.support.design.widget.CoordinatorLayout>

我在哪里设置颜色:

PagerActivity

    ...
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().setStatusBarColor(ContextCompat.getColor(this,
        R.color.black_trans80));
    }

    setContentView(R.layout.activity_pager);


    // Create the adapter that will return a fragment for each of the
     three
    // primary sections of the activity.
    mSectionsPagerAdapter = new
    SectionsPagerAdapter(getSupportFragmentManager());

    mNextBtn = (ImageButton) findViewById(R.id.intro_btn_next);

    mSkipBtn = (Button) findViewById(R.id.intro_btn_skip);
    mFinishBtn = (Button) findViewById(R.id.intro_btn_finish);

    zero = (ImageView) findViewById(R.id.intro_indicator_0);
    one = (ImageView) findViewById(R.id.intro_indicator_1);
    two = (ImageView) findViewById(R.id.intro_indicator_2);



    mCoordinator = (CoordinatorLayout) findViewById(R.id.main_content);


    indicators = new ImageView[]{zero, one, two};

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setPageTransformer(false, new IntroPageTransformer());

    mViewPager.setCurrentItem(page);
    updateIndicators(page);

    final int color1 = ContextCompat.getColor(this, R.color.cyan);
    final int color2 = ContextCompat.getColor(this, R.color.orange);
    final int color3 = ContextCompat.getColor(this, R.color.green);

    final int[] colorList = new int[]{color1, color2, color3};

    final ArgbEvaluator evaluator = new ArgbEvaluator();

    mViewPager.addOnPageChangeListener(new
    ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) {

            /*
            color update
             */
            int colorUpdate = (Integer)
            evaluator.evaluate(positionOffset, colorList[position],
            colorList[position == 2 ? position : position + 1]);
            mCoordinator.setBackgroundColor(colorUpdate);

        }

        @Override
        public void onPageSelected(int position) {

            page = position;

            updateIndicators(page);

            switch (position) {
                case 0:
                    mCoordinator.setBackgroundColor(color1);
                    break;
                case 1:
                    mCoordinator.setBackgroundColor(color2);
                    break;
                case 2:
                    mCoordinator.setBackgroundColor(color3);
                    break;
            }
            ...

1 个答案:

答案 0 :(得分:1)

经过一番唠叨终于找到了一个奇怪的解决方案: setAlpha(1.0f)的图像可以解决它! (它很糟糕,因为它仅适用于API 11及更高版本) 太奇怪了......