天天看点

Android-6步教你自定义View

如果你打算完全定制一个view,那么你需要实现view类(所有的android view都实现于这个类),并且实现确定尺寸的onmeasure(…))方法和确认绘图的ondraw(…))方法。

自定义view一共分为6步

第一步

public class smileyview extends view { 

    private paint mcirclepaint; 

    private paint meyeandmouthpaint; 

    private float mcenterx; 

    private float mcentery; 

    private float mradius; 

    private rectf marcbounds = new rectf(); 

    public smileyview(context context) { 

        this(context, null); 

    } 

    public smileyview(context context, attributeset attrs) { 

        this(context, attrs, 0); 

    public smileyview(context context, attributeset attrs, int defstyleattr) { 

        super(context, attrs, defstyleattr); 

        initpaints(); 

    private void initpaints() {/* ... */} 

    @override 

    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {/* ... */} 

    protected void ondraw(canvas canvas) {/* ... */} 

}  

2.实现画笔paint类

本文一共两只画笔

private void initpaints() { 

    mcirclepaint = new paint(paint.anti_alias_flag); 

    mcirclepaint.setstyle(paint.style.fill); 

    mcirclepaint.setcolor(color.yellow); 

    meyeandmouthpaint = new paint(paint.anti_alias_flag); 

    meyeandmouthpaint.setstyle(paint.style.stroke); 

    meyeandmouthpaint.setstrokewidth(16 * getresources().getdisplaymetrics().density); 

    meyeandmouthpaint.setstrokecap(paint.cap.round); 

    meyeandmouthpaint.setcolor(color.black); 

3.覆写onmeasure(…)方法

实现这个方法告诉了母容器如何放弃自定义view,可以通过提供的measurespecs来决定你的view的高和宽,以下是一个正方形,确认它的宽和高是一样的。

@override 

protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { 

    int w = measurespec.getsize(widthmeasurespec); 

    int h = measurespec.getsize(heightmeasurespec); 

    int size = math.min(w, h); 

    setmeasureddimension(size, size); 

注意:

这个方法需要至少保证一个setmeasureddimension(..)调用,否则会报illegalstateexception错误。

4.实现onsizechanged(…)方法

这个方法是你获取view现在的宽和高. 这里我们计算的是中心和半径。

protected void onsizechanged(int w, int h, int oldw, int oldh) { 

    mcenterx = w / 2f; 

    mcentery = h / 2f; 

    mradius = math.min(w, h) / 2f; 

5.实现ondraw(…)方法

这个方法提供了如何绘制view,它提供的canvas类可以进行绘制。

protected void ondraw(canvas canvas) { 

    // draw face 

    canvas.drawcircle(mcenterx, mcentery, mradius, mcirclepaint); 

    // draw eyes 

    float eyeradius = mradius / 5f; 

    float eyeoffsetx = mradius / 3f; 

    float eyeoffsety = mradius / 3f; 

    canvas.drawcircle(mcenterx - eyeoffsetx, mcentery - eyeoffsety, eyeradius, meyeandmouthpaint); 

    canvas.drawcircle(mcenterx + eyeoffsetx, mcentery - eyeoffsety, eyeradius, meyeandmouthpaint); 

    // draw mouth 

    float mouthinset = mradius /3f; 

    marcbounds.set(mouthinset, mouthinset, mradius * 2 - mouthinset, mradius * 2 - mouthinset); 

    canvas.drawarc(marcbounds, 45f, 90f, false, meyeandmouthpaint); 

6.添加你的view

<framelayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="match_parent" 

    android:layout_height="match_parent"> 

    <com.example.app.smileyview 

        android:layout_width="match_parent" 

        android:layout_height="match_parent" /> 

</framelayout>  

到此就结束了,自定义view没你想的那么难

本文作者:佚名

来源:51cto

继续阅读