class SquareImageView extends View {
public SquareImageView(Context context) {
super(context);
init();
}
private void init() {
bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
=mCanvas = new Canvas(bitmap);
mpaint = new Paint();
mpaint.setAntiAlias(true);
mpaint.setStyle(Paint.Style.STROKE);
mpaint.setColor(Color.RED);
mCanvas.drawColor(Color.BLUE, PorterDuff.Mode.DARKEN);
mCanvas.drawRect(100, 100, 200, 200, mpaint);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}``
}
if i use mCanas.drawRect() inside init method then it's not drawing anything but if i use inBuilt canvas object inside OnDraw(Canvas canvas) method like this
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(100, 100, 200, 200, mpaint);
}
then rectangle is drawing and if i used mCanvas inside onDraw() then again it's not showing anything can anybody explain to me why this happening i m really get frustrated any help would be appreciated
It means that onDraw is the only drawing gate for you. https://developer.android.com/training/custom-views/custom-drawing#java
Related
Here is the Code:
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
drawUPS( canvas);
drawFPS( canvas);
}
public void drawUPS(#NonNull Canvas canvas){
String averageUPS = Double.toString(gameloop.getAverageUPS());
Paint textPaint = new Paint();
int color = ContextCompat.getColor(context, R.color.white);
canvas.drawText("UPS" + averageUPS, 100, 100,textPaint);
textPaint.setColor(color);
textPaint.setTextSize(50);
}
public void drawFPS(#NonNull Canvas canvas){
String averageFPS = Double.toString(gameloop.getAverageFPS());
Paint paint = new Paint();
int color = ContextCompat.getColor(context, R.color.white);
paint.setColor(color);
paint.setTextSize(50);
canvas.drawText("FPS" + averageFPS, 100, 200,paint);
}
I did everything that is necessary for the canvas to be drawn but it just wont and i dont find any solutions anywhere
I'm tring to draw some line and shapes on canvas and then convert it to bitmap on ImageView. I'm usin a custom class that extands "View" and on "OnDraw method i'm drawing the lines. here is my code(this class only draw simple lines) :
public class finalDraw extends View {
public finalDraw(Context context) {
super(context);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
for (int i = 0; i <100; i++) {
canvas.drawLine(xStart * i + 50 , yStart , stopX + 30 , stopY,paint);
}
invalidate();
}
}
How can i get the drawing result and show it on ImageView?
Thanks!
Found this article may help: http://www.informit.com/articles/article.aspx?p=2143148&seqNum=2
draw.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bitmap imageBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(imageBitmap);
float scale = getResources().getDisplayMetrics().density;
Paint p = new Paint();
p.setColor(Color.BLUE);
p.setTextSize(24*scale);
canvas.drawText("Hello", imageView.getWidth()/2, imageView.getHeight()/2, p);
imageView.setImageBitmap(imageBitmap);
}
});
Create a Canvas with a bitmap, by Canvas(Bitmap bitmap). All draws to that canvas will in that bitmap.
There are two classes: Renderer which extends View and Grid. In the onDraw method of Renderer, I want to pass Canvas to the function of Grid. Since Java passes arguments only by value the code below does not work. I mean it does not draw a rectangle on the screen and says that program has stopped on my phone. Any idea?
public class Renderer extends View {
Paint paint;
Grid grid;
public Renderer(Context context, Grid grid) {
super(context);
paint = new Paint();
this.grid = grid;
}
protected void onDraw (Canvas canvas) {
canvas.drawRGB(255, 255, 255);
grid.tempName(canvas); // removing this solves problem
invalidate();
}
}
public class Grid {
Paint paint;
public void tempName (Canvas canvas) {
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
canvas.drawRect(100, 100, 200, 200, paint);
}
}
i have two imageviews one is gallery image and one is transparent image drawn throw canvas on second image and i want to clean that transparent image by finger
i trying to do like
Link:https://play.google.com/store/apps/details?id=com.steam.doodle&hl=en
i tryd below code
public class MainActivity extends Activity implements OnTouchListener {
ImageView image,transimage;
Paint paint;
Bitmap bitmap,resultbitmap;
Canvas canvas;
Button clear;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)findViewById(R.id.imageView1);
transimage=(ImageView)findViewById(R.id.imageView2);
//Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.saibaba);
Bitmap mBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.snw);
Bitmap bmOverlay = Bitmap.createBitmap(mBitmap2.getWidth(), mBitmap2.getHeight(), mBitmap2.getConfig());
Canvas canvas = new Canvas();
canvas.setBitmap(bmOverlay);
paint=new Paint();
paint.setAlpha(200);
// canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.drawBitmap(mBitmap2, 0, 0, paint);
transimage.setImageBitmap(bmOverlay);
}
}
How to clean the canvas by finger
You just need to set Color= Transparent on the method onDraw. This is the tested code and is working.
#Override
protected void onDraw(Canvas canvas) {
if(isEraseMode){
paint.setColor(Color.TRANSPARENT);
canvas.drawPath(eraserPath, erasePaint);
//canvas.save();
}else{
canvas.drawPath(path, paint);
canvas.restore();
}
canvas.drawPath(path, paint);
super.dispatchDraw(canvas);
}
Hope this would help you..:)
I am Getting a problem drawing rectangle over imageview. Here is the peace of code. Xml is also there . my whole concern is if we can draw rect over imageview.??
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cropimage);
paint=new Paint();
bobj = new BaldBooth1();
bm = BaldBooth1.bMap;
d = new BitmapDrawable(bm);
iv = ((ImageView) findViewById(R.id.image));
iv.setImageDrawable(d);
createRectInView(iv);
((ImageButton) findViewById(R.id.next)).setOnClickListener(this);
}
public void createRectInView(View v) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas=new Canvas();
canvas.drawRect(50, 50, 80, 80, paint);
v.draw(canvas);
}
Your method createRectInView(View v) does not draw a rectangle over ImageView, it just create a canvas, draw a rectangle on that canvas, then draw the content of ImageView on that canvas, so it does not do what you expect.
Here is one possible resolution: you can extend ImageView and override its onDraw() method, for example..
public class ExtendedImageView extends ImageView {
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(50, 50, 80, 80, paint);
}
}
Updated:
Hi arun, I just test the code, and it works fine. Here are the details: for example, you can create the ExtendedImageView in package com.abc.widget, so in your cropImage.xml file, replace the <ImageView android:id="#+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"> to <com.abc.widget.ExtendedImageView android:id="#+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content">. As you see, you only need to change the class name. Then changed the onCreate() method as:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cropimage);
bobj = new BaldBooth1();
bm = BaldBooth1.bMap;
d = new BitmapDrawable(bm);
iv = ((ImageView) findViewById(R.id.image));
iv.setImageDrawable(d);
((ImageButton) findViewById(R.id.next)).setOnClickListener(this);
}