2013-05-28 34 views
4

我對Android非常陌生,我試圖在自定義視圖上繪製(使用畫布)。我有一些& rects那裏。關鍵是,我現在想給整個視圖帶來圓角,但是這樣做效果不好,因爲我正在繪製視圖,而且我的繪圖位於通過資源添加的圓角之上。是否有可能添加圓角,覆蓋整個視圖?將圓角添加到自定義視圖

最好的問候,並感謝任何幫助!

+0

是你自定義佈局是佈局文件嗎?包含任何容器假設線性佈局或其他 – UnderGround

+0

我的自定義視圖從視圖延伸,只是一個視圖,我在其上繪製東西。我的做法是給它圓角。 –

+0

但是,這種觀點是膨脹任何佈局文件或不,因爲我不熟悉的觀點,爲什麼問或爲什麼我們可以設置背景視圖? – UnderGround

回答

4

我不確定你的要求。但是你可以使用下面的和修改相同

activity_main.xml中

<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" 
android:id="@+id/rl" 
android:background="@drawable/bkg" //set background 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 
</RelativeLayout> 

bkg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<solid android:color="#10EB0A"/> //set color 
<stroke android:width="3dp"   // set border 
android:color="#0FECFF" />   //set border color 
<padding android:left="5dp"   //set padding 
android:top="5dp" 
android:right="5dp" 
android:bottom="5dp"/> 
<corners android:bottomRightRadius="20dp" //set radius 
android:bottomLeftRadius="20dp" 
android:topLeftRadius="20dp" 
android:topRightRadius="20dp"/> 
</shape> 

MainActivity.java

public class MainActivity extends Activity { 

RelativeLayout rl; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    rl = (RelativeLayout) findViewById(R.id.rl); 
    CustomView cv = new CustomView(this); 
    rl.addView(cv); //add custom view to the relative layout 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
class CustomView extends View 
{ 
    Bitmap bmp; 
    PaintDrawable mDrawable; 

    public CustomView(Context context) { 
     super(context); 
     bmp = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     super.onDraw(canvas); 

     canvas.drawBitmap(bmp, 200, 200, null); 
    } 
     } 
    } 

快照

enter image description here

+0

嗨,這不能正常工作,因爲這不起作用與圖紙。不過謝謝你的回答! –

+0

@LucèBrùlè它與繪圖一起工作。我在畫布上繪製了一個位圖圖像。你能看到屏幕上的ic_launcher.png嗎? canvas.drawBitmap(bmp,200,200,null);你可以繪製線條或路徑 – Raghunandan

+0

我畫了很多行和recs,填充整個視圖,然後它不起作用,因爲我的繪圖在圓角以上:( –

3
public class RoundCornerView extends View{ 
    public RoundCornerView(Context context) { 
     super(context); 
    } 

    public RoundCornerView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public RoundCornerView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    protected void onDraw(android.graphics.Canvas canvas) 
    { 
     Paint paint = new Paint(); 

     paint.setAlpha(255); 
     canvas.translate(0, 30); 
     paint.setColor(Color.BLUE); 
     Path mPath = new Path(); 
     mPath.addRoundRect(new RectF(0, 0, 100,100),20,20, Path.Direction.CCW); 
     canvas.clipPath(mPath, Region.Op.INTERSECT); 
     paint.setColor(Color.GREEN); 
     paint.setAntiAlias(true); 
     canvas.drawRect(0, 0, 120,120,paint); 

    } 
} 

嘗試使用clippath,但正如一個側面說明3.0後,您需要關閉硬件加速在你的清單

安卓硬件加速=「假」

有解決方案,當成立時將張貼在這裏作爲補充

+0

這是圖像與圓角。 OP是要求帶圓角的自定義視圖 – Raghunandan

+0

事實上,我的糟糕之處在於,在文章的評論中還有一些關於佈局中圓角的討論。而Imageview是View的一個子類 – QVDev

+0

我建議你可以在xml文件中定義你的線性佈局或相對位置。使用形狀(圓角)設置背景。將內容設置爲活動。初始化視圖。創建自定義視圖。將自定義視圖添加到佈局。在評論相關佈局 – Raghunandan