daixinet

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1、Java Code

package com.fish.helloworld;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TestMotionView(this));
    }
    
    public class TestMotionView extends View{
        private Paint mPaint = new Paint();
        private int mAction;
        private float mX;
        private float mY;
        
        public TestMotionView(Context c){
            super(c);
            mAction = MotionEvent.ACTION_POINTER_UP;
            mX = 0;
            mY = 0;
        }
        
        @Override
        protected void onDraw(Canvas canvas){
            Paint paint = mPaint;
            canvas.drawColor(Color.WHITE);
            if(MotionEvent.ACTION_MOVE == mAction){
                paint.setColor(Color.RED);
            }else if(MotionEvent.ACTION_UP == mAction){
                paint.setColor(Color.GREEN);
            }else if(MotionEvent.ACTION_DOWN == mAction){
                paint.setColor(Color.BLUE);
            }
            canvas.drawCircle(mX, mY, 10, paint);
            setTitle("A = " + mAction + " [" + mX + "," +mY +"]");
        }
        @Override
        public boolean onTouchEvent(MotionEvent event){
            mAction = event.getAction();
            mX = event.getX();
            mY = event.getY();
            invalidate();
            return true;
        }
        
    }
    
}

 

posted on 2014-07-30 22:22  daixinet.com  阅读(581)  评论(0编辑  收藏  举报