3

For now I want the canvas to just draw a color.

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

The code above is what I´ve written so far that obviously does not work. I suspect the Bitmap is somehow not connected to what I am doing but I don´t know how to fix it. How do I?

7
  • What exactly are you trying to do? "Somehow not connected". What do you mean? Unless you display the bitmap somewhere, of course you are not going to see it. Commented Jan 1, 2013 at 18:39
  • The canvas does not show so the entire screen is black(on my s2 i use for debugging). How do I display the canvas? Commented Jan 1, 2013 at 19:00
  • 1
    You still have not explained WHAT you are trying to do. You cannot "display" a canvas. A canvas is just a holder for drawing calls on a bitmap. Unless you draw on the canvas of a bitmap that is part of your UI, you will not see anything. Commented Jan 1, 2013 at 19:27
  • I want to make the screen blue. Then how do I make the bitmap part of my UI? Commented Jan 1, 2013 at 19:49
  • Is there a reason you want to use a canvas for this? I think you need to start with some "Hello World" examples. Making the screen blue is very simple, it's first day in school. Create an XML layout and set the background colour of the root view to blue. Then use setContentView in onCreate() to inflate the XML into your UI. developer.android.com/guide/topics/ui/declaring-layout.html Commented Jan 1, 2013 at 21:00

2 Answers 2

1

Change the desired color in 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; } } 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.