欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义滑动接听电话控件组实例

程序员文章站 2024-03-03 21:44:10
本文根据组件开发思想,首先介绍android自定义控件,然后将自定义的控件封装为jar包。最为实现滑动接听电话控件组。 一、目录结构 二、运行效果...

本文根据组件开发思想,首先介绍android自定义控件,然后将自定义的控件封装为jar包。最为实现滑动接听电话控件组。

一、目录结构

Android自定义滑动接听电话控件组实例

二、运行效果

Android自定义滑动接听电话控件组实例

三、代码实现

首先,自定义一个类incomingphone继承relativelayout

public incomingphone(context 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() { 
 
        mincomingphoneresultlistener.incomingphoneresultevent("hangup"); 
      } 
    }); 
    pickupview.setontouchlistener(new ontouchlistener() { 
      @override 
      public boolean ontouch(view v, motionevent event) { 
        switch (event.getaction()) { 
          case motionevent.action_down: 
            hangupview.setvisibility(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 event) { 
        switch (event.getaction()) { 
          case motionevent.action_down: 
            pickupview.setvisibility(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); 
  } 

构造函数中添加子控件,并添加滑动事件以及监听器,其中pickupview、hangupview是自定义的子控件,是该类的内部类两个几乎一样,我贴出其中一个

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(context context) { 
 
    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",500).setduration(5000).start(); 
        break; 
      case motionevent.action_move: 
        lastx = (int) event.getx(); 
        setmwidth(lastx); 
//        log.e("起始坐标", string.valueof(firstx)); 
//        log.e("结束坐标", 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(); 
  } 
} 

在xml文件中定义

<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="挂断"/> 
  </relativelayout> 

   在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("", "打电话"); 
      incomingphone.setvisibility(view.gone); 
      mbtnhangup.setvisibility(view.visible); 
    } 
    else { 
      log.e("","挂电话"); 
      incomingphone.setvisibility(view.gone); 
      mbtnhangup.setvisibility(view.visible); 
    } 
  } 
}); 

问题:图片大小以及屏幕适配可能会有些问题,如果使用,请注意测试。以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。