【问题标题】:How to draw a line using finger ? [duplicate]如何用手指画线? [复制]
【发布时间】:2012-09-06 15:20:28
【问题描述】:

可能重复:
Android How to draw a smooth line following your finger

我使用以下代码创建了布局:

activity_hand_write_demo.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="5dp">

      <LinearLayout android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
    android:background="@drawable/custom_border">
   <TextView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="20dp"
    android:textColor="#FFFFFF"
    android:gravity="center_horizontal"
    android:background="#AAAAAA" />
    </LinearLayout>
</LinearLayout>

drawable 中的custom_border.xml

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"               android:shape="rectangle">
  <corners android:radius="20dp"/>
  <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="#1ABCD1"/>
 </shape>

HandWriteDemo.java

  package com.example.handwritedemo;
  import android.os.Bundle;
  import android.app.Activity;


     public class HandWriteDemo extends Activity {

 @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_hand_write_demo);

        } 
   }

现在我想在手指移动时画一条线。我被卡住了如何通过触摸监听器或其他方式来实现这一点。

如果有人知道...

【问题讨论】:

    标签: android android-layout android-widget touch


    【解决方案1】:

    我开发了一个应用程序。我在那用过手指画。我给你代码的链接。希望这会有所帮助。

    https://github.com/anupam1525/AcrylicPaint/blob/master/Acrylic_Paint/src/anupam/acrylic/EasyPaint.java

    【讨论】:

      【解决方案2】:

      这就是我的做法:

      public MyView(Context c, int width, int height) {
          super(c);
      
          WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
          Display display = wm.getDefaultDisplay();
          int w = display.getWidth(); // deprecated
          int h = display.getHeight();
          // setFocusable(true);
          // setBackgroundResource(R.drawable.download);
      
          // setting paint
          mPath = new Path();
          mPaint = new Paint();
      
          mPaint.setColor(0xFF000000);
          mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
          mPaint.setAntiAlias(true);
          mPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
          mBitmapPaint = new Paint(Paint.DITHER_FLAG);
      
          this.getContext().getResources();
      
          Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.smoke);
          Bitmap bm2 = getResizedBitmap(bm, h, w);
      
          // converting image bitmap into mutable bitmap
      
          bitmap = bm2.createBitmap(w, h, Config.ARGB_8888);
          mCanvas = new Canvas();
          mCanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
          mCanvas.drawBitmap(bm2, 0, 0, null);
      
      }
      
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
      }
      
      @Override
      protected void onDraw(Canvas canvas) {
          //mCanvas.drawCircle(x, y, r, mPaint);
      
          // canvas.drawBitmap(bitmap, 0, 0, null);
          canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);
      
          canvas.drawPath(mPath, mPaint);
      
          super.onDraw(canvas);
      
      }
      
      private float mX, mY;
      
      private static final float TOUCH_TOLERANCE = 4;
      
      private void touch_start(float x, float y) {
          mPath.reset();
          mPath.moveTo(x, y);
          // mPath.lineTo(x, y);
          // mCanvas.drawPoint(x, y, mPaint);
          mX = x;
          mY = y;
      
      }
      
      private void touch_move(float x, float y) {
          float dx = Math.abs(x - mX);
          float dy = Math.abs(y - mY);
          if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
              mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
              mX = x;
              mY = y;
          }
          // invalidate();
      }
      
      private void touch_up() {
          mPath.lineTo(mX, mY);
          // commit the path to our offscreen
          mCanvas.drawPath(mPath, mPaint);
          // kill this so we don't double draw
          mPath.reset();
      }
      
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          float x = event.getX();
          float y = event.getY();
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              touch_start(x, y);
      
              invalidate();
              break;
          case MotionEvent.ACTION_MOVE:
              touch_move(x, y);
              invalidate();
              break;
          case MotionEvent.ACTION_UP:
              touch_up();
              invalidate();
              break;
          }
          return true;
      }
      
      public Bitmap getImage() {
          return mBitmap;
      }
      }
      

      如果你想在空白页面上绘图,忽略位图,应该没问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多