Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android自定义滑动接听电话控件组

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

作者:gisdream

这篇文章主要介绍了Android自定义滑动接听电话控件组,接听电话可以左右滑动,感兴趣的小伙伴们可以参考一下。

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

一、目录结构

二、运行效果

三、代码实现

首先,自定义一个类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); 
    } 
  } 
}); 

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

您可能感兴趣的文章:
阅读全文