今天试了一天,在BookDetail实现public boolean onMotionEvent(MotionEvent event)
一直都没有效果!
晚上才发现,原来要实现触摸方式。必须在自己的view里面实现
贴上一个例子做为参考:
- package com.google.android.samples.graphics;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- public class TouchPaint extends Activity {
- @Override
- protected void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(new MyView(this));
- }
- public class MyView extends View {
- Bitmap mBitmap;
- Canvas mCanvas;
- private final Rect mRect = new Rect();
- private final Paint mPaint;
- private boolean mCurDown;
- private int mCurX;
- private int mCurY;
- private float mCurPressure;
- private float mCurSize;
- private int mCurWidth;
- public MyView(Context c) {
- super(c);
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setARGB(255, 255, 255, 255);
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- int curW = mBitmap != null ? mBitmap.width() : 0;
- int curH = mBitmap != null ? mBitmap.height() : 0;
- if (curW >= w && curH >= h) {
- return;
- }
- if (curW < w) curW = w;
- if (curH < h) curH = h;
- Bitmap newBitmap = Bitmap.createBitmap(curW, curH, false);
- Canvas newCanvas = new Canvas();
- newCanvas.setDevice(newBitmap);
- if (mBitmap != null) {
- newCanvas.drawBitmap(mBitmap, 0, 0, null);
- }
- mBitmap = newBitmap;
- mCanvas = newCanvas;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- if (mBitmap != null) {
- canvas.drawBitmap(mBitmap, 0, 0, null);
- }
- }
- @Override
- public boolean onMotionEvent(MotionEvent event) {
- int action = event.getAction();
- mCurDown = action == MotionEvent.ACTION_DOWN
- || action == MotionEvent.ACTION_MOVE;
- mCurX = (int)event.getX();
- mCurY = (int)event.getY();
- mCurPressure = event.getPressure();
- mCurSize = event.getSize();
- mCurWidth = (int)(mCurSize*(getWidth()/3));
- if (mCurWidth < 1) mCurWidth = 1;
- if (mCurDown && mBitmap != null) {
- int pressureLevel = (int)(mCurPressure*255);
- mPaint.setARGB(pressureLevel, 255, 255, 255);
- mCanvas.drawCircle(mCurX, mCurY, mCurWidth, mPaint);
- mRect.set(mCurX-mCurWidth-2, mCurY-mCurWidth-2,
- mCurX+mCurWidth+2, mCurY+mCurWidth+2);
- invalidate(mRect);
- }
- return true;
- }
- }
- }
发表于 @
2008年10月04日 09:05:00 | | 编辑|
举报| 收藏