Basic Android tutorial -- 13 Drawable types in Android 8.1.2 summary Part 2

Source: Internet
Author: User

Basic Android tutorial -- 13 Drawable types in Android 8.1.2 summary Part 2
This section introduces:

In this section, we continue to learn about Drawable resources in Android. In the previous section, we learned:
ColorDrawable;NinePatchDrawable;
ShapeDrawable;GradientDrawable! The four Drawable ~
In this section, we will continue to learn the next five Drawable, which are:
BitmapDrawable;InsertDrawable;ClipDrawable;
RotateDrawable;AnimationDrawable!
Paste the following 13 Drawable export charts:

Okay. Start this section ~ <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4nc1_vymxvy2txdw90zt4ncjxociavpg0kpggyiglkpq = "1 bitmapdrawable"> 1. BitmapDrawable

For a Bitmap encapsulation, you can set the bitmap painting method in the BitmapDrawable area, which includes:
Tile filling, stretch filling or keep the original image size! To <Bitmap> It is the root node!
The optional attributes are as follows:

Src: Image resources ~ Antialias: Whether anti-aliasing is supported Filter: Whether bitmap filtering is supported. If yes, it can be smooth when the image is criticized and displayed. Dither: Whether to perform jitter processing on bitmap Gravity: If the bitmap is smaller than the container, you can set the relative position of the bitmap in the container. TileMode: Specifies the image tiled filling container mode. If this mode is set, the gravity attribute is ignored. The following optional values are available:
Disabled(The entire image is stretched and tiled ), Clamp(Source image size ),
Repeat(Tiled ), Mirror(Image tiled)

Corresponding:

① XML defines BitmapDrawable:


     
     
   

② Implement Java code with the same effect:

BitmapDrawable bitDrawable = new BitmapDrawable(bitmap);  bitDrawable.setDither(true);  bitDrawable.setTileModeXY(TileMode.MIRROR,TileMode.MIRROR);  
2. InsetDrawable

It means to embed a Drawable into another Drawable and leave some spacing inside it,
Similar to the padding attribute of DrawablePaddingIndicatesThe content of Drawable and the margin of Drawable itself!
WhileInsetDrawableIndicatesMargins between two Drawable and container, When the control requires ** background ratio to the actual border
When it is small **, it is more suitable to use InsetDrawable. For example, using this can solve the problem between our custom Dialog and the screen.
I believe all the friends who have done this know that even if we set layout_margin, this is useless.
You can use this InsetDrawable! You only need to set an insetXxx for InsetDrawable.
Margin of the direction, and then set it to the background of the Dialog!

The related attributes are as follows:

1. Drawable: The referenced Drawable. If it is null, there must be a Drawable subnode! 2. Visible: Set whether Drawable has enough space. 3. InsetLeft, InsetRight, InsetTop, InsetBottm: Set the padding between the left and right sides.

① Use in XML:


     
     
   

Use in Java code:

InsetDrawable insetDrawable = new InsetDrawable(getResources()          .getDrawable(R.drawable.test1), 10, 10, 10, 10);  

Use:

3. ClipDrawable

Clip can be translated into scissors. We can understand ClipDrawable as cutting the next part from a bitmap;
The progress bar in Android is implemented using ClipDrawable. It is cut Based on the set level value.
The size of the region. The root node is <Clip>

The related attributes are as follows::

ClipOrietntion: Sets the direction of the cut. You can set two horizontal and vertical directions. Gravity: Crop from that position Drawable: The referenced drawable resource. If it is null, a child node of the Drawable type is required.
Ps: The child node of the Drawable type is in

   
    
   

② Set an ImageView in the activity_main main layout file and set src to clipDrawable!
Remember src. If you write it as blackground, a null pointer will be reported !!!!


       
    
     

③ MainActivity. java uses setLevel to set the size of the intercepted Area:

Public class MainActivity extends AppCompatActivity {private ImageView img_show; private ClipDrawable cd; private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {if (msg. what = 0x123) {cd. setLevel (cd. getLevel () + 500) ;}};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); img_show = (ImageView) findViewById (R. id. img_show); // core implementation code cd = (ClipDrawable) img_show.getDrawable (); final Timer timer = new Timer (); timer. schedule (new TimerTask () {@ Override public void run () {handler. sendEmptyMessage (0x123); if (cd. getLevel ()> = 10000) {timer. cancel () ;}}, 0,300 );}}

Okay, it's a bit interesting. Don't ask me to take it. Baidu has a bunch of pictures ~

4. RotateDrawable

It is used to rotate Drawable, and setLevel is used to control the rotation. The maximum value is 10000.

The related attributes are as follows::

FromDegrees: Start angle, which corresponds to the lowest level value. The default value is 0. ToDegrees: End angle, which corresponds to the highest level value. The default value is 360. PivotX: Set the x coordinate of the reference point. The value ranges from 0 ~ 1. The default value is 50%, that is, 0.5. Ty: Set the Y coordinate of the reference point. The value ranges from 0 ~ 1. The default value is 50%, that is, 0.5.
Ps: If the display of the rotated image is incomplete, you can modify the above two values! Drawable: Set bitmap Resources Visible: Set whether drawable is visible!

The angle chart is as follows::

Example:

Run:

Code Implementation:

Make a few changes to clipDrawable in the third point!

① Define a rotateDrawable resource file:


   
     
   

② In activity_main.xml, modify src to point to the above drawable. MainActivity only needs to set ClipDrawable
Change to rotateDrawable!

Public class MainActivity extends AppCompatActivity {private ImageView img_show; private RotateDrawable cd; private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {if (msg. what = 0x123) {if (cd. getLevel ()> = 10000) Toast. makeText (MainActivity. this is complete ~, Toast. LENGTH_LONG ). show (); cd. setLevel (cd. getLevel () + 400) ;}};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); img_show = (ImageView) findViewById (R. id. img_show); // core implementation code cd = (RotateDrawable) img_show.getDrawable (); final Timer timer = new Timer (); timer. schedule (new TimerTask () {@ Override public void run () {handler. sendEmptyMessage (0x123); if (cd. getLevel ()> = 10000) {timer. cancel () ;}}, 0,100 );}}
5. AnimationDrawable

In this section, the last Drawable and AnimationDrawable are used to implement Frame Animation in Android.
Drawable, which can be played at a certain order by frame. In Android, there are a wide range of animations, including traditional supplementary animation and translation,
Scaling and other effects, but here we only introduce this AnimationDrawable to implement frame animation, about alpha, scale,
Translate, rotate, etc. We will introduce it in detail later in the animation chapter ~
Here we use <Animation-list> As the root node

Related Property Methods:

Oneshot: Sets whether to play cyclically. false indicates loop playback !!!
Duration: Frame interval, which is usually set to 300 ms
After obtaining the AniamtionDrawable instance, you must call its start () method to play the animation.
Calling the OnCreate () method has no effect, because the View has not been initialized, we can
Use a simple handler to delay playing the animation! Of course there are other ways to see the following links:
Android AnimationDrawable
It is really convenient to use AnimationDrawable to implement frame animation ~

Example:

Run:

Code Implementation:

First define an xml resource file for the AnimationDrawable:


       
       
        
         
          
        
        
       
      
     
    
     

② Set src in activity_main.xml and then in MainActivity:

Public class MainActivity extends AppCompatActivity {private ImageView img_show; private AnimationDrawable ad; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); img_show = (ImageView) findViewById (R. id. img_show); // core implementation code ad = (AnimationDrawable) img_show.getDrawable (); Handler handler = new Handler (); handler. postDelayed (new Runnable () {@ Override public void run () {ad. start () ;}}, 300 );}}

Hey hey, it's super simple. You can use Frame Animation directly in the future.AnimationDrawable,
Of course, it is only suitable for Frame Animation that does not require control. For example, the above is the progress bar material during the over-Table pull-down refresh.
A simple Frame Animation! Expand based on your needs ~

 

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.