Android自定义view的触摸反馈

* 触摸控件使其能够做一个放大缩小的动画.
* 我是在项目中有这样的需求就简单的实现了一下,今天做个记录.
	
  /* *
     * Created by Qin_Li_Yang on 2017/11/14.
 	 * <p>
    * 创建一个TouchFeedback类
    * 这里使用单利模式,应该是可以的哈哈.
   */

    public class TouchFeedback {
    /**
     * 缩放动画的单方面时长,假如是100,是指放大100,当手指离开缩小又是100.
     */
    private int duration = 100;
    /**
     * 放大或者缩小的值
     */
    private float aFloatBig = 1.2f;

    /**
     * 可以自己设定
     *
     * @param duration
     * @param aFloatBig
     */
    public void setDurationAndFloatBig(int duration, float aFloatBig) {
        this.duration = duration;
        this.aFloatBig = aFloatBig;
    }

    /**
     * 私有构造
     */
    private TouchFeedback() {
    }

    /**
     * 定义回调的接口
     */
    public interface TouchFeedbackListener {
        void onFeedback(View view);
    }

    private static TouchFeedback touchFeedback = null;

    public static TouchFeedback getTouchFeedbackInstance() {
        if (touchFeedback == null) {
            synchronized (TouchFeedback.class) {
                if (touchFeedback == null) {
                    touchFeedback = new TouchFeedback();
                }
            }
        }
        return touchFeedback;
    }

    /**
     * 触摸的监听
     * @param touchFeedListener
     * @param view
     */
    public void setTouchFeedListener(final TouchFeedback.TouchFeedbackListener touchFeedListener, View view) {
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        scaleBig(v);
                        break;
                    case MotionEvent.ACTION_MOVE:

                        break;

                    case MotionEvent.ACTION_UP:
                        scaleSmall(v, true, touchFeedListener);

                        break;
                    case MotionEvent.ACTION_CANCEL:
                        scaleSmall(v, false, touchFeedListener);
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 恢复原形的动画
     * @param v
     * @param b
     * @param touchFeedListener
     */
    private void scaleSmall(final View v, boolean b, final TouchFeedbackListener touchFeedListener) {
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(v, "ScaleX", aFloatBig, 1f).setDuration(duration);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(v, "ScaleY", aFloatBig, 1f).setDuration(duration);
        objectAnimator2.start();
        objectAnimator3.start();
        if (b) {
        /**动画执行的监听**/
            objectAnimator2.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                /**
                 * 动画结束
                 * @param animation
                 */
                @Override
                public void onAnimationEnd(Animator animation) {
                    touchFeedListener.onFeedback(v);
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        }


    }
		
		    /**
		     * 变大的动画
		     * @param v
		     */
		    private void scaleBig(View v) {
		        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(v, "ScaleX", 1f, aFloatBig).setDuration(duration);
		        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(v, "ScaleY", 1f, aFloatBig).setDuration(duration);
		        objectAnimator.start();
		        objectAnimator1.start();
		    }
		}
	
	
   * 简单使用
		/**
      * 实现刚才写的那个类里面的TouchFeedbackListener接口
     */
    public class MainActivity extends AppCompatActivity implements TouchFeedback.TouchFeedbackListener {

    private Button t1;
    private Button t2;
    private Button t3;
    private Button t4;
    private Button t5;
    private Button t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        t1 = (Button) findViewById(R.id.t1);
        t2 = (Button) findViewById(R.id.t2);
        t3 = (Button) findViewById(R.id.t3);
        t4 = (Button) findViewById(R.id.t4);
        t5 = (Button) findViewById(R.id.t5);
        t6 = (Button) findViewById(R.id.t6);

        TouchFeedback touchFeedbackInstance = TouchFeedback.getTouchFeedbackInstance();
        touchFeedbackInstance.setDurationAndFloatBig(300, 1.5f);
        touchFeedbackInstance.setTouchFeedListener(this, t1);
        touchFeedbackInstance.setTouchFeedListener(this, t2);
        touchFeedbackInstance.setTouchFeedListener(this, t3);
        touchFeedbackInstance.setTouchFeedListener(this, t4);
        touchFeedbackInstance.setTouchFeedListener(this, t5);
        touchFeedbackInstance.setTouchFeedListener(this, t6);
		    }
		
		    /**
		     * 这里像onclick一样
		     * @param view
		     */
		    @Override
		    public void onFeedback(View view) {
		        switch (view.getId()) {
		            case R.id.t1:
		                Toast.makeText(this, "t1", Toast.LENGTH_LONG).show();
		                break;
		            case R.id.t2:
		                Toast.makeText(this, "t2", Toast.LENGTH_LONG).show();
		                break;
		            case R.id.t3:
		                Toast.makeText(this, "t3", Toast.LENGTH_LONG).show();
		                break;
		            case R.id.t4:
		                Toast.makeText(this, "t4", Toast.LENGTH_LONG).show();
		                break;
		            case R.id.t5:
		                Toast.makeText(this, "t5", Toast.LENGTH_LONG).show();
		                break;
		            case R.id.t6:
		                Toast.makeText(this, "t6", Toast.LENGTH_LONG).show();
		                break;
		        }
		    }
		}

源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值