2014-05-16 43 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