Learning Android custom View (3)

Source: Internet
Author: User

MainActivity is as follows:

Package cc. testviewstudy3; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. app. activity;/*** Demo Description: * learning about custom View (3) ** there are three methods to implement custom View: * Self-painted controls, composite controls, and inheritance controls * In this Demo, You can implement self-painted controls and composite controls ** learning materials: * http://blog.csdn.net/guolin_blog/article/details/17357967 * Thank you very much **/public class MainActivity extends Activity {private TitleViewFrameLayout mTitleViewFrameLayout; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mTitleViewFrameLayout = (TitleViewFrameLayout) findViewById (R. id. titleViewFrameLayout); mTitleViewFrameLayout. setBackButtonText ("return"); mTitleViewFrameLayout. setTitleTextViewText ("title"); mTitleViewFrameLayout. setButtonClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {System. out. println ("Click Back"); finish ();}});}}


CounterView:

Package cc. testviewstudy3; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. util. attributeSet; import android. view. view; // The self-painted control public class CounterView extends View implements View. onClickListener {private Paint mPaint; private Rect mRect; private Rect mTextBoundsRect; private int counter = 0; public CounterView (Context context) {super (context);} public CounterView (Context context, attributeSet attrs) {super (context, attrs); mPaint = new Paint (Paint. ANTI_ALIAS_FLAG); mRect = new Rect (); mTextBoundsRect = new Rect (); // set the listener to setOnClickListener (this);} @ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); mPaint. setColor (Color. YELLOW); // getWidth () and getHeight () indicate the width and height of the custom View canvas. drawRect (0, 0, getWidth (), getHeight (), mPaint); mPaint. setColor (Color. BLUE); mPaint. setTextSize (40); String counterString = String. valueOf (counter); // measure the width and height of the text. Save the result to a Rect. mTextBoundsRectmPaint. getTextBounds (counterString, 0, counterString. length (), mTextBoundsRect); float textBoundWidth = mTextBoundsRect. width (); float textBoundHeight = mTextBoundsRect. height (); // System. out. println ("textBoundWidth =" + textBoundWidth + ", textBoundHeight =" + textBoundHeight); // draw a text canvas. drawText (counterString, getWidth ()/2-textBoundWidth/2, getHeight ()/2 + textBoundHeight/2, mPaint);} @ Overridepublic void onClick (View v) {counter ++; // redraw. the onDraw () method invalidate () ;}} will be called ();}}

TitleViewFrameLayout is as follows:

Package cc. testviewstudy3; import android. content. context; import android. util. attributeSet; import android. view. layoutInflater; import android. widget. button; import android. widget. frameLayout; import android. widget. textView; // composite control public class TitleViewFrameLayout extends FrameLayout {private Button mBackButton; private TextView mTitleTextView; public attributes (Context context, AttributeSet attrs) {super (context, attrs); LayoutInflater. from (context ). inflate (R. layout. test_title, this); mBackButton = (Button) findViewById (R. id. backButton); mTitleTextView = (TextView) findViewById (R. id. titleTextView);} // definition method ---> set the public void setBackButtonText (String text) {mBackButton. setText (text) ;}// define method ---> set the Button click listener public void setButtonClickListener (OnClickListener listener) {mBackButton. setOnClickListener (listener);} // definition method ---> set the TextView text public void setTitleTextViewText (String text) {mTitleTextView. setText (text );}}


Main. xml is as follows:

     
      
  
 

Test_title:

 
     
      
  
 


Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.