Canvas网上很多文章将它解释成画布,但是在使用
Canvas canvas=new 
Canvas(bitmap);后应该是设置画布内容为该bitmap,然后再使用,canvas.drawLine(startx,starty,stopx,stopy,paint);在上面作画,程序直接会报错关闭,想问这是为什么,只有canvas.drawBitmap(bitmap,matrix,null);后再使用drawLine才能在bitmap上面画图.

解决方案 »

  1.   

    package com.example.drawdemo011;import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.Config;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;

    public class Draw extends View {

    int startX=0,startY=0,stopX=0,stopY=0;
    Path path=new Path();
    Paint paint=new Paint();
    Matrix matrix=new Matrix();
    private float mX=0,mY=0;

    Bitmap cacheBitmap=null;
    Bitmap bitmap=null;
    Canvas cacheCanvas=null; public Draw(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.koala);
    cacheCanvas=new Canvas(bitmap); }
    public void onDraw(Canvas canvas){ Paint mpaint=new Paint();
    canvas.drawBitmap(bitmap, matrix, null);
    canvas.drawPath(path, paint);
    }
    public boolean onTouchEvent(MotionEvent event){
    float x=event.getX(),y=event.getY();
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
    touch_down(x,y);
    break; case MotionEvent.ACTION_MOVE:
    touch_move(x,y);
    break;
    case MotionEvent.ACTION_UP:
    touch_up(x,y);
    break;
    }

    return true;

    }private void touch_down(float x, float y) {
    path.moveTo(x,y);
    mX=x;mY=y;
    this.postInvalidate();
    }private void touch_move(float x, float y) {
    int distx,disty;
    distx=(int)Math.abs(mX-x);
    disty=(int)Math.abs(mY-y); path.quadTo(mX,mY,x,y);
    mX=x;mY=y;
    this.postInvalidate();
    }
    private void touch_up(float x, float y) { cacheCanvas.drawPath(path,paint);
    path.reset();
    this.postInvalidate();
    }}
    这个代码是改了好多遍之后的,还是卡在设置画布为bitmap那,不知道为什么就是过不去.
    如果新建立bitmap=Bitmap.created(w,h,Config_ARGB_8888);
    cacheCanvas=new Canvas();
    cacheCanvas.setIamge(bitmap);
    这样就能运行了.....
      

  2.   

    能告诉我是哪里错了,为什么不能把cacheCanvas= new Canvas(bitmap); ?
      

  3.   

    还是卡在设置画布为bitmap那,不知道为什么就是过不去.
    你说的卡在那里是什么意思?一直在那里运行么
      

  4.   

    不是,是一到那就直接程序报错强行关闭了,一运行就报错.这段代码改成其他的以后就能顺利运行也可以作画,但是我就是想要在引入的bitmap上面作画
      

  5.   

    06-21 09:56:39.465: D/dalvikvm(1329): GC_EXTERNAL_ALLOC freed 45K, 53% free 2543K/5379K, external 1625K/2137K, paused 85ms
    06-21 09:56:39.965: D/AndroidRuntime(1329): Shutting down VM
    06-21 09:56:39.975: W/dalvikvm(1329): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    06-21 09:56:39.995: E/AndroidRuntime(1329): FATAL EXCEPTION: main
    06-21 09:56:39.995: E/AndroidRuntime(1329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawdemo011/com.example.drawdemo011.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.drawdemo011.Draw
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.os.Handler.dispatchMessage(Handler.java:99)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.os.Looper.loop(Looper.java:123)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at java.lang.reflect.Method.invokeNative(Native Method)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at java.lang.reflect.Method.invoke(Method.java:507)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at dalvik.system.NativeStart.main(Native Method)
    06-21 09:56:39.995: E/AndroidRuntime(1329): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.drawdemo011.Draw
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.Activity.setContentView(Activity.java:1657)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at com.example.drawdemo011.MainActivity.onCreate(MainActivity.java:21)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  ... 11 more
    06-21 09:56:39.995: E/AndroidRuntime(1329): Caused by: java.lang.reflect.InvocationTargetException
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at java.lang.reflect.Constructor.constructNative(Native Method)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  ... 21 more
    06-21 09:56:39.995: E/AndroidRuntime(1329): Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at android.graphics.Canvas.<init>(Canvas.java:83)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  at com.example.drawdemo011.Draw.<init>(Draw.java:36)
    06-21 09:56:39.995: E/AndroidRuntime(1329):  ... 24 more
      

  6.   

    是不是因为xml出错而导致的呢?
      

  7.   

    xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
       tools:context=".MainActivity" ><com.example.drawdemo011.Draw
        android:id="@+id/ondraw"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
    </RelativeLayout>应该没问题的吧...