On Android: How to calculate row height prior to TextView creation? - java

Can anyone explain correlation between Paint.setTextSize() and TextView.setTextSize() ?
I have code like this:
Rect bounds = new Rect();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTextSize(48);
paint.getTextBounds("W", 0, 1, bounds);
int rowHeight = bounds.height();
LayoutParams params1 = new RelativeLayout.LayoutParams(300, rowHeight);
TextView tv = new TextView(getContext());
tv.setLayoutParams(params1);
**tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 48);**
but final text size in TextView is way bigger than rowHeight calculated.
I guess, my question is:
Is there any way to calculate rowHeight prior to TextView creation so that text fits by HEIGHT not width?
Thanks

Related

set bitmap in the center of screen in span

I am trying to put the bitmap in the center of my layout (Center-horizontal) but my bitmap always starts with the left and not centered. I assign the x value to setbound which center my image but shrink it rather the maintaining the ratio I selected.
private void setImageinText(Bitmap myBitmap){
myBitmap = Util.scaleBitmapToFitWidth(myBitmap, 1360, true);
Drawable d = new BitmapDrawable(getResources(), myBitmap);
Point outSize = new Point();
WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
display.getSize(outSize);
int[] widthAndHeight = new int[2];
widthAndHeight[0] = outSize.x;
int x = (widthAndHeight[0]- d.getIntrinsicWidth())/2;
SpannableString ssd = new SpannableString("\n \n");
d.setBounds(x, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ssd.setSpan(span, 1 , 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
texto.setTransformationMethod(null);
texto.getText().insert(texto.getSelectionStart(), ssd);
}
take ImageView below TextView and set it Centerhorizontal with Visibility GONE, then when you capture image set it in that ImageView and VISIBLE it.

Add Space between LinearLayouts

I'm trying to make a list of database items like the following..
for (int i=0; i < jArray.length(); i++)
{
JSONObject row = jArray.getJSONObject(i);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,100));
Button btn = new Button(MainActivity.this);
//btn.setText(row.getString("subject"));
btn.setText(String.valueOf(i));
btn.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.8));
File imgFile = new File(file_path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView iv = new ImageView(MainActivity.this);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageBitmap(myBitmap);
iv.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2));
ll.addView(btn);
ll.addView(iv);
LinearLayout sv = findViewById(R.id.sv_layout);
sv.addView(ll);
}
The result is like this:
But I want to have space between Linear Layouts and make Image height shorter to be equal with button height
How can I achieve this? Thank you,
Please Try this code
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
params.setMargins(0, 5, 5, 10);
ll.setLayoutParams(params);
you can add your specific margins in this line params.setMargins(0, 5, 5, 10);
Take button and image in linear layout with horizontal orientation. Set Button and Imageview height to wrap content. Now for gap between Linear Layouts give appropriate margin.
Set the margins in the layout params and pass them to your ImageView object.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2);
int dp = getDps(8);
layoutParams.setMargins(0,dp,0,dp);
iv.setLayoutParams(layoutParams);
Function getDps():
public int getDps(int sizeInDp){
float scale = getResources().getDisplayMetrics().density;
return (int) (sizeInDp*scale + 0.5f);
}
This looks like:

Imageview on a random position [duplicate]

This question already has answers here:
How can I dynamically set the position of view in Android?
(13 answers)
Closed 6 years ago.
I want myImageView to locate itself somewhere on a random position inside the screen, without pushing textviews away. I found this code in another thread, but I can't get it to work.
myImageView = (ImageView) findViewById(R.id.myImageView);
LinearLayout game = (LinearLayout)findViewById(R.id.game);
int width = game.getWidth();
int height = game.getHeight();
random = new Random();
int x = width;
int y = height;
int imageX = random.nextInt(x-50)+50;
int imageY = random.nextInt(y-100)+100;
myImageView.setX(imageX);
myImageView.setY(imageY);
You can give margin programmatically between certain range, may be you can use device width or height for range. Check this links for setting margin dynamically on run time and get screen dimension.
I Think first get programmatically device size then set image view width & height
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
hear you get width & height now set image view size
android.view.ViewGroup.LayoutParams layoutParams =myImageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
myImageView.setLayoutParams(layoutParams);
and if you still have a problem to change it's size try to put it inside of a LinearLayout and manipulate the imageView size using the layout params:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
myImageView.setLayoutParams(layoutParams);

I have an image which i would like to have grow everytime i click a button

Is there anyway i could scale an image to become 1.25x bigger everytime a button is pushed?
Basically, could someone give me some tips on how to do this? An explanation, a link, a tutorial, anything would be helpful :) thanks in advance
You should follow this.
http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
Here is code from this page. It creates a Matrix to perform operations(resize and rotate) and applies that matrix to create new BitMap.
You can add the code(with some modifications) on OnClickListener event of yout Button.
public class bitmaptest extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}
You can use Bitmap.createScaledBitmap() to resize image.
Refer to my articles on Image Processing to get some idea :): https://xjaphx.wordpress.com/learning/tutorials/

Android: Changing ImageView size is not working

I am trying to change my ImageView size, but I can't seem to change both - width and height. If I just change one of them, then all works correctly, but when I try to change both, only width is changed.
I have the size in dp: width 22 and height 38, and I convert them to px. Is there another way to do so? I have tried many other ways, but they don't seem to work either.
Could someone tell me, what I should do differently?
Here is my code:
public GridLayout gridLayout;
public GridLayout.Spec row = GridLayout.spec(0);
public GridLayout.Spec col = GridLayout.spec(3);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = (GridLayout) findViewById(R.id.gameGrid);
createBlock();
}
public void createBlock() {
ImageView block = new ImageView(this);
block.setImageResource(R.drawable.red);
if (gridLayout != null) {
gridLayout.addView(block, new GridLayout.LayoutParams(row, col));
int width_in_dp = 22, height_in_dp = 38;
final float scale = getResources().getDisplayMetrics().density;
int width_in_px = (int) (width_in_dp * scale + 0.5f);
int height_in_px = (int) (height_in_dp * scale + 0.5f);
ViewGroup.LayoutParams layoutParams = block.getLayoutParams();
layoutParams.width = width_in_px;
layoutParams.height = height_in_px;
block.setLayoutParams(layoutParams);
}
}
Have you tried playing with the block's scaleType? The default is fitCenter but I usually choose centerCrop or fitXY. Here's a good webpage that describes the differences.
You can set the scaleType through ImageView's setScaleType method.

Categories

Resources