本文介绍了滑块按钮接受Android的电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发自己的接受和拒绝按钮来电。以prevent调用意外回答或采取电话了,我想使一个滑盖造型按钮或类似的东西的口袋时,被拒绝。我,接受呼叫不仅仅是轻触接受按钮。它会更喜欢由左到右(或相反)滑动手指和释放按钮获得与时刻更宽。就像Android的一样。

I want to develop my own Accept and Decline buttons for an incoming call. To prevent the call to be accidentally answered or rejected when taking the phone out of the pocket I would like to make a slider style button or something similar. I am, to accept the call is not just to tap on the Accept button. It would be more like sliding the finger from left to right (or opposite) and let the button get wider with the moment. Just like Android does.

有什么办法,使这个?任何提示吗?

Is there any way to make this? Any hint?

我希望是明确的。

推荐答案

如何创建图像并将其向右滑动(或左),然后发送事件的活动,或者你想处理结果的看法?

How about create an image and slide it to the right (or left) and then send the event to an Activity or any view that you wanna handle the result?

对于这一点,你可以创建一个实现 OnTouchListener 自定义视图:

For this, you can created a custom view which implements OnTouchListener :

public class ImageTouchSlider extends RelativeLayout implements View.OnTouchListener {

private Context mContext;

private ImageView mImage;
private int mScreenWidthInPixel;
private int mScreenWidthInDp;
private float mDensity;

private int mPaddingInDp = 15;
private int mPaddingInPixel;

private int mLengthOfSlider;

public interface OnImageSliderChangedListener{
    void onChanged();
}

private OnImageSliderChangedListener mOnImageSliderChangedListener;

public ImageTouchSlider(Context context) {
    super(context);
    mContext = context;
    createView();
}

public ImageTouchSlider(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    createView();
}

public ImageTouchSlider(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
    createView();
}

public void createView() {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.image_touch_slider, this, true);

    mImage = (ImageView) findViewById(R.id.slider_image);
    mImage.setOnTouchListener(this);

    WindowManager manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics ();
    display.getMetrics(outMetrics);

    mDensity  = getResources().getDisplayMetrics().density;
    float dpWidth  = outMetrics.widthPixels / mDensity;
    mScreenWidthInPixel = outMetrics.widthPixels;
    mScreenWidthInDp = (int) (mScreenWidthInPixel / mDensity);

    mLengthOfSlider = (int) (mScreenWidthInDp - mPaddingInDp*2);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    LayoutParams layoutParams = (LayoutParams) v.getLayoutParams();
    int width = v.getWidth();
    float xPos = event.getRawX();

    switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // You can add some clicked reaction here.
        break;
    case MotionEvent.ACTION_MOVE:
        if(xPos < (mScreenWidthInPixel - width - mPaddingInDp*mDensity) && xPos > mPaddingInDp*mDensity) {
            mOnImageSliderChangedListener.onChanged();
            layoutParams.leftMargin = (int) xPos - width / 2;
            mImage.setLayoutParams(layoutParams);
        }
        break;
    case MotionEvent.ACTION_UP:
        break;
    default:
        break;
    }

    return true;
}

public void setOnImageSliderChangedListener(OnImageSliderChangedListener listener) {
    mOnImageSliderChangedListener = listener;
}

} //end of class

image_touch_sli​​der.xml布局:

image_touch_slider.xml layout :

<?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"
android:orientation="vertical" >

<ImageView
    android:id="@+id/slider"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_centerVertical="true"
    android:src="@drawable/your_drawable" />
</RelativeLayout>

您可以修改屏幕宽度计算部分(我目前的code是不那么干净),并在的.xml 添加这个观点是这样的:

You can modify screen width calculation part (my current code is not so clean), and add this view in .xml like this :

<com.your.package.path.ImageTouchSlider
      android:id="@+id/slider"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />

在你的类,你可以找到这样的观点:

In your class, you can find this view :

ImageTouchSlider slider = (ImageTouchSlider) findViewById(R.id.slider);
slider.setOnImageSliderChangedListener(new ImageTouchSlider.OnImageSliderChangedListener() {

        @Override
        public void onChanged() {
            // do something what you want here.
        }

    });

希望这可以帮助! :)

Hope this can help! :)

这篇关于滑块按钮接受Android的电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 01:33