Android custom Sliding answering phone control group instance _android

Source: Internet
Author: User

Based on the idea of component development, this article first introduces the Android custom control and then encapsulates the custom control as a jar package. The most implementation of the sliding answering phone control group.

First, directory structure

Second, the Operation effect

Third, code implementation

First, customize a class Incomingphone inheritance Relativelayout

Public Incomingphone (context, AttributeSet attrs) {Super (context, attrs); 
    Mcontext = context; 
    TextView TextView = new TextView (mcontext); 
    Textview.settext ("Caonima"); 
    Pickupview = new Pickupview (mcontext); 
    Hangupview = new Hangupview (mcontext); 
    Pickupview.setbackground (Getresources (). getdrawable (R.drawable.pick_up_background)); 
    Hangupview.setbackground (Getresources (). getdrawable (R.drawable.hang_up_background)); Relativelayout.layoutparams LP1 = new Relativelayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, 
    ViewGroup.LayoutParams.WRAP_CONTENT); 
Lp1.addrule (Relativelayout.align_parent_right, relativelayout.true); 
    /Pickupview.setbackground (Mcontext.getdrawable (R.drawable.pick_up_background)); 
        Pickupview.setpickuplistener (New Pickupview.pickuplistener () {@Override public void pickupevent () { 
      Mincomingphoneresultlistener.incomingphoneresultevent ("PICKUP"); 
    } 
    }); hangupview.seThanguplistener (New Hangupview.hanguplistener () {@Override public void hangupevent () {mincoming 
      Phoneresultlistener.incomingphoneresultevent ("Hangup"); 
    } 
    }); Pickupview.setontouchlistener (New Ontouchlistener () {@Override public boolean ontouch (View V, motionevent E Vent) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:hangUpView.setVisibili 
            Ty (View.gone); 
          Break 
            Case MotionEvent.ACTION_UP:hangUpView.setVisibility (view.visible); 
            Pickupview.setvisibility (view.visible); 
          Break 
        Default:break; 
      return false; 
    } 
    }); Hangupview.setontouchlistener (New Ontouchlistener () {@Override public boolean ontouch (View V, motionevent E Vent) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:pickUpView.setVisibili Ty (View.gone); 
            Break 
            Case MotionEvent.ACTION_UP:hangUpView.setVisibility (view.visible); 
            Pickupview.setvisibility (view.visible); 
          Break 
        Default:break; 
      return false; 
    } 
    }); 
    AddView (TextView); 
    AddView (Pickupview); 
  AddView (HANGUPVIEW,LP1); 
 }

The

Constructor adds a child control and adds a sliding event as well as a listener, where Pickupview, Hangupview is a custom child control, is the inner class of the class two almost the same, I posted one of the

Class Pickupview extends Relativelayout {private context mcontext; 
  Private final int minwidth = 360; 
  private int screenwidth; 
  private int mwidth; 
  private int mheight; 
 
  Private Pickuplistener Mpickuplistener; 
  public void Setpickuplistener (Pickuplistener mpickuplistener) {this.mpickuplistener = Mpickuplistener; 
    public int getmwidth () {mwidth = Getlayoutparams (). width; 
  return mwidth; 
    public void setmwidth (int width) {mwidth = width; 
    Getlayoutparams (). width = mwidth; 
  Requestlayout (); 
    public int getmheight () {mheight = Getlayoutparams (). Height; 
  return mheight; 
    public void setmheight (int height) {mheight = height; 
    Getlayoutparams (). height = mheight; 
  Requestlayout (); 
    Public Pickupview {Super (context); 
 
    WindowManager wm = (WindowManager) getcontext (). Getsystemservice (Context.window_service); ScreenWidth = Wm.getdefaultDisplay (). GetWidth (); 
    @Override public boolean ontouchevent (Motionevent event) {int firstx = 0; 
    int lastx = 0; 
    Mwidth = Getmwidth (); 
Switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:firstX = (int) event.getx (); 
        Objectanimator.ofint (This, "Mwidth"). Setduration (5000). Start (); 
      Break 
        Case MotionEvent.ACTION_MOVE:lastX = (int) event.getx (); 
Setmwidth (LASTX); 
LOG.E ("Starting coordinates", string.valueof (FIRSTX)); 
        LOG.E ("End coordinates", string.valueof (LASTX)); 
      Break 
        Case MotionEvent.ACTION_UP:lastX = (int) event.getrawx (); 
        if (Lastx > SCREENWIDTH/7 * 6) {mpickuplistener.pickupevent (); 
        else {setmwidth (minwidth); 
      } break; 
    Default:break; 
  return true; 
  Public interface Pickuplistener {public void pickupevent (); 
 } 
}

Define in XML file

<relativelayout 
    android:layout_width= "match_parent" 
    android:layout_height= "wrap_content" > 
    <com.example.administrator.pickuptest.incomingphone 
      android:id= "@+id/incoming" 
      android:layout_width = "Match_parent" 
      android:layout_height= "80DP" > 
    </ com.example.administrator.pickuptest.incomingphone> 
    <button 
      android:id= "@+id/btn_hang_up" 
      Android:layout_margin= "10DP" 
      android:background= "#ff0000" 
      android:textcolor= "#ffffff" 
      android: Visibility= "Gone" 
      android:layout_width= "match_parent" 
      android:layout_height= "80DP" 
      android:text = "Hang Up"/> 
  </RelativeLayout> 

Use in activity

Incomingphone = (incomingphone) Findviewbyid (r.id.incoming); 
mbtnhangup= (Button) Findviewbyid (r.id.btn_hang_up); 
Incomingphone.setincomingphoneresultlistener (New Incomingphone.incomingphoneresultlistener () { 
  @Override Public 
  void Incomingphoneresultevent (String result) { 
    if (' PICKUP '. Equals (Result)) { 
      log.e ("", "call"); 
      Incomingphone.setvisibility (view.gone); 
      Mbtnhangup.setvisibility (view.visible); 
    } 
    else { 
      log.e ("", "hang Phone"); 
      Incomingphone.setvisibility (view.gone); 
      Mbtnhangup.setvisibility (view.visible); 
    } 
  } 
); 

Problem: Picture size and screen adaptation may have some problems, if used, please note the test. The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.