2014-05-14 73 views
0

您好,我正在嘗試設置背景圖像的LinearLayout,首先我需要使其四捨五入,我使用shape標記並調用setBackgroundResource使其圓整但現在我應該怎麼設置任何可繪製也在同一上LinearLayout如何在運行時設置線性佈局的背景+圓角形狀

layout_rounded.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#FFFFFF"/> 
    <stroke android:width="3dip" android:color="#B1BCBE" /> 
    <corners android:radius="10dip"/> 
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> 
</shape> 

運行時間,我這樣做

linearLayout.setBackgroundResource(R.drawable.layout_rounded); 

這裏設置在LinearLayout的背景,然後我打電話給setBackgroundDrawable但調用此之後,其設置可繪製但同樣取消設置LinearLayout的形狀。

我沒有在那個水庫繪製,這樣的背景下,我從URL獲取然後試圖設置

能否請你看看如何實現這一目標?

在此先感謝

+0

您在'layout_rounded.xml'本身設置了背景.. – Lal

+0

@Lal我沒有那個可繪製的'res',那個背景我從URL –

+0

首先明確了一件事,我要設置什麼'Image'或'rounded_xml'? –

回答

0

最後我簡單地使位圖圓形這樣做。現在有沒有必要給該LinearLayout外形圓潤,因爲我設置爲LinearLayout位圖是正在LinearLayout形狀滾圓滾圓形狀。

import android.content.res.Resources; 
import android.graphics.Paint; 
import android.graphics.BitmapShader; 
import android.graphics.RectF; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Shader; 
import android.graphics.drawable.BitmapDrawable; 

public class RoundedCornersDrawable extends BitmapDrawable { 

    private final BitmapShader bitmapShader; 
    private final Paint p; 
    private final RectF rect; 
    private final float borderRadius; 

    public RoundedCornersDrawable(final Resources resources, final Bitmap bitmap) { 
    super(resources, bitmap); 
    bitmapShader = new BitmapShader(getBitmap(), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
    final Bitmap b = getBitmap(); 
    p = getPaint(); 
    p.setAntiAlias(true); 
    p.setShader(bitmapShader); 
    final int w = b.getWidth(); 
    final int h = b.getHeight(); 
    rect = new RectF(0, 0, w, h); 
    borderRadius = 0.15f * Math.min(w, h); 
    } 

    @Override 
    public void draw(final Canvas canvas) { 
    canvas.drawRoundRect(rect, borderRadius, borderRadius, p); 
    } 
} 

這是對我有幫助,爲我所尋找的工作。

1

使用Shape在android系統,使圓邊角

創建將它命名爲XML文件作爲roundcorner.xml

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
     <solid android:color="#33DDFF" /> 
     <corners android:radius="4dp" /> 
    </shape> 

在你ImageButton添加此屬性android:background="@drawable/roundcorner"

<ImageButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/imageButton" 
       android:layout_marginTop="57dp" 
       android:src="@drawable/friends" 
       android:background="@drawable/roundcorner" 
       android:padding="1dp" 
       android:layout_alignParentTop="true" 
       android:layout_toLeftOf="@+id/imageButton2" 
       android:layout_marginRight="62dp" /> 

OR

使用圖像視圖設置android:src = "your image"android:background= "your shape "android:clickable="true"

+0

它使用'setColor'設置背景顏色,這不是我正在尋找 –

+0

請參閱我的編輯@Williams – Lal

+0

'GradientDrawable'對象上沒有方法'setBackgrounddrawable',請參閱文檔 –

0
// Try this way,hope this will help you.. 

Note Here i used FrameLayout insted of LinearLayout due to achieved your requiremt. 

**activity_main.xml** 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


</FrameLayout> 

**MainActivity.java** 

public class MainActivity extends Activity { 

    FrameLayout frame; 
    protected void onCreate(Bundle paramBundle) { 
     super.onCreate(paramBundle); 
     setContentView(R.layout.activity_main); 
     frame = (FrameLayout) findViewById(R.id.frame); 
     frame.setBackgroundResource(R.drawable.layout_round); // set your rounded background here 
     frame.setForeground(getResources().getDrawable(R.drawable.ic_launcher)); // set your background image here 
    } 


} 
相關問題