Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android自定義滑動接聽電話控件組實例

Android自定義滑動接聽電話控件組實例

編輯:關於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); 
    } 
  } 
}); 

問題:圖片大小以及屏幕適配可能會有些問題,如果使用,請注意測試。以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved