如何在android上实现IPhone的按钮滑动效果

来源:互联网 发布:怎么把不安全网络 编辑:IT博客网 时间:2019/09/17 14:51

要实现苹果的按钮动画效果首先肯定要自定义控件。

package com.jrrjw.button;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.support.v4.widget.SearchViewCompat.OnCloseListenerCompat;import android.util.AttributeSet;import android.util.MonthDisplayHelper;import android.view.MotionEvent;import android.view.View;public class MyView extends View {private Bitmap backgroundImg;private Bitmap sildeImg;private Paint paint;int sliderPosition = 0;MyClickListener clickListener;boolean isOpen = false;int firstPosition = 0;int lastPosition = 0;boolean isClick = true;public MyView(Context context, AttributeSet attrs) {super(context, attrs);initView();}/** * 第一步 初始化view 把一些图片,资源加载到对象里 第二步 告诉ViewGroup 你自定义的这个view的宽和高onMeasure 第三步 * 告诉ViewGroup你的view的布局,一般默认 第四步 onDraw,开始画你的view * 移动的思路: 首先记录下手指按下的时候的位置,然后判断手指移动的位置,如果位置很少然后就松开了则为点击,否则让图片跟着手指走,当手指松开的时候 * 判断图片的位置是否可以设置成开或者关, */private void initView() {//初始化背景图backgroundImg = BitmapFactory.decodeResource(getResources(),R.drawable.switch_background);//初始化按钮sildeImg = BitmapFactory.decodeResource(getResources(),R.drawable.slide_button);//初始化画笔paint = new Paint();paint.setAntiAlias(false); // 取消锯齿clickListener = new MyClickListener();setOnClickListener(clickListener);}/***设置触摸事件*/public boolean onTouchEvent(MotionEvent event) {super.onTouchEvent(event);//获得动作switch (event.getAction()) {case MotionEvent.ACTION_DOWN://当用户按下的时候记录第一位置,和最后位置firstPosition = lastPosition = (int) event.getX();//因为在android中的事件传递机制,先执行触摸事件,然后才会由点击事件,这里先取消点击事件isClick = false;if (Math.abs(event.getX() - firstPosition) < 10) {isClick = true;}break;case MotionEvent.ACTION_MOVE://判断移动的距离,如果距离够远,才算是拖动,否则则是点击if (Math.abs(event.getX() - firstPosition) < 10) {isClick = true;} else {isClick = false;}System.out.println(isClick + "click is move event position"+ event.getX() + "firstposition " + firstPosition);int dip = (int) (event.getX() - lastPosition);lastPosition = (int) event.getX();sliderPosition += dip;// influeState();break;case MotionEvent.ACTION_UP:System.out.println(isClick + "click is up");//判断是不是点击,如果是点击,就传递给点击事件去做if (!isClick) {int po = (backgroundImg.getWidth() - sildeImg.getWidth()) / 2;System.out.println(sliderPosition + "position:" + po);if (sliderPosition < po) {isOpen = false;} else {isOpen = true;}//最后刷新状态influeState();}break;}infueView();return true;}private void infueView() {// 判断SlidePosition的值是否合法int maxLeft = backgroundImg.getWidth() - sildeImg.getWidth(); // slideBtn// 左边届最大值// 确保 slideBtn_left >= 0sliderPosition = (sliderPosition > 0) ? sliderPosition : 0;// 确保 slideBtn_left <=maxLeftsliderPosition = (sliderPosition < maxLeft) ? sliderPosition : maxLeft;invalidate();}private class MyClickListener implements OnClickListener {@Overridepublic void onClick(View v) {if (isClick) {System.out.println("被点击了");isClick = !isClick;influeState();}}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(backgroundImg.getWidth(),backgroundImg.getHeight()); // 告诉viewgroup 他的宽和高}/** * 修改按钮的位置 */public void influeState() {//判断按钮是否为打开状态if (isOpen) {sliderPosition = backgroundImg.getWidth() - sildeImg.getWidth();System.out.println("isopen " + sliderPosition);} else {sliderPosition = 0;}isOpen = !isOpen;infueView();}@Overrideprotected void onDraw(Canvas canvas) {//画两张图片canvas.drawBitmap(backgroundImg, 0, 0, paint);canvas.drawBitmap(sildeImg, sliderPosition, 0, paint);}}

资源文件


0 0