I have a view class and and activity and the view class in linked in with the XML. The view class starts by drawing a bitmap of the letter a. In the activity there are next and previous buttons to allow the user to maneuver through the letter images. However I have looked everywhere for clearing a canvas and there doesn't seem to be anything on the matter. I am unsure how to access the view class to change the images as they are linked through the XML layout file.
View class
protected void onDraw(Canvas canvas) {
drawLetterA(canvas);
}
public void drawLetterA (Canvas canvas) {
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lettera);
canvas.drawBitmap(myBitmap, 0, 0, null);
}
public void drawLetterB (Canvas canvas) {
//canvas.drawColor(Color.WHITE);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.letterb);
canvas.drawBitmap(myBitmap, 0, 0, null);
}
Related
I'm trying to create myImageView Custom Control that extends ImageView
I need to animate everything that draw in myImageView.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(bitmap, getImageMatrix(), paint);
}
I created two class - one is a class which extends SurfaceView and has a method onDraw(), inside which I'm drawing an image on my canvas:
public class Board extends SurfaceView{
public BitmapFactory myBitmapFactory = new BitmapFactory();
public Bitmap myBitmap = new Bitmap;
public Board(Context context) {
super(context);
setFocusable(true);
}
//here i implement all the abstract classes, which are not important now
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);
Paint paint = new Paint();
canvas.drawBitmap(myBitmap, 20, 20, paint);
}
public void setBitmap(Bitmap b){
this.myBitmap = b;
}
}
And I have also my activity class, where after a button click which runs the method changeImage. I want two change this image which i put on my canvas
public class MyActivity extends Activity {
public Context context = this;
public Board board;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_game);
}
public void changeImage(View view){
Bitmap tmpBitmap = new Bitmap();
BitmapFactory tmpBitmapFactory = new BitmapFactory();
tmpBitmap = Bitmap.createScaledBitmap(tmpBitmapFactory.decodeResource(getResources(), R.drawable.image2), 130, 130, false);
board.setBitmap(tmpBitmap);
board.invalidate();
}
}
But after calling method changeImage nothing is changing - the image is the same as at the beginning. I tried to use invalidate() also inside setBitmap() method, but it also doesn't work. Is this because every time when I call invalidate() I'm creating a new Bitmap or I'm doing something else wrong?
---EDIT---
So ofc, wrong was that:
myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);
was setting all the time the same image. I changed it, but still invalidate() thosent work. I mean the image is not changing. I was looking for the result in other topics, but I did't find any solution, now my classes looks like this:
public class Board extends SurfaceView{
public BitmapFactory myBitmapFactory = new BitmapFactory();
public Bitmap myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), size, size, false);
public int tmp = 0;
public Paint paint = new Paint();
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawBitmap(myBitmap, 50, 50, paint);
}
public void setBitmaps(Bitmap one){
this.paint = new Paint();
this.myBitmap = one;
this.tmp=4;
this.invalidate();
}
And the method changeImage() inside my activity:
public void changeImage(View view){
Bitmap tmpBitmap1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.blue), 230, 230, false);
myGameBoard.setBitmaps(tmpBitmap1);
}
I tried to use: invalidate(), postInvalidate(), but it doesn't work and i hae now idea what am I doing wrong
You update myBitmap in onDraw():
myBitmap = Bitmap.createScaledBitmap(myBitmapFactory.decodeResource(getResources(), R.drawable.image), 90, 90, false);
So your updated image is still not used as you directly override it with another image...
(old answer, which is now obsolete due to fixed question)
There seems something else off in your example code. In setBitmap() you store the new bitmap in pawnBitmap. In the onDraw() method, you do not use pawnBitmap. So it is expected that nothing changes, as you do not use the changed values...
(old answer, which is now obsolete due to change in question)
You are trying to call a static class method with:
Board.setBitmap(tmpBitmap);
Board.invalidate();
(I am wondering why you did not get compile errors... Or did you forgot to mention them?)
As Board is the name of your class. Instead you need to call the instance method.
Board board = ...
board.setBitmap(tmpBitmap);
board.invalidate();
This obviously requires the board instance, which you need to available in your activity class.
Note: if you are calling invalidate() from a non-UI thread, you need to use postInvalidate().
I have a LinearLayout whose background color is set to black. In this LinearLayout, I have a View in which I draw using Canvas. Because the onDraw() method will be called multiple times, I want to clear what I drew previously when I call onDraw() method, thus I use Canvas.drawColor(Color.BLACK) to clear the canvas.
But what I get is a black screen without anything even when I draw something new. I can already draw something before I add Canvas.drawColor(Color.BLACK) within onDraw() method.
EDIT: codes of my onDraw() method
String value = "";
static Bitmap bitmap;
static Canvas canvas;
public void init(){// this is called by constructor method
this.setWillNotDraw(false);
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas();
canvas.setBitmap(bitmap);
}
public void onDraw(Canvas canvas){
canvas.drawBitmap(bitmap, 0, 0, null);
drawGrid();
}
public void drawGrid(){
Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setStrokeWidth(1);
canvas.drawText(value, somex, somey, paint);
}
public void changeData(String value){
this.value = value;
this.postInvalidate();
}
Another question, where is the right place I call Canvas.drawColor(Color.BLACK)?
I used the below code and it works for me. Clearing the draw on canvas.
public void resetBitmapCanvasAndPath() {
// TODO Auto-generated method stub
mDrawingUtilities.mBitmap = Bitmap.createBitmap(Constants.SCREEN_WIDTH,Constants.SCREEN_HEIGHT ,
Bitmap.Config.ARGB_8888);
mDrawingUtilities.mCanvas = new Canvas(mDrawingUtilities.mBitmap);
mDrawingUtilities.mPath = new Path();
}
Here's a link similar to your question and have a look at the accepted answer.
How to clear finger paint?
use Color.TRANSPARENT instead of using Color.BLACK
I have this class that extends View and draws a line:
public class MyDraw extends View
{
Paint paint = new Paint();
public MyDraw(Context context)
{
super(context);
paint.setColor(Color.BLUE);
}
#Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(1, 1, 100, 100, paint);
}
}
I would like to use the existing view from the Context to draw on top of it. Is it possible?
If you are just trying to get the view as a bitmap, you can get it from the drawing cache. This should work.
view.buildDrawingCache;
Bitmap bm = view.getDrawingCache
You cannot use the exisitng view instance and add it again as it already has a parent assigned to it and it wil cause an exception.
I want to display canvas contents on imageview in android
i am not understanding the
imageview.draw(canvas);
Heres my code:
public class Matrix extends Activity {
public Bitmap mybitmap,newbmp,bitmap,bmp;
ImageView imageview;
Paint paint;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView)findViewById(R.id.ImageView01);
imageview.setDrawingCacheEnabled(true);
}
protected void onDraw(Canvas canvas)
{
imageview.draw(canvas);
mybitmap=BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(mybitmap, 0, 0, paint);
}
}
"I want to display canvas contents on imageview in android"
So you want to draw what is on the Canvas in to your ImageView? If that's what you want then you need to read the links given by johike because you seem to have become confused a little.
The following in your code:
imageview.draw(canvas);
Does NOT mean draw the contents of canvas in to the imageview. It means the opposite, draw the imageview to the canvas.
Even if your question is not detailed enough to give you a precise answer I can give you the following hints:
Derive your on class from ImageView and then override the onDraw method
#Override
protected void onDraw(Canvas canvas) {
// draw a blue background
canvas.drawColor(Color.BLUE);
// additional drawings here
}
Further study the Android references:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
http://developer.android.com/reference/android/graphics/Canvas.html