Android:绑定导航栏的渐变背景。 setDither不工作

问题描述:

出于某种原因,我无法让我的导航栏的背景渲染流畅。我在这里做错了什么?这是我的主要活动的xml文件:Android:绑定导航栏的渐变背景。 setDither不工作

<RelativeLayout 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" 
tools:context=".MainActivity" > 

<ImageView 
    android:id="@+id/HomeBackground" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:contentDescription="@string/homescreen_desc" 
    android:src="@drawable/homescreen" 
    android:scaleType="centerCrop" 
    /> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/NavigationBar"></RelativeLayout> 
<include 
    android:layout_gravity="bottom" 
    layout="@layout/navigation_main" /> 

</RelativeLayout> 

下面是它加载的导航栏:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/NavigationBar" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:layout_alignParentBottom="true" 
android:padding="5dp" 
android:background="@drawable/gradient_bar_bg"> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal"> 

    <ImageButton 
     android:id="@+id/home_button" 
     android:contentDescription="@string/home_btn_txt" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/home" 
     android:scaleType="centerInside" 
     android:tint="#176fe6" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:background="@android:color/transparent"/> 

    <ImageButton 
     android:id="@+id/explore_button" 
     android:contentDescription="@string/explore_btn_txt" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/compass1" 
     android:scaleType="centerInside" 
     android:tint="#176fe6" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:background="@android:color/transparent"/> 

    <ImageButton 
     android:id="@+id/info_button" 
     android:contentDescription="@string/info_btn_txt" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/info2" 
     android:scaleType="centerInside" 
     android:tint="#176fe6" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:background="@android:color/transparent"/> 

    <ImageButton 
     android:id="@+id/social_button" 
     android:contentDescription="@string/social_btn_txt" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:src="@drawable/chat" 
     android:scaleType="centerInside" 
     android:tint="#176fe6" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:background="@android:color/transparent"/> 
</LinearLayout> 
</LinearLayout> 

这里的梯度绘制:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape android:shape="rectangle"> 
     <solid android:color="#004097" /> 
    </shape> 
</item> 
<item android:top="2dp"> 
    <shape> 
     <gradient 
      android:angle="90" 
      android:startColor="#FF004097" 
      android:endColor="#FF0153ca" 
      android:type="linear" /> 
    </shape> 
</item> 
</layer-list> 

最后,这里的活动:

package org.childrensmuseum.visittcmindy; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    getWindow().setFormat(PixelFormat.RGBA_8888); 
    findViewById(R.id.NavigationBar).getBackground().setDither(true); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 


} 
+0

好吧,我把它安装到了我的手机上,绑定消失了。它似乎只在模拟器中。唯一的区别是仿真器运行4.2模拟Galaxy Nexus。我的手机是运行姜饼的旧Droid Incredible(第一代)。我很快就会得到Galaxy S4。我只需要看看问题是否仍然存在于该手机上。 – LoneWolfPR

The您可以尝试更多选项:抗锯齿和过滤。

<?xml version="1.0" encoding="utf-8"?> 
<bitmap 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/your_drawable_id" 
    android:antialias="true" 
    android:dither="true" //I think this is the same one you already tried 
    android:filter="true" 
</bitmap> 

编辑:

由指向您的位图这样的一个XML文件中创建一个位图启用他们看来,在这种方式的位图XML将不会加载XML绘制...

我没有看到任何东西,除了从罗曼盖伊也许这个解决方案:

@Override 
public void onAttachedToWindow() { 
    super.onAttachedToWindow(); 
    Window window = getWindow(); 
    window.setFormat(PixelFormat.RGBA_8888); 
} 

上看到这个帖子:Android: Using linear gradient as background looks banded

你试过了吗?

+0

这不需要位图图像而不是我正在使用的渐变形状? – LoneWolfPR

+0

该文档只说:“引用可绘制资源”。我想你将不得不定义维度(左边,顶部,右边,底部属性),因为它可能需要设置其内在界限......:/让我们知道你是否尝试过。 – Guian

+0

刚刚尝试过。导致模拟器崩溃。不过谢谢。 – LoneWolfPR