Android Custom View Process parsing _android

Source: Internet
Author: User
Tags drawtext

Android Custom view, mainly inherits view, then implements OnDraw this method, to draw.

    • 1. Write your own custom view
    • 2. Join Logical Threads
    • 3. Extracting and Encapsulating custom view
    • 4. Use XML to define styles to affect display effects

First, write your own custom view
1. Use your own view in XML

 <!--can use the public properties of the view, such as background-->
 <com.niuli.view.myview 
  android:layout_width= "Match_parent"
  android:layout_height= "Match_parent"
  android:background= "#ffff00"
  />

2. By inheriting view, and then implementing OnDraw to implement the method

public class MyView extends View {Bitmap Bitmap;
  Public MyView {Super (context); 
 /** * Getresources () can obtain resources within the current resource * * * bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
  Public MyView (context, AttributeSet attrs) {Super (context, attrs);
 Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher); /** * Drawing is carried by Canvas, which is equivalent to a canvas * More learning will be updated in the continuous small things/@Override protected void OnDraw (Canvas Canvas) {super.
  OnDraw (canvas);
  /** * Paint is equivalent to a brush, you can draw text, geometry, bit pictures and so on * * Paint Paint = new Paint ();

  Set the color of the drawing, whether the hollow and so on is the design of the Brush Paint.setcolor (Color.Blue);
  Painting text paint.settextsize (30);
  Canvas.drawtext ("This is a custom view", 0, paint);
  Painting Straight line Canvas.drawline (0, N, m, paint); Draw Rounded Rectangle method one//Set hollow Paint.setstyle (Paint.getstyle ().

  STROKE);
  RECTF rec = new RECTF (0, 90, 100, 190);
  Canvas.drawroundrect (REC, a, paint); Draw Pictures Canvas.drawbitmap (bitmap, 0, 350, paint);

 }

}

Effect

Second, custom view+ thread usage control
Draws text, and circles, through thread control so that it can move through the screen

public class Myviewtwo extends View {//definition brush private Paint Paint = new Paint ();
 private int x;
 private float sweepangle;
 private control thread;

 Private Random Random = new Random ();
 Public Myviewtwo (context, AttributeSet attrs) {Super (context, attrs);
 Public Myviewtwo {Super (context);
  } @Override protected void OnDraw (Canvas Canvas) {paint.settextsize (80);
  Draw Text Canvas.drawtext ("Happy Tanabata", X, Paint,);
  Draw graphics RECTF rect = new RECTF (0, 80, 100, 160);

  Canvas.drawarc (rect, 0, Sweepangle, true, paint);
   Start thread if (thread ==null) {thread = new control ();
  Thread.Start ();
    Extends thread{@Override public void Run () {while (true) {x +=3;
    sweepangle++;
    if (X>getwidth ()) {x = (int) (0-paint.measuretext ("Happy Tanabata"));
    }//Control circle of rotation if (sweepangle>360) {sweepangle = 0; //Set Brush color Paint.setargb (255, Random.nextint (255), Random.nextint (255), RAndom.nextint (255));

    Equivalent to refreshing the canvas postinvalidate ();
    try {sleep (30);
    catch (Interruptedexception e) {e.printstacktrace ();
 }
   }
  }
 }

Effect

Third, packaging Mobile program, modular Thinking
encapsulation is mainly the use of abstract methods, the subclass inherits as long as the implementation of these methods can be run, greatly simplifying the program

Public abstract class Myviewtwo extends View {

 private control thread;

 Public Myviewtwo (context, AttributeSet attrs) {
  Super (context, attrs);
 }

 Public Myviewtwo {
  super (context);
 }
 Encapsulation, constructs the picture, after the subclass inherits needs to rewrite
 protected abstract void Drawsub (Canvas Canvas);

 @Override
 protected final void OnDraw (Canvas Canvas) {

  //boot thread
  if (thread ==null) {
   thread = new CONTR OL ();
   Thread.Start ();
  } else {
   drawsub (canvas);
  }
 }
 Encapsulates the Move method, which requires overriding the
 protected abstract void Move () after the subclass inherits;

 The public class control extends thread{@Override the public
  Void Run () {while
   (true) (
    ).
    Equivalent to refreshing the canvas
    postinvalidate ();

    try {sleep
     ;
    } catch (Interruptedexception e) {
     e.printstacktrace ();
    }

}}}

iv. using defined styles in XML to affect display effects
1, the first step is to create their own style files in the value folder

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
 <declare-styleable name= "Numtext" >
  <attr name= "linenum" format= "integer"/> <attr name= "
  xscroll" format= "boolean"/>
 </ Declare-styleable>
</resources>

2, in the XML first to join the namespace, and then you can directly use the attribute

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"
 xmlns:tools= "http:// Schemas.android.com/tools "
 xmlns:nt=" Http://schemas.android.com/apk/res/com.jikexueyuan.myview
 " Android:id= "@+id/container"
 android:layout_width= "match_parent"
 android:layout_height= "Match_parent" >

 <com.jikexueyuan.myview.v4.numtext 
  android:layout_width= "match_parent"
  android:layout_ height= "Match_parent" 
  nt:linenum= "6"
  nt:xscroll= "true"/>

</FrameLayout>

3, in the code to have the corresponding parsing XML defined in this element

Public Numtext (context, AttributeSet attrs) {
  Super (context, attrs);

  TypedArray ta = context.obtainstyledattributes (attrs, r.styleable.numtext);
  LineNum = Ta.getint (r.styleable.numtext_linenum, 1);
  Xscroll = Ta.getboolean (R.styleable.numtext_xscroll, false);
  Ta.recycle ();
 }

The main thing is to use the above method and the XML to define the element value to be associated with exposure.

The above is the entire content of this article, I hope to help you learn.

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.