How to add view on view (e.g. button on image) - android

image is set in imageview. I draw text successfully on image with the help of below code.
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.awais, myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
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);
canvas.drawText(String.valueOf(f), h, w, paint);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
but I want draw Buttons on image with different height and width.

If you already define your imageView inside relative layout in xml than just instantiate it by findViewById
//RelativeLayout relativeLayout =(RelativeLayout)(findViewById(.....));
otherwise dynamically create relative layout like this
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.setLayoutParams(lp);
relativeLayout.addView(imageview);
//than create button dynmaically
Button b = new Button(this);
b.setText("Button");
RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(btnrl);
relativeLayout.addView(b);
**Edit:**
In case you want arbitrary width and height than set button with and height like this
int width = 0; // assign your width
int height = 0; // assign your height
RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
width,height);
btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(btnrl);

Related

Textview at the center of canvas

I need to create a canvas in which i have add two images as BITMAP and a TEXTVIEW at the bottom right with multiple line.
Here is my code which i have tried -
drawnBitmap = Bitmap.createBitmap(1000, 1500, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(drawnBitmap);
canvas.drawColor(getResources().getColor(R.color.creamColor));
// JUST CHANGE TO DIFFERENT Bitmaps and coordinates .
canvas.drawBitmap(b, 0, 0, null);
canvas.drawBitmap(b2, 630, 250, null);
//for more images :
// canvas.drawBitmap(b3, 0, 0, null);
// canvas.drawBitmap(b4, 0, 0, null);
/*Draw text*/
//TextPaint paintText=new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
paintText.setColor(Color.BLACK);
paintText.setTextAlign(Paint.Align.CENTER);
paintText.setTextSize(50);
Typeface tf;
tf = Typeface.createFromAsset(getAssets(), "HelveticaNeueLTStd-Lt.otf");
paintText.setTypeface(tf);
RelativeLayout layout = new RelativeLayout(this);
layout.setBackgroundColor(Color.GREEN);
layout.setDrawingCacheEnabled(true);
TextView textView = new TextView(this);
textView.setVisibility(View.VISIBLE);
textView.setText(INNERTEXT);
textView.setTextSize(30);
textView.setWidth(300);
textView.setTextColor(Color.BLACK);
textView.setTypeface(tf);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
layout.addView(textView);
layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
// To place the text view somewhere specific:
// canvas.translate(500, 750);
canvas.drawBitmap(layout.getDrawingCache(), 500, 750, null);
// layout.draw(canvas);
I have divided the canvas into 4 equal parts, but the textview inside the 4. part is not centered.

Android: text turning into bitmap is not at Gravity.CENTER

I would like to convert text into bitmap, coded as follows:
Code:
public Bitmap convertTextToBM(String strength, int maxWidth, int maxHeight, String text)
{
TextView tv = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(maxWidth, maxHeight);
tv.setLayoutParams(layoutParams);
tv.setText(""+text);
tv.setTextColor(Color.BLACK);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, (maxHeight/2));
//strength
if (strength.equals("bold"))
{
tv.setTypeface(null, Typeface.BOLD);
}
tv.setGravity(Gravity.CENTER);
Bitmap testB = Bitmap.createBitmap(maxWidth, maxHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, maxWidth, maxHeight);
tv.draw(c);
return testB;
}
Screenshot:
Question:
How to make the text drawn at center of the available space? Thanks!
One thing you can do is to try to create this in XML. Try to set the gravity to CENTER_VERTICAL | CENTER_HORIZONTAL, but the other thing is, when you create the XML, you can set the layout_gravity to CENTER_VERTICAL | CENTER_HORIZONTAL (you can do it through code as well but it's more complicated).
Instead of
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(maxWidth, maxHeight);
tv.setLayoutParams(layoutParams);
tv.setGravity(Gravity.CENTER);
Use
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
tv.setGravity(Gravity.CENTER_VERTICAL |Gravity.CENTER_HORIZONTAL);
I have used another method to achieve a similar effect:
public Bitmap drawText(int textSize, String textColor, String strength, int textWidth, int maxHeight, String text)
{
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG| Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setAntiAlias(true);
textPaint.setColor(Color.BLACK);
//strength
if (strength.equals("bold"))
{
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
}
textPaint.setTextSize(textSize);
StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
c.drawPaint(paint);
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}

Android place ImageView on top of Canvas

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

Android : Can I draw a line without using bitmap width and height?

I added a button and a point in my project and put the point and button in the relative layout and set them same params like this code:
rllp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rllp.topMargin = 0 ;
rllp.leftMargin = 0 ;
rllp2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
rllp2.topMargin = 0 ;
rllp2.leftMargin = 0 ;
drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawingImageView.setImageBitmap(bitmap);
Paint paint = new Paint();
paint.setStrokeWidth(6);
drawingImageView.setLayoutParams(rllp);
canvas.drawPoint(0, 0, paint);
btn = (Button) findViewById(R.id.button1);
btn.setLayoutParams(rllp2);
but result is not what I wanted!! the button and the point is not in the same position ... and I was going to put both of them in the (0,0).
I think problem is here:
Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
.getDefaultDisplay().getWidth(), (int) getWindowManager()
.getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
but I don't have any solution for this ... how can I set their position in one place??
it's the picture of result :
http://i.stack.imgur.com/IWx7V.png
I want to put the point at the top-left corner of the button.
I think bitmap width and height make this ploblem... can I draw a line without using bitmap??

Getting Null bitmap from getDrawingCache() in android

This is my code. I am Getting null value. i was trying to find out the solution since morning, i have created layout by using xml file then i got the bitmap of view, but i want it dynamically and I have created layout programatically see bellow code.
My view is appearing fine but still getting getDrawingCache() null bitmap
LinearLayout mainlayout = new LinearLayout(getApplicationContext());
mainlayout.setOrientation(LinearLayout.VERTICAL);
mainlayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ImageView top= new ImageView(getApplicationContext());
ImageView bot= new ImageView(getApplicationContext());
Bitmap bottomImage = BitmapFactory.decodeResource(getResources(), R.drawable.collagebot);
Bitmap topImage =BitmapFactory.decodeResource(getResources(), R.drawable.collagetop);
top.setImageBitmap(topImage);
bot.setImageBitmap(bottomImage);
LinearLayout midLayout = new LinearLayout(getApplicationContext());
midLayout.setOrientation(LinearLayout.HORIZONTAL);
midLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Bitmap midlaybg =BitmapFactory.decodeResource(getResources(), R.drawable.bg);
Drawable middrow = new BitmapDrawable(getResources(),midlaybg);
midLayout.setBackground(middrow);
ImageView right= new ImageView(getApplicationContext());
ImageView left= new ImageView(getApplicationContext());
Bitmap rightImage = BitmapFactory.decodeResource(getResources(), R.drawable.fb);
Bitmap leftImage =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable rightdrow = new BitmapDrawable(getResources(),rightImage);
Drawable leftdrow = new BitmapDrawable(getResources(),leftImage);
right.setBackground(rightdrow);
left.setBackground(leftdrow);
right.setLayoutParams(new LayoutParams(150,150));
left.setLayoutParams(new LayoutParams(150,150));
left.setLayoutParams(new LinearLayout.LayoutParams(150, 150, 10));
midLayout.setPadding(50, 0, 0, 0);
midLayout.addView(right);
midLayout.addView(left);
mainlayout.addView(top);
mainlayout.addView(midLayout);
mainlayout.addView(bot);
Bitmap mainlaybg =BitmapFactory.decodeResource(getResources(), R.drawable.bg);
Drawable maindrow = new BitmapDrawable(getResources(),mainlaybg);
mainlayout.setBackground(maindrow);
mainlayout.setDrawingCacheEnabled(true);
mainlayout.buildDrawingCache();
// setContentView(mainlayout); // i dont want to setContentView
Log.e("ceche",""+mainlayout.getDrawingCache());// here i am getting null bitmap of mainlayout view
Bitmap imgb=mainlayout.getDrawingCache();
mainlayout.measure(MeasureSpec.makeMeasureSpec(
mainlayout.getLayoutParams().width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(
mainlayout.getLayoutParams().height,
MeasureSpec.EXACTLY));
mainlayout.layout(0, 0, mainlayout.getMeasuredWidth(),
mainlayout.getMeasuredHeight());
mainlayout.setDrawingCacheEnabled(true);
final Bitmap bitmap_ = Bitmap
.createBitmap(mainlayout.getDrawingCache());
mainlayout.setDrawingCacheEnabled(true);

Categories

Resources