i write text on a picture with this code :
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.file);
Canvas g = new Canvas(mBitmap);
Paint p = new Paint();
p.setColor(Color.BLACK);
p.setAntiAlias(true);
g.drawText("Text", 10, 10, p);
How can i show result in a imageview ?
thanks
Create an ImageView (if you don't have one in your layout), and use setImageDrawable() :
ImageView image = new ImageView(this);
image.setImageDrawable(new BitmapDrawable(mBitmap));
Then add your ImageView to your layout with addView().
Related
how can I display a programmatically created ImageView on top of a previously created View Canvas? I tried using bringToFront(), but without success.
Code:
RelativeLayout root = findViewById(R.id.layout);
ImageView img = new ImageView(GameGuessActivity.context);
img.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;
root.addView(img, params);
img.bringToFront();
The Image is being displayed, but underneath the canvas, which I don't want.
You have to redraw your imageview on canvas:
pcanvas = new Canvas();
pcanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
pcanvas.drawBitmap(bitmap, 0, 0, null);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
image.draw(pcanvas);
img.bringToFront();
img.invalidate();
img.draw(pcanvas);
I'm trying to draw a circle on an image which placed as res/drawable/schoolboard.png. the image fills the activity background. the following does not work:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(60, 50, 25, paint);
ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(bitmap);
any help will be highly appreciated. thanks.
There are some errors in your code:
first of thing you cannot give reference Id for drawable in findViewById
so I think you mean something like that
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
schoolboard_image_view is the image id in your xml layout (check your layout for the right id)
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(60, 50, 25, paint);
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
Please make sure to use the right image Id for:
ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view);
First of all you need to create a new bitmap, because bitmap from BitmapFactory.decodeResource() method is immutable. You can do this with the following code:
Bitmap canvasBitmap = Bitmap.createBitmap([bitmap_width], [bitmap_height], Config.ARGB_8888);
Use this bitmap in Canvas constructor. Then draw your bitmap on canvas.
Canvas canvas = new Canvas(canvasBitmap);
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
canvas.drawCircle(60, 50, 25, paint);
Also R.drawable.schoolboard is not a correct view id.
ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard);
I have an image view. I need to display the various text on images.For each image the position of the text gets differ.I will give position value using x,y co ordinates of ImageView.
Any guesses How to perform this?
Create a bitmap
Create a canvas
Draw what you need
Post it to the imageview
Like this:
Bitmap drawingSurface = Bitmap.createBitmap(picBitmap.getWidth(), picBitmap.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(drawingSurface);
canvas.drawBitmap(picBitmap, 0, 0, null);
canvas.drawText("Hi!", 10, 10, new Paint());
myImageView.setImageBitmap(drawingSurface);
Encapsulate the image and text in a layout then give coordinates to the views inside their parrent then use theencapsulated view group like your imageview.
Options options = new BitmapFactory.Options();
options.inScaled=false;
Bitmap original;
Bitmap mutable = null;
try{
original = BitmapFactory.decodeResource(this.getResources(), R.drawable.,null);
mutable = original.copy(Bitmap.Config.ARGB_8888, true);
}catch (Exception e) {
// TODO: handle exception
}
Paint paint=new Paint();
paint.setAntiAlias(true);
Canvas c= new Canvas(mutable);
Log.d("density",""+dm.density);
paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(42);
c.drawText("text", (mutable.getWidth()/2)-x,(mutable.getHeight()/2)+y, paint);
BitmapDrawable bitmap = new BitmapDrawable(mutable)
imageView set background drawable bitmap
I want to draw a number on an existing drawable. Like the unread count at an email icon. The drawable is the top icon of a button. This is my code:
BitmapDrawable d = (BitmapDrawable) button.getCompoundDrawables()[1];
if(d != null) {
Bitmap src = d.getBitmap();
Bitmap b = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
Canvas c = new Canvas(b);
Paint p = new Paint();
p.setAntiAlias(true);
p.setStrokeWidth(1);
p.setStyle(Style.FILL_AND_STROKE);
p.setColor(Color.rgb(254, 0, 1));
c.drawBitmap(src, 0, 0, p);
c.drawCircle(b.getWidth()-5, 5, 5, p);
button.setCompoundDrawables(null, new BitmapDrawable(b), null, null);
}
The resulting drawable is emtpy. Is there something wrong with this code?
Thanks in advance.
The new bitmap has to call setBounds(...)
BitmapDrawable dn = new BitmapDrawable(b);
dn.setBounds(d.getBounds());
button.setCompoundDrawables(null, dn, null, null);
I'm not sure about your implementation but another way to go about this is to add your bitmap to a framelayout as an imageview and then add a textview (appropriately styled) with an offset; depending where you want the badge e.g. top right hand corner.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting a view to Bitmap without displaying it in Android?
I am trying to convert the view into bitmap from following reference link
link text
Now the problem is how can i get the bitmap that is being converted from view only. in the example author has used relativelayout.dispatchDraw(c) but this line is giving me compile time error i.e.
The method dispatchDraw(Canvas) from
the type ViewGroup is not visible
Here is my code and i have written the following code inside onCreate function
Canvas c=null;
//Create Layout
RelativeLayout relativeView ;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeView = new RelativeLayout(this);
relativeView.setLayoutParams(lp);
//Background of Layout
Bitmap viewBgrnd = BitmapFactory.decodeResource(getResources(),R.drawable.bgblack);
relativeView.setBackgroundDrawable(new BitmapDrawable(viewBgrnd));
//associated with canvas
Bitmap returnedBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
c = new Canvas(returnedBitmap);
Paint paint = new Paint();
//Create Imageview that holds image
ImageView newImage = new ImageView(this);
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bgpink);
newImage.setImageBitmap(srcBitmap);
TextView newText = new TextView(this);
newText.setText("This is the text that its going to appear");
c.drawBitmap(viewBgrnd, 0, 0, paint);
relativeView.layout(100, 0, 256, 256);
relativeView.addView(newImage);
relativeView.addView(newText);
// here i am getting compile time error so for timing i have replaced this line
// with relativeView.draw(c);
relativeView.dispatchDraw(c);
Here the returnedBitmap should contain the image of (ImageView and TextView) but this bitmap contains only bacground bitmap of relativeView i.e. bgblack
here is my solution:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
Enjoy :)
This works for me:
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
I believe the view has to be visible (ie. getVisiblity() == View.VISIBLE). If you are trying to capture it but hide it to the user at the same time, you could move it off screen or put something over it.