Android自訂View的學習(二)

來源:互聯網
上載者:User

MainActivity如下:

package cc.testviewstudy2;import android.os.Bundle;import android.app.Activity;/** * Demo描述: * 關於自訂View的學習(二) *  * View的繪製流程:onMeasure()-->onLayout()-->onDraw() *  * 學習資料: * http://blog.csdn.net/guolin_blog/article/details/16330267 * Thank you very much *  */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}}


LinearLayoutTest如下:

package cc.testviewstudy2;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;import android.widget.LinearLayout;/** * 注意: * 1 繼承自XXXXLayout不要繼承自ViewGroup *   假如繼承自ViewGroup那麼xml的cc.testviewstudy2.ViewGroupLayout中 *   設定layout_width和layout_height不論是wrap_content還是fill_parent *   這個父視圖總是充滿螢幕的. *   現象是如此,原因暫不明. *    * 2 在本LinearLayoutTest我們只放入一個子View作為測試 */public class LinearLayoutTest extends LinearLayout {    private Paint mPaint;public LinearLayoutTest(Context context) {super(context);}public LinearLayoutTest(Context context, AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (getChildCount()>0) {View childView=getChildAt(0);//這裡的widthMeasureSpec和heightMeasureSpec都是父視圖的//這兩個值都由Size和Mode組成,所以要是如下輸出://System.out.println("widthMeasureSpec="+widthMeasureSpec+",heightMeasureSpec="+heightMeasureSpec);//會得到兩個很大的值.這個值就是Size和Mode的組合.//所以我們應該按照下面的方式來//分別擷取widthMeasureSpec和heightMeasureSpec的Size和Modeint widthSize=MeasureSpec.getSize(widthMeasureSpec);int widthMode=MeasureSpec.getMode(widthMeasureSpec);int heightSize=MeasureSpec.getSize(heightMeasureSpec);int heightMode=MeasureSpec.getMode(heightMeasureSpec);System.out.println("父視圖widthSize="+widthSize+",父視圖widthMode="+widthMode+           ",父視圖heightSize="+heightSize+",父視圖heightMode="+heightMode);//測量子ViewmeasureChild(childView, widthMeasureSpec, heightMeasureSpec);}}@Overrideprotected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {if (getChildCount()>0) {View childView=getChildAt(0);System.out.println("子視圖childView.getMeasuredWidth()="+childView.getMeasuredWidth()+           ",子視圖childView.getMeasuredHeight()="+ childView.getMeasuredHeight());//擺放子ViewchildView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());//X和Y均平移50//在此犯錯寫成了:childView.layout(50, 50, childView.getMeasuredWidth(), childView.getMeasuredHeight());//導致圖片顯示出來很小,正確的方式如下://childView.layout(50, 50, childView.getMeasuredWidth()+50, childView.getMeasuredHeight()+50);}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);mPaint = new Paint();mPaint.setColor(Color.YELLOW);mPaint.setTextSize(20);String content = "Hello World";canvas.drawText(content, 150, 100, mPaint);}}

main.xml如下:

    




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.