Android finger drawing (2)

Source: Internet
Author: User
package cn.c;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Bundle;import android.view.Display;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;public class MainActivity extends Activity{    private ImageView mImageView;    private Bitmap mBitmap;    private Paint mPaint;    private Canvas mCanvas;    float downX=0;    float downY=0;    float upX=0;    float upY=0;        @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        mImageView=(ImageView) findViewById(R.id.imageView);        Display display=getWindowManager().getDefaultDisplay();        int w=display.getWidth();        int h=display.getHeight();        mBitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);        mCanvas=new Canvas(mBitmap);        mPaint=new Paint();        mPaint.setColor(Color.RED);        mImageView.setImageBitmap(mBitmap);    }    @Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN:            downX=event.getX();            downY=event.getY();break;case MotionEvent.ACTION_MOVE:upX=event.getX();            upY=event.getY();            mCanvas.drawLine(downX, downY, upX, upY, mPaint);            mImageView.invalidate();            downX=upX;            downY=upY;break;case MotionEvent.ACTION_UP:            upX=event.getX();            upY=event.getY();            mCanvas.drawLine(downX, downY, upX, upY, mPaint);            mImageView.invalidate();break;case MotionEvent.ACTION_CANCEL:break;default:break;}return true;}   }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.