2012-09-06 49 views
0

可能重複:
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> 

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); 

     } 
    } 

現在我要畫一條線作爲我的手指移動。我被卡住瞭如何通過觸摸監聽器或其他方式來實現這一點。

如果有任何人知道......

回答

0

這是我做的:

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; 
} 
} 

如果你想畫一個空白頁上,忽略位圖,你應該罰款。