Android Viewer mode

Source: Internet
Author: User
Tags modifiers

1: Viewer mode:  1: Usage scenario: Generally used in the event of a custom control to listen on the above (or the encapsulation method for callback) 2: Write the Observer Mode Step: (1): Declare an Interface (2): The interface encapsulates an abstract method (3): You need to encapsulate a method, The parameter of the method is the instantiation of the interface class (used to set the listener) (4): declares an instantiated object of the interface Class (5): An interface instantiation object invokes an abstract method   interface Callback Implementation method: (1): Observer Mode (2): abstract class    1: Abstract class and interface differences: (1): The default method to implement an abstract class can have a default method, the interface is completely abstract. It doesn't exist. Implementation of the method (2): Implements an abstract class: Subclasses use the extends keyword to inherit an abstract class. If the subclass is not an abstract class, it needs to provide an implementation interface for all declared methods in the abstract class: Subclasses use the keyword implements to implement the interface. It needs to provide an implementation of all the methods declared in the interface (3): Constructor abstract class: Abstract class can have constructor interface: interface cannot have constructor (4): Abstract class differs from normal Java class: except that you cannot instantiate an abstract class, it does not have any distinguishing interface from ordinary Java classes: Interfaces are completely different types (5): Access modifiers abstract classes: Abstract methods can have public, protected, and default modifier interfaces: The interface method default modifier is public. You can not use other modifiers (6): The Main method abstract class: The abstract method can have the main method and we can run it interface: The interface does not have the main method, so we cannot run it. (7): Multiple Inheritance abstract class: An abstract method can inherit a class and implement multiple interfaces (single-inheritance) interfaces: interfaces can only implement one or more other interfaces (multiple implementations) (8): Speed abstract class: It is faster than interface speed interface: The interface is a little bit slower, because it takes time to find the method implemented in the class. (9): Add a new method abstract class: If you add a new method to an abstract class, you can give it a default implementation. So you don't need to change your current code. Interface: If you add a method to an interface, you must change the scope of the class that implements the interface  2:public,private,protected,default four modifiers: (1): Public indicates that the data member, member functions are open to all users and can be called directly by all users (2):p rivate is private, private means that no one can use it directly except class itself (under the same package, different classes are not allowed to access the private adornments in other classes. ) (3):p rotected for children and friends,It is public, free to use, without any restrictions, and for other external class,protected it becomes private. (can be called under the same package, not in the same package) (4):d efault is visible in the subclass outside the package  private this class, its subclasses and others can not call protected class, the same package under the class, the subclass can call other cannot  public this package can call   Default this class, the same package can call other cannot call  3: Custom control 1: Inherit view2: Rewrite ondraw,onmesure,onlayout3 to draw (1): Claim Brush (2): Set brush Properties ( Includes color and width and is not hollow circle and set anti-aliasing) (3): Draw Circle 4: Set Listener (1): OverrideOntouchevent Method(2): Get click Coordinates (x, y) (3): Calculate (4): If you click on Control 4 in the range: does memory leak cause oom? More memory leaks will cause oom.damage to memory leaks:only one, that is, the virtual machine consumes too much memory, causing oom (memory overflow)Workarounds for memory leaks using handlermethod One: Through the program logic to protect. 1. Stop your background thread while the activity is closed. When the thread is stopped, it is equivalent to cutting off the handler and externally connected lines, and the activity will naturally be recycled at the right time. 2. If your handler is a message with a delay, then use the corresponding handler Removecallbacks () method to remove the messaging object from the message queue. method Two: Declare the handler as a static class. static classes do not hold objects from external classes, so your activity can be recycled at will. The code is as follows:Static class MyHandler extends Handler {@OverridePublic void Handlemessage (Message msg) {Mimageview.setimagebitmap (mbitmap); }}but it's not so simple. Using the above code, you will find that because handler no longer holds references to external class objects, the program does not allow you to manipulate objects in the activity in handler. So you need to add a weak reference to activity in handler (WeakReference):Static class MyHandler extends Handler {weakreference<activity > mactivityreference;MyHandler (activity activity) {mactivityreference= New weakreference<activity> (Activity); }@OverridePublic void Handlemessage (Message msg) {Final Activity activity = Mactivityreference.get ();if (activity! = null) {Mimageview.setimagebitmap (mbitmap); } }}after changing the code to the form above, even if you complete the

Android Viewer mode

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.