Android Custom View Notepad out scabbard

Source: Internet
Author: User

Now our mobile phone basically will have a notepad, use up is also convenient, notepad this stuff, if I want to achieve, how to do? Today we customize a notepad by customizing the view. OK, don't say a lot of nonsense, first to see.

The entire page is still very simple.

1. Customize the View category

OK, so before the text starts, I'd like to start by saying custom view categories, custom view we are divided into three categories

1. Self-drawing controls

The self-Drawing control is our custom view that inherits from the controls, and then expands its functionality, before two custom view blogs (Android Custom view of the birth of the clock, Android custom view of the imitation Address Book side bar sliding, to achieve a-Z letter search) belong to this one.

2. Inheriting from existing controls extending its functionality

Sometimes, we can also inherit from an existing control and then extend its functionality, such as the notepad that we are going to say right now, that is, inherit from EditText, and then extend its function

3. Combining controls

is to put some existing controls together to form a new control, which can refer to my previous blog Android custom UI template graphic detailed

2. Realization of Ideas

OK, finish the classification of the custom view, let's take a look at the implementation of the Notepad to be introduced in this article. First of all, this kind of notebook background is actually a picture, but this one line is I through the canvas DrawLine this API to draw up. When I click on the carriage return, I will go on to draw a new horizontal line. This whole idea is also very simple.

3. Code implementation

First I'll create a class that inherits from EditText, and then I'll declare a few variables, as follows:

[Java]View Plaincopy print?
  1. line width of horizontal lines
  2. Private int linewidth = 1;
  3. The color of the horizontal line
  4. Private int linecolor = Color.Black;
  5. Line spacing
  6. Private int spacing_line = 10;
  7. Inner margin
  8. private int padding = 10;
  9. Brush
  10. Private Paint Mpaint;


Then do some initialization operations in the constructor, as follows:

[Java]View Plaincopy print?
  1. Public NotePad (Context context) {
  2. This (context, null);
  3. }
  4. Public NotePad (context context, AttributeSet attrs) {
  5. This (context, Attrs, 0);
  6. }
  7. Public NotePad (context context, AttributeSet attrs, int defstyleattr) {
  8. Super (context, attrs, defstyleattr);
  9. Setfocusableintouchmode (true);
  10. Setgravity (Gravity.top);
  11. Setlinespacing (Spacing_line, 1);
  12. Setpadding (+, padding, 10);
  13. Mpaint = new Paint ();
  14. Mpaint.setcolor (LineColor);
  15. Mpaint.setstrokewidth (linewidth);
  16. }

Setfocusableintouchmode method can let my Notepad get focus, setgravity let me notepad the default position of the cursor in the upper left corner of the control, the Setlinespacing method is mainly to set the line spacing, Setpadding is to set the padding, the last brush initialization will not have to say more.

OK, after the initialization of the operation, we can then complete the horizontal line drawing, the code is as follows:

[Java]View Plaincopy print?
  1. @Override
  2. protected void OnDraw (canvas canvas) {
  3. Super.ondraw (canvas);
  4. //Get the height of the screen
  5. int viewheight = GetHeight ();
  6. //Get the height of each line of text
  7. int lineheight = Getlineheight ();
  8. //Calculate the total number of rows per page
  9. int pagecount = viewheight/lineheight;
  10. //Circular Draw horizontal line
  11. For (int i = 0; i < PageCount; i++) {
  12. Canvas.drawline (padding, (i + 1) * lineheight, getwidth ()-Padding, (i + 1) * lineheight, Mpaint);
  13. }
  14. //Gets the total number of rows in the current text
  15. int linecount = Getlinecount ();
  16. The number of lines in the text minus the number of rows per page, the remaining value is the number of rows I want to continue drawing
  17. int extracount = Linecount-pagecount;
  18. if (Extracount > 0) {
  19. For (int i = PageCount; i < LineCount; i++) {
  20. Canvas.drawline (padding, (i + 1) * lineheight, getwidth ()-Padding, (i + 1) * lineheight, Mpaint);
  21. }
  22. }
  23. }


Please see the comment for the code explanation.

OK, that's so simple. Finally, let's refer to my custom control in the layout file:

[Java]View Plaincopy print?
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <relativelayout
  3. xmlns:android="Http://schemas.android.com/apk/res/android"
  4. xmlns:tools="Http://schemas.android.com/tools"
  5. Android:layout_width="Match_parent"
  6. android:layout_height="Match_parent"
  7. tools:context="Org.lenve.notepad.MainActivity" >
  8. <org.lenve.notepad.notepad
  9. Android:layout_width="Match_parent"
  10. android:layout_height="Match_parent"
  11. android:background="@drawable/background"/>
  12. </RelativeLayout>


Once you've done this, you'll be able to get your app running.

SOURCE Download http://download.csdn.net/detail/u012702547/9502328

Above.

Android Custom View Notepad out scabbard

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.