2014-05-16 40 views
0

我想在背景上使用较小的图片/背景色,但我希望较小图片/背景的边缘具有渐变边缘,因此它不仅仅是方形的砖。如何在Android上使用具有渐变边缘的背景或图像?

下面的代码我有我的FrameLayout的背景图像和的FrameLayout里面的白色在我的LinearLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    tools:context="com.example.snake.LevelScoreActivity" 
    tools:ignore="MergeRootFrame" 
    android:background="@drawable/snakebackground"> 

    <TextView 
     android:id="@+id/outputTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:gravity="center" 
     android:layout_gravity="center_horizontal" 
     android:text="Level X Score" 
     android:textSize="14dp"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center_horizontal" 
     android:background="#ffffff"> 

    <TextView 
     android:id="@+id/output" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:gravity="center" 
     android:text="Username" /> 

    <TextView 
     android:id="@+id/output2" 
     android:layout_width="40dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:gravity="center" 
     android:text="Score" /> 

    <TextView 
     android:id="@+id/output3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:gravity="center" 
     android:text="Timestamp" /> 

    </LinearLayout> 

     <TextView 
     android:id="@+id/outputNoScore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:text="" 
     android:textSize="14dp"/> 

</FrameLayout> 

我怎样才能做到这一点?

+0

我的第一个直觉是用透明胶片定制'GradientDrawable'。但我不确定这是否可以用'GradientDrawable'。 –

+0

只是简单的自定义BitmapDrawable将会诀窍 – pskink

回答

0

您应该直接淡出你的图像中的图像编辑软件,而不是在你的Android应用程序做,而不是淡出宽度取决于一个变量...

+0

你能重新解释一下吗?我不太明白。 – Vanquiza

+0

如果你只是想让你的背景图像的边缘变淡,为什么不直接在原始图像上淡出一次,而不是通过android中的代码来做到这一点?对不起我的英语不好。我希望你明白我的意思。 – bobby4078

0

试试这个定制BitmapDrawable:

class BD extends BitmapDrawable { 
    private Paint mPaint; 
    private Paint mSrcInPaint; 
    private int mRadius; 
    private Rect mDrawRect; 

    public BD(Resources res, Bitmap b, int radius) { 
     super(res, b); 
     mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     mPaint.setMaskFilter(new BlurMaskFilter(radius, Blur.NORMAL)); 

     mSrcInPaint = new Paint(); 
     mSrcInPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
     mRadius = radius; 
     mDrawRect = new Rect(); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     int cnt = canvas.saveLayer(null, null, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG); 
     mDrawRect.set(getBounds()); 
     mDrawRect.inset(mRadius, mRadius); 
     canvas.drawRect(mDrawRect, mPaint); 

     canvas.saveLayer(null, mSrcInPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG); 
     super.draw(canvas); 
     canvas.restoreToCount(cnt); 
    } 
} 
+0

我将如何使用这个类来设置我的线性布局的背景?如果我不完全理解,我很抱歉,但我对Android非常陌生。 – Vanquiza

+0

setBackgroundDrawable – pskink

+0

我知道设置背景的命令,我的意思是,通常你把你的照片,例如'setBackgroundDrawable(R.drawable.blabl.png)',但我该如何使用这种方法,并使用“转换图片“作为setBackgroudndrawable的参数? – Vanquiza