CN108647024A - A kind of implementation method of slide switch button - Google Patents
A kind of implementation method of slide switch button Download PDFInfo
- Publication number
- CN108647024A CN108647024A CN201810336744.9A CN201810336744A CN108647024A CN 108647024 A CN108647024 A CN 108647024A CN 201810336744 A CN201810336744 A CN 201810336744A CN 108647024 A CN108647024 A CN 108647024A
- Authority
- CN
- China
- Prior art keywords
- android system
- methods
- switch button
- slide switch
- variable
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/38—Creation or generation of source code for implementing user interfaces
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Human Computer Interaction (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Processing Or Creating Images (AREA)
Abstract
The present invention relates to a kind of implementation methods of slide switch button, and steps are as follows:1), define slide switch button class SlideSwitch;2), definition is monitored and callback interface;3), call parameter is defined, for setting the relevant basic information of slide switch button;4), Define Brush variable mPaint;5), defined variable mOnSwitchChangedListener, state passback when for external call;6), bitmap is initialized, the width and height of background picture are calculated;7), rewrite onTouchEvent methods, the event that monitoring users are pressed, moved, lifting, and sliding animation process;8), onDraw methods are rewritten according to different state rendering context figures and text prompt information.The present invention passes through self-defined Switch switch buttons(Switch controls), realize that slide switch button, different editions, different type of machines have the same landscaping effect, can increase mobile animation effect on demand, reduce the lofty sense of state mutation, increase the experience of user.
Description
Technical field
The present invention relates to switch button interaction technique field, specifically a kind of implementation method of slide switch button.
Background technology
Although android system provides Switch switch buttons(Switch controls), but the default style is unsightly.It is main to use
In display state(Open state and closed state).
In Android exploitations, we can reach the boundary that makes oneself beautiful by changing the Switch controls appearance of acquiescence
The purpose in face probably has following several method:
1, control property is changed,
2, original Switch classes are inherited in code, redefine onDraw methods,
3, self-defined style patterns.
The android system of highest version is to Switch switch buttons(Switch controls)Beautified, but lowest version
Mobile phone is not supported.This leads to the desired effect disunity that different type of machines is shown.
Invention content
In view of the deficiencies in the prior art, the purpose of the present invention is to provide a kind of realization sides of slide switch button
Method passes through self-defined Switch switch buttons(Switch controls), realize slide switch button, different editions, different type of machines tool
There is the same landscaping effect, mobile animation effect can be increased on demand, reduces the lofty sense of state mutation, increase the body of user
It tests.
To achieve the above objectives, the technical solution adopted by the present invention is that:
A kind of implementation method of slide switch button, which is characterized in that include the following steps:
Step 1, slide switch button class SlideSwitch is defined, the View that android system provides is inherited;
Step 2, definition monitoring and callback interface OnSwitchChangedListener, the monitoring for providing state and readjustment;
Step 3, call parameter is defined, for setting the relevant basic information of slide switch button;
Step 4, Define Brush variable mPaint is the variable of the Paint types of android system, for drawing;
Step 5, defined variable mOnSwitchChangedListener is customized interface, when for external call
State returns;
Step 6, bitmap is initialized, the width and height of background picture are calculated;
Bitmap provides the getResources methods for obtaining Resources objects by android system and obtains;
Default bitmap is ready-made picture resource R.drawable.bg_switch_off, R.drawable.bg_switch_on
And R.drawable.switch_thumb;
The width and height of picture are obtained by getWidth the and getHeight methods of android system Bitmap objects;
Step 7, setOnSwitchChangedListener methods are set, is transmitted through and carrys out the change that interface object is used for listening state
Change,
SetStatus methods are set, for changing the current status information of control;
Step 8, onTouchEvent methods, the event that monitoring users are pressed, moved, lifting, and sliding animation process are rewritten;
Step 9, onDraw methods are rewritten according to different state rendering context figures and text prompt information.
Based on the above technical solution, in step 3, the call parameter includes:
Constant parameter for the state for representing current control, including:
SWITCH_OFF, control are closed, and are the int types of android system,
SWITCH_ON, control are opened, and are the int types of android system,
SWITCH_SCROLING, control sliding, is the int types of android system;
Defined variable mSwitchStatus is the variable of the int types of android system, the shape for marking control current
State;
Definition character string variable mOnText and mOffText, are the variables of the String types of android system, for carrying
Show the current state in space;
Boolean type variable mHasScrolled, default value false are defined, is the change of the boolean types of android system
Amount, rolls for marking whether;
Defined variable mSrcX and mDstX are the variables of the int types of android system, circular intermediate for marking
Beginning x coordinate and end x coordinate;
Defined variable mSwitch_off, mSwitch_on and mSwitch_thumb are the Bitmap classes of android system
The variable of type, for loading background picture when different conditions;
Defined variable mBmpWidth, mBmpHeight and mThumbWidth are the changes of the int types of android system
Amount, the width for calculating background picture and high and intermediate picture width.
Based on the above technical solution, in step 3, the call parameter further comprises:
The text prompt of different conditions, including:OnText and offText is the change for the String types that android system provides
Amount.
Based on the above technical solution, in step 8, the event object event provided by android system, solution
The type of precipitation event and current x, y-coordinate information,
When detecting user's movement, the invalidate methods that calling system View controls provide are used to carry out current control
It redraws.
Based on the above technical solution, in step 9, according to the canvas object canvas that android system provides, lead to
The font size for crossing the setTextSize methods setting paintbrush of android system Paint objects, to be adapted to painting canvas;
Picture is uniformly drawn by customizing method drawBitmap,
Before drawing process, the face of paintbrush is arranged in the setColor methods of object in the Paint provided by android system
Color,
In drawing process, Bitmap is drawn by the drawBitmap methods of android system Canvas objects, it is described
Bitmap is the target bitmap object to be drawn,
In drawing process, text prompt information is drawn on painting canvas by the drawText methods of Canvas objects,
In drawing process, painting canvas is cut by clipRect methods, the part after only display is cut passes through
Translate methods in android system Canvas objects translate painting canvas,
In drawing process, current paintbrush information is preserved by the restoreToCount methods in Canvas objects.
Based on the above technical solution, in step 9, further pass through android system Paint objects
The type that setTypeface methods set font.
Based on the above technical solution, in step 9, AnimationTransRunnable classes is further defined and are realized
Android system provides Runnable interfaces, for realizing mobile animation effect.
Based on the above technical solution, when realizing mobile animation effect, intermediate roundlet is represented by parameter srcX
The position of the x coordinate of shape starting is represented the position of the intermediate small circular x coordinate to be moved to by parameter dstX, passes through parameter
Duration represents the time of animation execution, passes through the side postInvalidate provided in the self-defined View of android system
Method redraws control.
The implementation method of slide switch button of the present invention passes through self-defined Switch switch buttons(Switch is controlled
Part), realize that slide switch button, different editions, different type of machines have the same landscaping effect, mobile animation effect can be increased on demand
Fruit reduces the lofty sense of state mutation, increases the experience of user.
Description of the drawings
The present invention has following attached drawing:
The flow chart of Fig. 1 present invention.
Specific implementation mode
Below in conjunction with attached drawing, invention is further described in detail.
As shown in Figure 1, the implementation method of slide switch button of the present invention, includes the following steps:
Step 1, slide switch button class SlideSwitch is defined, the View that android system provides is inherited;
Step 2, definition monitoring and callback interface OnSwitchChangedListener, the monitoring for providing state and readjustment;
Step 3, call parameter is defined, for setting the relevant basic information of slide switch button;
Step 4, Define Brush variable mPaint is the variable of the Paint types of android system, for drawing;
Step 5, defined variable mOnSwitchChangedListener is customized interface, when for external call
State returns;
Step 6, bitmap is initialized, the width and height of background picture are calculated;
Bitmap provides the getResources methods for obtaining Resources objects by android system and obtains;
Default bitmap is ready-made picture resource R.drawable.bg_switch_off, R.drawable.bg_switch_on
And R.drawable.switch_thumb;
The width and height of picture are obtained by getWidth the and getHeight methods of android system Bitmap objects;
Step 7, setOnSwitchChangedListener methods are set, is transmitted through and carrys out the change that interface object is used for listening state
Change,
SetStatus methods are set, for changing the current status information of control;
Step 8, onTouchEvent methods, the event that monitoring users are pressed, moved, lifting, and sliding animation process are rewritten;
Step 9, onDraw methods are rewritten according to different state rendering context figures and text prompt information.
Based on the above technical solution, in step 3, the call parameter includes:
Constant parameter for the state for representing current control, including:
SWITCH_OFF, control are closed, and are the int types of android system,
SWITCH_ON, control are opened, and are the int types of android system,
SWITCH_SCROLING, control sliding, is the int types of android system;
Defined variable mSwitchStatus is the variable of the int types of android system, the shape for marking control current
State;
Definition character string variable mOnText and mOffText, are the variables of the String types of android system, for carrying
Show the current state in space;
Boolean type variable mHasScrolled, default value false are defined, is the change of the boolean types of android system
Amount, rolls for marking whether;
Defined variable mSrcX and mDstX are the variables of the int types of android system, circular intermediate for marking
Beginning x coordinate and end x coordinate;
Defined variable mSwitch_off, mSwitch_on and mSwitch_thumb are the Bitmap classes of android system
The variable of type, for loading background picture when different conditions;
Defined variable mBmpWidth, mBmpHeight and mThumbWidth are the changes of the int types of android system
Amount, the width for calculating background picture and high and intermediate picture width.
Based on the above technical solution, in step 3, the call parameter further comprises:
The text prompt of different conditions, including:OnText and offText is the change for the String types that android system provides
Amount.
Based on the above technical solution, in step 8, the event object event provided by android system, solution
The type of precipitation event and current x, y-coordinate information,
Such as:The coordinate value of x is obtained by event.getX ();
When detecting user's movement, the invalidate methods that calling system View controls provide are used to carry out current control
It redraws.
Based on the above technical solution, in step 9, according to the canvas object canvas that android system provides, lead to
The font size for crossing the setTextSize methods setting paintbrush of android system Paint objects, to be adapted to painting canvas;
Picture is uniformly drawn by customizing method drawBitmap,
Before drawing process, the face of paintbrush is arranged in the setColor methods of object in the Paint provided by android system
Color,
In drawing process, Bitmap is drawn by the drawBitmap methods of android system Canvas objects, it is described
Bitmap is the target bitmap object to be drawn,
In drawing process, text prompt information is drawn on painting canvas by the drawText methods of Canvas objects,
In drawing process, painting canvas is cut by clipRect methods, the part after only display is cut passes through
Translate methods in android system Canvas objects translate painting canvas,
In drawing process, current paintbrush information is preserved by the restoreToCount methods in Canvas objects.
Based on the above technical solution, in step 9, further pass through android system Paint objects
The type that setTypeface methods set font.
Based on the above technical solution, in step 9, AnimationTransRunnable classes is further defined and are realized
Android system provides Runnable interfaces, for realizing mobile animation effect.
Based on the above technical solution, when realizing mobile animation effect, intermediate roundlet is represented by parameter srcX
The position of the x coordinate of shape starting is represented the position of the intermediate small circular x coordinate to be moved to by parameter dstX, passes through parameter
Duration represents the time of animation execution, passes through the side postInvalidate provided in the self-defined View of android system
Method redraws control.
Such as:Realize that mobile animation effect code is as follows:
public void run()
{
final int patch = (dstX > srcX 5 : -5);
if(duration == 0)
{
SlideSwitch.this.mSwitchStatus = SWITCH_SCROLING;
SlideSwitch.this.postInvalidate();
}
else
{
int x = srcX + patch;
while (Math.abs(x-dstX) > 5)
{
mDstX = x;
SlideSwitch.this.mSwitchStatus = SWITCH_SCROLING;
SlideSwitch.this.postInvalidate();
x+=patch;
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
mDstX = dstX;
SlideSwitch.this.mSwitchStatus = mDstX > 35 SWITCH_ON : SWITCH_OFF;
SlideSwitch.this.postInvalidate();
}
}
The content not being described in detail in this specification belongs to the prior art well known to professional and technical personnel in the field.
Claims (8)
1. a kind of implementation method of slide switch button, which is characterized in that include the following steps:
Step 1, slide switch button class SlideSwitch is defined, the View that android system provides is inherited;
Step 2, definition monitoring and callback interface OnSwitchChangedListener, the monitoring for providing state and readjustment;
Step 3, call parameter is defined, for setting the relevant basic information of slide switch button;
Step 4, Define Brush variable mPaint is the variable of the Paint types of android system, for drawing;
Step 5, defined variable mOnSwitchChangedListener is customized interface, when for external call
State returns;
Step 6, bitmap is initialized, the width and height of background picture are calculated;
Bitmap provides the getResources methods for obtaining Resources objects by android system and obtains;
Default bitmap is ready-made picture resource R.drawable.bg_switch_off, R.drawable.bg_switch_on
And R.drawable.switch_thumb;
The width and height of picture are obtained by getWidth the and getHeight methods of android system Bitmap objects;
Step 7, setOnSwitchChangedListener methods are set, is transmitted through and carrys out the change that interface object is used for listening state
Change,
SetStatus methods are set, for changing the current status information of control;
Step 8, onTouchEvent methods, the event that monitoring users are pressed, moved, lifting, and sliding animation process are rewritten;
Step 9, onDraw methods are rewritten according to different state rendering context figures and text prompt information.
2. the implementation method of slide switch button as described in claim 1, it is characterised in that:In step 3, the call parameter
Including:
Constant parameter for the state for representing current control, including:
SWITCH_OFF, control are closed, and are the int types of android system,
SWITCH_ON, control are opened, and are the int types of android system,
SWITCH_SCROLING, control sliding, is the int types of android system;
Defined variable mSwitchStatus is the variable of the int types of android system, the shape for marking control current
State;
Definition character string variable mOnText and mOffText, are the variables of the String types of android system, for carrying
Show the current state in space;
Boolean type variable mHasScrolled, default value false are defined, is the change of the boolean types of android system
Amount, rolls for marking whether;
Defined variable mSrcX and mDstX are the variables of the int types of android system, circular intermediate for marking
Beginning x coordinate and end x coordinate;
Defined variable mSwitch_off, mSwitch_on and mSwitch_thumb are the Bitmap classes of android system
The variable of type, for loading background picture when different conditions;
Defined variable mBmpWidth, mBmpHeight and mThumbWidth are the changes of the int types of android system
Amount, the width for calculating background picture and high and intermediate picture width.
3. the implementation method of slide switch button as claimed in claim 2, it is characterised in that:In step 3, the call parameter
Further comprise:
The text prompt of different conditions, including:OnText and offText is the change for the String types that android system provides
Amount.
4. the implementation method of slide switch button as described in claim 1, it is characterised in that:In step 8, pass through Android
The event object event that system provides, parses the type of outgoing event and current x, y-coordinate information,
When detecting user's movement, the invalidate methods that calling system View controls provide are used to carry out current control
It redraws.
5. the implementation method of slide switch button as described in claim 1, it is characterised in that:In step 9, according to Android
Paintbrush is arranged by the setTextSize methods of android system Paint objects in the canvas object canvas that system provides
Font size, to be adapted to painting canvas;
Picture is uniformly drawn by customizing method drawBitmap,
Before drawing process, the face of paintbrush is arranged in the setColor methods of object in the Paint provided by android system
Color,
In drawing process, Bitmap is drawn by the drawBitmap methods of android system Canvas objects, it is described
Bitmap is the target bitmap object to be drawn,
In drawing process, text prompt information is drawn on painting canvas by the drawText methods of Canvas objects,
In drawing process, painting canvas is cut by clipRect methods, the part after only display is cut passes through
Translate methods in android system Canvas objects translate painting canvas,
In drawing process, current paintbrush information is preserved by the restoreToCount methods in Canvas objects.
6. the implementation method of slide switch button as claimed in claim 5, it is characterised in that:In step 9, further pass through
The type that the setTypeface methods of android system Paint objects set font.
7. the implementation method of slide switch button as claimed in claim 5, it is characterised in that:In step 9, further define
AnimationTransRunnable classes realize that android system provides Runnable interfaces, are imitated for realizing mobile animation
Fruit.
8. the implementation method of slide switch button as claimed in claim 7, it is characterised in that:Realize mobile animation effect
When, the position of the x coordinate of intermediate small circular starting is represented by parameter srcX, representing intermediate small circular by parameter dstX will move
The position of the x coordinate moved represents the time of animation execution by parameter duration, self-defined by android system
The postInvalidate methods provided in View redraw control.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201810336744.9A CN108647024A (en) | 2018-04-16 | 2018-04-16 | A kind of implementation method of slide switch button |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201810336744.9A CN108647024A (en) | 2018-04-16 | 2018-04-16 | A kind of implementation method of slide switch button |
Publications (1)
Publication Number | Publication Date |
---|---|
CN108647024A true CN108647024A (en) | 2018-10-12 |
Family
ID=63746477
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201810336744.9A Pending CN108647024A (en) | 2018-04-16 | 2018-04-16 | A kind of implementation method of slide switch button |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN108647024A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111240655A (en) * | 2020-01-07 | 2020-06-05 | 红有软件股份有限公司 | Method for beautifying WinForm component based on GDI/GDI + |
CN111381757A (en) * | 2020-03-11 | 2020-07-07 | 上海索辰信息科技有限公司 | Timing diagram activity callback processing system and method |
Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN105094847A (en) * | 2015-08-24 | 2015-11-25 | 江西好帮手电子科技有限公司 | Multi-graphic-layer customizable button control implementation method and system based on Android system |
CN107766038A (en) * | 2017-10-24 | 2018-03-06 | 四川长虹电器股份有限公司 | It is a kind of to carry out the method that profile is cut out and beautified to UI controls based on android system |
-
2018
- 2018-04-16 CN CN201810336744.9A patent/CN108647024A/en active Pending
Patent Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN105094847A (en) * | 2015-08-24 | 2015-11-25 | 江西好帮手电子科技有限公司 | Multi-graphic-layer customizable button control implementation method and system based on Android system |
CN107766038A (en) * | 2017-10-24 | 2018-03-06 | 四川长虹电器股份有限公司 | It is a kind of to carry out the method that profile is cut out and beautified to UI controls based on android system |
Non-Patent Citations (1)
Title |
---|
从此醉: ""android自定义开关控件-SlideSwitch"", 《HTTPS://BLOG.CSDN.NET/GAOTONG2055/ARTICLE/DETAILS/84455893》 * |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111240655A (en) * | 2020-01-07 | 2020-06-05 | 红有软件股份有限公司 | Method for beautifying WinForm component based on GDI/GDI + |
CN111240655B (en) * | 2020-01-07 | 2023-11-07 | 红有软件股份有限公司 | Method for beautifying WinForm component based on GDI/GDI + |
CN111381757A (en) * | 2020-03-11 | 2020-07-07 | 上海索辰信息科技有限公司 | Timing diagram activity callback processing system and method |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN101446883B (en) | Method for controlling mobile equipment by touch screen and mobile equipment | |
CN107636593A (en) | For equipment, method and the graphic user interface for providing virtual drawing Aids He interacting | |
CA2335127A1 (en) | System and method for the visual customization of business object interfaces | |
CN106201173B (en) | A kind of interaction control method and system of user's interactive icons based on projection | |
CN108647024A (en) | A kind of implementation method of slide switch button | |
CN107220047B (en) | Method for realizing progress bar control piece of dynamic image display of audio frequency spectrum | |
CN109064388A (en) | Facial image effect generation method, device and electronic equipment | |
CN106681503A (en) | Display control method, terminal and display device | |
CN107621919A (en) | A kind of interactive approach and user terminal for cultivating behavioural habits | |
CN105072264A (en) | Intelligent watch control method and system based on tri-axis accelerometer | |
CN105487667A (en) | Electronic equipment control method and device | |
CN106502510A (en) | The adaptation processing method at icon and interface, device | |
CN109064529A (en) | A kind of animation algorithms of button dynamic flashing | |
WO2019057096A1 (en) | Method for sub-area control of full-touch-screen smart watch | |
CN103838487B (en) | A kind of information processing method and electronic equipment | |
CN106354376A (en) | Information processing method and client terminal | |
CN104077114B (en) | A kind of method and electronic equipment of display interface that replacing application software | |
CN104049845B (en) | A kind of information processing method and electronic equipment | |
CN108235104B (en) | Live video floating window playing method for mobile terminal | |
CN106873763B (en) | Virtual reality equipment and information input method thereof | |
CN104267885B (en) | The display control method and electronic equipment of the user interface of application program | |
CN105988762B (en) | Display methods, hidden method and the device of animation frame | |
CN109343783A (en) | It is a kind of to facilitate the input method and device for changing text attribute | |
CN111676658B (en) | Interaction method of interaction interface of clothes treatment equipment | |
CN109117223B (en) | Interactive algorithm for submitting request state prompt |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
RJ01 | Rejection of invention patent application after publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20181012 |