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
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 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);
}
I have a class called Preview that is added as a child to one of my FrameLayouts. This is the Preview's onDraw method. fingerprint.png is in my drawable folder, and
protected void onDraw(Canvas canvas) {
System.out.println("on draw");
Resources res = getResources();
fingerprint = res.getDrawable(R.drawable.fingerprint);
fingerprint.draw(canvas);
//fingerprintScaled.draw(canvas);
}
"on draw" prints, but the fingerprint image is not rendered.
The reason it is not drawing is because you are calling draw wrong.
in protected void onDraw(Canvas canvas)
do something like this (except dont decode stuff in onDraw)
Bitmap fingerprint = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
canvas.drawBitmap (fingerPrint, aMatrix, paintToDraw);
this should draw your bitmap properly.
I want to create an ImageView and then draw text on top of the ImageView. I also need to be able to modify the text periodically. Currently I've created a custom view that extends ImageView. Then I overwrite onDraw() and use it to draw the text. Only problem then is that when I use my custom ImageView it doesn't draw the image, just the text.
public class BoardView extends ImageView
{
public BoardView(Context context)
{
super(context);
}
protected void onDraw(Canvas canvas)
{
Paint paint = new Paint();
setImageResource(R.drawable.board);
paint.setColor(Color.BLUE);
canvas.drawText(x.getName(), x.getX(), x.getY(), paint);
}
}
You should call the super.onDraw in your onDraw method before your draw code.
Is there a way to resize my canvas to fit the width of a path, or is there a way to resize a path to fit a canvas.
IF both are possible which would be easier and how?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
demoview = new DemoView(this);
}
public class DemoView extends View implements OnTouchListener{
Paint paint = new Paint();
public DemoView(Context context){
super(context);
}
public void onDraw(Canvas canvas) {
canvas.drawPath(Plotter.path, paint);
}
}
Somewhat artificial, but if you use your path as a clipping path you may be able to then obtain its bounds by calling getClipBounds, and from there you can easily calculate the scale transformation needed to make it resize to fit a canvas.
There's got to be a better way though.