Android Basics Getting Started tutorial--8.1.2 Android 13 drawable Summary Part 2

Source: Internet
Author: User

Android Basics Getting Started tutorial--8.1.2 Android 13 drawable Summary Part 2

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

In this section we continue to learn about drawable resources in Android, which we learned in the previous section:
colordrawable;ninepatchdrawable;
shapedrawable,gradientdrawable! These four drawable~
In this section, we continue to learn about the next five drawable, namely:
bitmapdrawable;insertdrawable;clipdrawable;
rotatedrawable,animationdrawable!
Or put down 13 kinds of drawable map:

OK, start this section ~

1.BitmapDrawable

An encapsulation of bitmap that sets the way in which it wraps the bitmap in the Bitmapdrawable area, with:
Tile fill, stretch fill or keep the original size of the picture! <bitmap> as the root node!
The optional attributes are as follows:

  • src: Image Resources ~
  • AntiAlias: Whether anti-aliasing is supported
  • filter: If bitmap filtering is supported, it can be more smooth when the image is criticized.
  • dither: Whether to dither the bitmap
  • Gravity: If the bitmap is smaller than the container, you can set the relative position of the bitmap in the container
  • Tilemode: Specifies that the picture tile fills the container's mode, setting this, the Gravity property is ignored and has the following optional values:
    Disabled (Whole pattern damper),clamp(original size),
    Repeat (tiled),mirror(mirrored tile)

Corresponds to:

①xml definition bitmapdrawable:

<?xml version="1.0" encoding="utf-8"?>  <bitmap xmlns:android="http://schemas.android.com/apk/res/android"      android:dither="true"      android:src="@drawable/ic_launcher"      android:tileMode="mirror" />  

② Java code that implements the same effect :

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

means to embed a drawable inside another drawable, and to leave some space inside,
Similar to the Padding property of drawable, but padding represents the margin of drawable content with drawable itself !
While insetdrawable represents the margin between the two drawable and the container , when the control needs a * * background that is more than the actual border
Small time * *, more suitable to use insetdrawable, such as using this can solve our custom dialog and screen
A spacing problem, I believe that friends who have done know, even if we set up layout_margin words is useless, this
It's time to use this insetdrawable! Simply set a insetxxx setting for insetdrawable to be different
The direction of the margin, and then set the background to dialog!

The relevant properties are as follows:

  • 1.drawable: The referenced drawable, if empty, must have a drawable type of child node!
  • 2.visible: Set drawable whether the amount of space
  • 3.insetleft,insetright,insettop,Insetbottm: Set the left and right margins

used in ①xml :

<?xml version="1.0" encoding="utf-8"?>  <inset xmlns:android="http://schemas.android.com/apk/res/android"      android:drawable="@drawable/test1"      android:insetBottom="10dp"      android:insetLeft="10dp"      android:insetRight="10dp"      android:insetTop="10dp" />  

used in Java code :

InsetDrawable insetDrawable = new InsetDrawable(getResources()          .getDrawable(R.drawable.test10101010);  

Use :

3.ClipDrawable

Clip can be translated into the meaning of scissors, we can think of clipdrawable as a bitmap to cut a part;
The progress bar in Android is implemented using Clipdrawable, which determines the cut based on the value of the set level.
The size of the area, the root node is <clip>

The relevant properties are as follows :

  • cliporietntion: Set the direction of clipping, you can set the horizontal and vertical 2 directions
  • Gravity: Start cropping from that position
  • drawable: Referenced drawable resource, empty if required to have a drawable type of child node
    PS: This drawable type of child node: is in
<?xml version="1.0" encoding="utf-8"?><clip xmlns:android="http://schemas.android.com/apk/res/android"    android:clipOrientation="horizontal"    android:drawable="@mipmap/ic_bg_meizi"    android:gravity="left" />

② set a imageview in the Activity_main main layout file, set src to clipdrawable!
Remember it is src Oh, if you write a blackground, but you will report a NULL pointer OH!!!!

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Xmlns:tools="Http://schemas.android.com/tools"android:layout_width="Match_parent"  android:layout_height="Match_parent"android:orientation="Vertical" >                    <ImageViewandroid:id="@+id/img_show"android:layout_width="Match _parent "android:layout_height=" Match_parent "android:src=" @drawable/clip_bg " />                                </linearlayout>  

③mainactivity.java the size of the interception area via Setlevel :

 Public  class mainactivity extends appcompatactivity {    PrivateImageView img_show;PrivateClipdrawable cd;PrivateHandler Handler =NewHandler () {@Override         Public void Handlemessage(Message msg) {if(Msg.what = =0x123) {Cd.setlevel (Cd.getlevel () + -); }        }    };@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Img_show = (ImageView) Findviewbyid (r.id.img_show);//CORE Implementation Codecd = (clipdrawable) img_show.getdrawable ();FinalTimer timer =NewTimer (); Timer.schedule (NewTimerTask () {@Override             Public void Run() {Handler.sendemptymessage (0x123);if(Cd.getlevel () >=10000) {timer.cancel (); }            }        },0, -); }}

OK, a little meaning, sister Tubie asked me to take, Baidu on a heap ha ~

4.RotateDrawable

Used to rotate the drawable, but also by setlevel to control the rotation, the maximum value is also: 10000

The relevant properties are as follows :

  • fromdegrees: The starting angle, which corresponds to the lowest level value, defaults to 0
  • todegrees: End angle, corresponding to the highest level value, default 360
  • pivotx: Sets the x-coordinate of the reference point, the value is 0~1, the default is 50%, that is, 0.5
  • Pivoty: Sets the y-coordinate of the reference point, the value is 0~1, the default is 50%, which is 0.5
    PS: If the rotating picture display is not complete, you can modify the above two values to solve!
  • drawable: Setting Bitmap resources
  • Visible: Set drawable is visible!

The angle chart is as follows :

Examples of Use :

Run :

Code Implementation :

Make a little change on the 3rd clipdrawable!

① defines a rotatedrawable resource file :

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/ic_launcher"    android:fromDegrees="-180"    android:pivotX="50%"    android:pivotY="50%" />  

②activity_main.xml in the modified SRC point to the above drawable can, mainactivity only need to clipdrawable
Change to Rotatedrawable!

 Public  class mainactivity extends appcompatactivity {    PrivateImageView img_show;PrivateRotatedrawable cd;PrivateHandler Handler =NewHandler () {@Override         Public void Handlemessage(Message msg) {if(Msg.what = =0x123) {if(Cd.getlevel () >=10000) Toast.maketext (mainactivity. This,"Turn around.", Toast.length_long). Show (); Cd.setlevel (Cd.getlevel () + -); }        }    };@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Img_show = (ImageView) Findviewbyid (r.id.img_show);//CORE Implementation Codecd = (rotatedrawable) img_show.getdrawable ();FinalTimer timer =NewTimer (); Timer.schedule (NewTimerTask () {@Override             Public void Run() {Handler.sendemptymessage (0x123);if(Cd.getlevel () >=10000) {timer.cancel (); }            }        },0, -); }}
5.AnimationDrawable

The last drawable,animationdrawable in this section is used to achieve frame animation in Android, which is to put a series of
Drawable, play in a frame at a certain order; animations in Android are rich, with traditional motion tweens, panning,
Zoom and so on, but here we just introduce this animationdrawable implementation frame animation, about Alpha,scale,
Translate,rotate and so on, follow-up in the animation section of the detailed introduction ~
We use <animation-list> as the root node here

Related Property methods :

OneShot: Sets whether to loop playback, FALSE for looping!!!
duration: Frame interval time, usually we set to 300 milliseconds
After we get the aniamtiondrawable instance, we need to call its start () method to play the animation, in addition to note
In the OnCreate () method, it has no effect, because the view is not yet initialized, we can
Delay the animation with a simple handler! Of course there are other ways to see the following links:
Several ways to run Android animationdrawable
The use of animationdrawable to achieve frame animation is really very convenient ~

Examples of Use :

Run :

Code Implementation :

First defines a animationdrawable XML resource file :

<?xml version= "1.0" encoding= "Utf-8"?><animation-list xmlns:android="Http://schemas.android.com/apk/res/android"  android:oneshot="false">        <item        android:drawable="@mipmap/ic_pull_to_refresh_loading01"        android: Duration=" /> "    <itemandroid:drawable="@mipmap/ic_pull_to_refresh_loading02"android: Duration=" />                 "    <itemandroid:drawable="@mipmap/ic_pull_to_refresh_loading03"  Android:duration=" />                 "    <item        android:drawable="@mipmap/ic_pull_to_refresh_loading04"        android: Duration=" /> "    <itemandroid:drawable="@mipmap/ic_pull_to_refresh_loading05"android: Duration=" />                 "    <itemandroid:drawable="@mipmap/ic_pull_to_refresh_loading06"  Android:duration=" />                 "</animation-list>  

②activity_main.xml settings under SRC, and then mainactivity in :

 Public  class mainactivity extends appcompatactivity {    PrivateImageView img_show;PrivateAnimationdrawable 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 CodeAD = (animationdrawable) img_show.getdrawable (); Handler Handler =NewHandler (); Handler.postdelayed (NewRunnable () {@Override             Public void Run() {Ad.start (); }        }, -); }}

Hey, super simple have wood, and later in some need to use the frame animation place, directly on the animationdrawable,
Of course, it is only suitable for frame animations that do not need to be controlled, such as this is the progress bar material in the case of a super table drop-down refresh.
A simple frame animation made of! Self-development According to your own needs

This section summarizes:


This section also introduces the other five drawable, which is interesting, and quickly apply them to your actual development.
Hehe, say so much, thank you! Also just have a private messages I said the previous article do not delete line no, well, here say
Just delete some duplicates, like the ones that are similar to this one, and of course I'll back it up! Deleted articles will be backed up by ~
So rest assured!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--8.1.2 Android 13 drawable Summary Part 2

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.