Android Custom View---combination view

Source: Internet
Author: User

Recently the work is relatively easy, nothing to do, so into the high-yielding model (er .... High yield like XX).

Should be a lot of children's shoes on the custom view this thing is more inconsistent, may be to listen to the online view is more difficult, in fact, the custom view is not difficult

Custom view is divided into three types

1. Self-portrait view

2. Combination control view

3. Rewrite the system view

Today we're going to talk about a small example of a combined control view in a custom view, the so-called combination control view is combined into a new view using the System preset view. Does not draw operations on graphics. Well, today's goal is to use the Animation implementation of the loading animation into a view, if you do not know Animation animation can go to open this blog: animation introduction--animation to achieve loading animation effect

The usual, first:


Can see the effect is similar to the previous blog, but now he is a view. Let's look at the layout file first:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "android:layout_height=" Match_parent ">< Com.wingsoft.loadinganimation.LoadingView    android:id= "@+id/loading"    android:layout_width= "300DP    " Android:layout_centerinparent= "true"    android:layout_height= "300DP" ></ com.wingsoft.loadinganimation.loadingview>    <button        android:id= "@+id/button_cancel"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Cancel"/></ Relativelayout>

This is the main layout file, a relative layout, a button. Seeing that one of the controls is a package name means that the control is our custom control.

Since we are introducing the control combination view today, we have created a new class to inherit from Framelayout

public class Loadingview extends Framelayout {private ImageView mimageview;    Private TextView Mtextview;    Private Animationset Mimageani = new Animationset (true);    Private Animationset Mtextani = new Animationset (true);        Public Loadingview (context context, AttributeSet Attrs) {Super (context, attrs);        Layoutinflater.from (context). Inflate (R.layout.loading_view,this);        Mimageani = new Animationset (true);        Mtextani = new Animationset (true);        Mimageview = (ImageView) Findviewbyid (r.id.loadingview_point);        Mtextview = (TextView) Findviewbyid (r.id.loadingview_loading);        Translateanimation ta = new translateanimation (100, 0, 200, 0);        Ta.setduration (5000);        Rotateanimation ra = new Rotateanimation (0, Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);        Ra.setduration (5000);        Mimageani.addanimation (TA);        Mimageani.addanimation (RA); Scaleanimation sa = new Scaleanimation (0, 1, 0, 1, Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);        Sa.setduration (5000);        Alphaanimation AA = new Alphaanimation (0, 1);        Aa.setduration (5000);        Mtextani.addanimation (SA);        Mtextani.addanimation (AA); Aa.setanimationlistener (New Animation.animationlistener () {@Override public void Onanimationstart (A                Nimation animation) {} @Override public void Onanimationend (animation animation) {                Mtextview.startanimation (Mtextani);            Mimageview.startanimation (Mimageani);    } @Override public void Onanimationrepeat (Animation Animation) {}});        } public void Start () {mtextview.startanimation (Mtextani);    Mimageview.startanimation (Mimageani);        } public void Stop () {mtextview.clearanimation ();    Mimageview.clearanimation (); }}

The key is to implement a two-parameter constructor. Call Layoutinflater in the constructor to convert the layout file to view, then add two methods to our view one is start (), and one is stop () to control whether the animation plays. This completes the view's writing. Let's do the following in Mainactivity.

public class Mainactivity extends Actionbaractivity {    private loadingview mloadingview;    Private Button Mbutton;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Mloadingview = (Loadingview) Findviewbyid (r.id.loading);        Mbutton = (Button) Findviewbyid (r.id.button_cancel);        Mloadingview.setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick (view view) {                Mloadingview.start ();            }        });        Mbutton.setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick (view view) {                Mloadingview.stop ();}}        );}    }

As you can see, we can use the view as if it were a system control, what a magical thing it is. You should try it too! We will then introduce a self-drawing view.

Source code Download


Copyright NOTICE: This article is for bloggers original article, welcome to indicate the source after reprint.

Android Custom View---combination view

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.