Touch Screen

今天试了一天,在BookDetail实现public boolean onMotionEvent(MotionEvent event)
一直都没有效果!
晚上才发现,原来要实现触摸方式。必须在自己的view里面实现


贴上一个例子做为参考:

  1. /*
  2. * Copyright (C) 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.android.samples.graphics;
  17. import android.app.Activity;
  18. import android.content.Context;
  19. import android.graphics.Bitmap;
  20. import android.graphics.Canvas;
  21. import android.graphics.Paint;
  22. import android.graphics.Rect;
  23. import android.os.Bundle;
  24. import android.view.MotionEvent;
  25. import android.view.View;
  26. //Need the following import to get access to the app resources, since this
  27. //class is in a sub-package.
  28. /**
  29. * Demonstrates the handling of touch screen events to implement a simple
  30. * painting app.
  31. */
  32. public class TouchPaint extends Activity {
  33.     @Override
  34.     protected void onCreate(Bundle icicle) {
  35.         super.onCreate(icicle);
  36.         setContentView(new MyView(this));
  37.     }
  38.     public class MyView extends View {
  39.         Bitmap mBitmap;
  40.         Canvas mCanvas;
  41.         private final Rect mRect = new Rect();
  42.         private final Paint mPaint;
  43.         private boolean mCurDown;
  44.         private int mCurX;
  45.         private int mCurY;
  46.         private float mCurPressure;
  47.         private float mCurSize;
  48.         private int mCurWidth;
  49.         public MyView(Context c) {
  50.             super(c);
  51.             mPaint = new Paint();
  52.             mPaint.setAntiAlias(true);
  53.             mPaint.setARGB(255255255255);
  54.         }
  55.         @Override
  56.         protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  57.             int curW = mBitmap != null ? mBitmap.width() : 0;
  58.             int curH = mBitmap != null ? mBitmap.height() : 0;
  59.             if (curW >= w && curH >= h) {
  60.                 return;
  61.             }
  62.             if (curW < w) curW = w;
  63.             if (curH < h) curH = h;
  64.             Bitmap newBitmap = Bitmap.createBitmap(curW, curH, false);
  65.             Canvas newCanvas = new Canvas();
  66.             newCanvas.setDevice(newBitmap);
  67.             if (mBitmap != null) {
  68.                 newCanvas.drawBitmap(mBitmap, 00null);
  69.             }
  70.             mBitmap = newBitmap;
  71.             mCanvas = newCanvas;
  72.         }
  73.         @Override
  74.         protected void onDraw(Canvas canvas) {
  75.             if (mBitmap != null) {
  76.                 canvas.drawBitmap(mBitmap, 00null);
  77.             }
  78.         }
  79.         @Override
  80.         public boolean onMotionEvent(MotionEvent event) {
  81.             int action = event.getAction();
  82.             mCurDown = action == MotionEvent.ACTION_DOWN
  83.                     || action == MotionEvent.ACTION_MOVE;
  84.             mCurX = (int)event.getX();
  85.             mCurY = (int)event.getY();
  86.             mCurPressure = event.getPressure();
  87.             mCurSize = event.getSize();
  88.             mCurWidth = (int)(mCurSize*(getWidth()/3));
  89.             if (mCurWidth < 1) mCurWidth = 1;
  90.             if (mCurDown && mBitmap != null) {
  91.                 int pressureLevel = (int)(mCurPressure*255);
  92.                 mPaint.setARGB(pressureLevel, 255255255);
  93.                 mCanvas.drawCircle(mCurX, mCurY, mCurWidth, mPaint);
  94.                 mRect.set(mCurX-mCurWidth-2, mCurY-mCurWidth-2,
  95.                         mCurX+mCurWidth+2, mCurY+mCurWidth+2);
  96.                 invalidate(mRect);
  97.             }
  98.             return true;
  99.         }
  100.     }
  101. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值