2013-01-01 140 views
3

現在我想畫布畫出一個顏色。用畫布繪製Android

public class AndroidTentaTestActivity extends Activity { 

    private Bitmap bm; 
    private Canvas c; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.main); 

     bm = Bitmap.createBitmap(100, 100, Config.ARGB_8888); 
     c = new Canvas(bm); 
     c.drawARGB(100, 0, 0, 150); 
    } 
} 

上面的代碼是我到目前爲止寫的,顯然不起作用。我懷疑Bitmap不知道怎麼連接到我正在做的事情上,但我不知道如何解決它。我如何?

+0

什麼* *究竟是你想怎麼辦? 「不知何故未連接」。你什麼意思?除非你在某處顯示位圖,當然你不會看到它。 – Simon

+0

畫布不顯示,因此整個屏幕都是黑色的(在我用於調試的s2上)。我如何顯示畫布? –

+1

你還沒有解釋你想做什麼。您無法「顯示」畫布。畫布只是一個在位圖上繪製調用的持有者。除非您在畫布上繪製屬於您的用戶界面的位圖,否則您將看不到任何內容。 – Simon

回答

0

在mPaint.setColor()中更改所需的顏色。

public class AndroidTentaTestActivity extends AppCompatActivity { 

MyView mv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    mv = new MyView(this); 
    mv.setDrawingCacheEnabled(true); 
    setContentView(mv); 

    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(0xFFFF0000); 
} 

private Paint mPaint; 

@Override 
public void onClick(View v) { 

} 

public class MyView extends View { 

    private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Path mPath; 
    private Paint mBitmapPaint; 
    Context context; 

    public MyView(Context c) { 
     super(c); 
     context = c; 
     mPath = new Path(); 
     mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 

    } 


    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawColor(0xFFFFFFFF); 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 
    } 

    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 1; 

    private void touch_start(float x, float y) { 
     //showDialog(); 
     mPath.reset(); 
     mPath.moveTo(x, y); 

     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);      //2,2.old values 

      mX = x; 
      mY = y; 
     } 

    } 

    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; 
    } 
} 
0
LinearLayout ll = (LinearLayout) findViewById(R.id.rect); 
Paint paint = new Paint(); 
paint.setColor(Color.parseColor("#CD5C5C")); 
Bitmap bg = Bitmap.createBitmap(ll.getWidth(),ll.getHeight() , Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bg); 
canvas.drawARGB(200, 0, 225, 255); 
ll.setBackgroundDrawable(new BitmapDrawable(bg));