Set the text above the image in pdf cell using itext - android

I created the pdf using iText. In that, how to set the background image to the particular PdfCell and place the text in that same cell..
I set the background image to that cell by:
cell.addElement(image_green_left);
In case of both text and image, it show only image and text was hidden.
cell.addElement(image_green_left);
cell.addElement(new Paragraph("Test 3"));
table.addCell(cell);
How to show both text and image?

Create your own implementation of a PdfPCellEvent, for instance:
class ImageBackground implements PdfPCellEvent {
protected Image image;
public ImageBackground(Image image) {
this.image = image;
}
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
Image.scaleToFit(position.getWidth() , position.getHeight());
Image.setAbsolutePosition(position.getBottom(), position.getLeft());
cb.addImage(image);
}
}
Now when cell is the PdfPCell with the text and bgImage the path to your image, do this:
Image image = Image.getInstance(bgImage);
cell.setCellEvent(new ImageBackground(image));
Now, you'll have an image that acts as the background of the cell.

Related

Limit Image to size of containing view (xamarin.android)

I'm trying to create a chess board using a GridLayout in Xamarin.Android. Each View in the GridLayout will be an image view depicting a piece (if the square contains a piece). Before adding the image resource, the board displays fine. But as soon as an image is added, it pushes the size of the ImageView to be much larger than desired.
Here is the code I am using. The following method is run for each square on the board and creates the ImageView and returns it to be added to the GridLayout:
public View BuildSquare(Square squareModel)
{
SquareView rtn = new SquareView(this.Activity, squareModel); //SquareView extends ImageView
rtn.UpdateBackgroundColor(); //set square color
if (squareModel.Piece != null)
{
rtn.DrawPiece();
}
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
//set weight of each row and column to ensure equal size
param.RowSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
param.ColumnSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
rtn.LayoutParameters = param;
return rtn;
}
and the DrawPiece() method is as follows, which sets the image resource:
public void DrawPiece()
{
if (squareModel.Piece == null) return;
string key = squareModel.Piece.GetPieceNotation();
int resource = -1;
//get the image resource corresponding to the piece on the ssquare
if (AndroidConstants.PieceResources.TryGetValue(key, out resource))
{
this.SetImageResource(resource);
}
this.SetScaleType(ScaleType.FitXy);
}
When I comment out the rtn.DrawPiece() line, the board displays correctly as shown below:
However, when the line is included and the image resource is set, it displays as follows:
What you are seeing here is the top left square on the board hugely inflated to accommodate the image. I want the image to be scaled to fit in the square in the first image above. I have tried with different ScaleTypes but it seems to make no difference. Is this something to do with using the GridLayout? Or is there something else going on here that I'm missing?
I think you need to set the height and width to your SquareView.
For example:
public View BuildSquare(Square squareModel)
{
SquareView rtn = new SquareView(this, squareModel); //SquareView extends ImageView
rtn.UpdateBackgroundColor(); //set square color
if (squareModel.Piece ==0)
{
rtn.DrawPiece();
}
//Set the height and width
GridLayout.LayoutParams param = new GridLayout.LayoutParams(new ViewGroup.LayoutParams(50, 50));
//set weight of each row and column to ensure equal size
param.RowSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
param.ColumnSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
rtn.LayoutParameters = param;
return rtn;
}
And the result:

How to set image to an image button

I have taken an image button with the following code:
skin = new Skin(Gdx.files.internal("glassy-ui.json"));
button2 = new ImageButton(skin);
myTexture = new Texture(Gdx.files.internal("PlayButton.png"));
myTextureRegion = new TextureRegion(myTexture);
myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
button2.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("PlayButton.png"))));
button2.addListener(new InputListener(){
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
game.dispose();
game.setScreen(new GameScreen(game));
return true;
}
});
stage.addActor(button2);
I want to set image in imagebutton as drawable,Refer this link for further understanding . How can I do that? What I also want to do is that there is any visible reaction to an users input like changing its size etc..
By the way, the image is round.
ImageButtonStyle imgBtnStyle=new ImageButtonStyle();
imgBtnStyle.imageUp=new SpriteDrawable(spriteUp);
imgBtnStyle.imageDown=new SpriteDrawable(spriteDown);
ImageButton imgBtn=new ImageButton(imgBtnStyle);
Add Image button to stage. Do whatever animation(actions) you want to do, on this ImageButton Actor.
You can add the image in imagebutton like,
then try this
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
yourimagebutton.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
To fill the image in button, set Scale type FitXY.
button2.setScaleType(ImageView.ScaleType.FIT_XY);
or To put the image in center of the button, set Scale type FitCenter.
button2.setScaleType(ImageView.ScaleType.FIT_CENTER);

Is there any possible to convert Edittext what ever the enterd text only into image in android

[ [1]: http://i.stack.imgur.com/Lnz0U.png!][1]I have written code for converting edittext to image but i want only text what ever i entered in edittext only that text convert into image(not over all edittext). please help me
Typeface font = Typeface.createFromAsset(getAssets(), "barbatrick.ttf");
et.setTypeface(font);
et.setCursorVisible(false);
et.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(et.getDrawingCache());
ImageView iv=(ImageView)findViewById(R.id.imageView1);
iv.setImageBitmap(bmp);
public Bitmap getViewImage(View view) {
view.buildDrawingCache();
return view.getDrawingCache();
}

How to save image that in EditText to SharedPreferences and read it out?

I write a richText Edit, I want to insert some image into EidtText use these code:
public void insertImage(int i,Context context){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.notes_icon);
if(i==1){
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.smileyface);
}
ImageSpan imageSpan = new ImageSpan(bitmap);
SpannableString spannableString = new SpannableString("face");
spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
EditText contentText = (EditText) findViewById(R.id.content);
contentText.append(spannableString);
}
That it work , the image display in EditText. But I want to save content to SharedPreferences. I use Html.toHtml , I found the img tab is and save to SharedPreferences. So when I read it out and display to EditText the image became white box.
How to give some value behind imge scr when Html.toHtml? because there are lots of image, I want to recognition from different value.

Android: showing default image in gallery's ImageView while actual image is downloaded

I believe this is pretty trivial but I can't get it to work.
I want to display a default image in gallery elements (ImageViews) while their actual image is being fetched from the net.
Right now, nothing is shown for an ImageView which its image has yet to arrive. Once it arrives it is immediately shown.
What I tried is right after the instantiation of the ImageView to call its setImageResource function like so:
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
But it doesn't seem to work. Below is the full getView() function.
Any help is appreciated.
Thanks.
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
// if the drawbale is in the buffer - fetch it from there
Drawable bufferedImage = DataManager.getInstance().getImagesBuffer()[position];
if (bufferedImage != null){
i.setImageDrawable(bufferedImage);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
// if drawable is not in buffer - fetch it from the net via AsyncImageLoader
else
{
String imageUrl = DataManager.getInstance().getImageBufferInstance().getImageUrl(position);
Drawable downloadedImage = AsyncImageLoader.getInstance().loadDrawable(imageUrl, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
if (imageDrawable == null)
{
imageDrawable = getResources().getDrawable(R.drawable.icon);
}
i.setImageDrawable(imageDrawable);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
});
i.setImageDrawable(downloadedImage);
}
i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInstance().getScreenWidth() / 2,
Utils.getInstance().getScreenHeight() / 2));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return i;
}
Fix your indentation... If I'm reading that correctly, you're only setting the temporary drawable icon in the imageLoaded callback of your (not shown) AsyncImageLoader, which I assume means it's then only being set after the image downloads and is then immediately overwritten with the downloaded image. Try moving the placeholder-setting code into your else block outside the callback.

Categories

Resources