android - viewPager 背景颜色对我的图像的影响

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

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

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

问题出在这里:

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

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

截图如下: enter image description here

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

这是我的代码:

fragment 寻呼机

<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 寻呼机

<?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>

我在哪里设置颜色:

寻呼机 Activity

    ...
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;
            }
            ...

最佳答案

经过一番折腾终于找到了一个奇怪的解决办法: 图像的setAlpha(1.0f)就可以解决!!! (这很糟糕,因为它仅适用于 API 11 及更高版本) 好奇怪……

关于android - viewPager 背景颜色对我的图像的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34485646/

相关文章:

android - 在 Android 中禁用 native logcat 输出

Android adb pm 禁用组件

java - 更改方向时应用程序崩溃

android - 在 fragment 中添加可扩展列表

android - OpenGL 纹理到 AHardwareBuffer

android - 如何将 fragment 显示为对话框?

java - 如何传递ArrayList<Fragment>?

android-viewpager - 如何使用tablayout在viewpager中设置当前选项卡

android - 查看分页器2/回收器 View 崩溃的不一致

从 backStack 返回 fragment 后,Android ViewPager 无法正确显示