set bitmap in the center of screen in span - java

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.

Related

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 can't draw more than one circle on an image

I have a problem. I need to draw some circles on an image, but I don't know why, it only draws one circle. I put the code below to explain it better:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.previsualizacion);
myImage = (ImageView) findViewById(R.id.imageView1);
ctx = getApplicationContext();
//Obtenemos la informaciĆ³n del layout anterior y la asignamos al tamaƱo del paso
data = getIntent();
myBundle = data.getExtras();
presasX = myBundle.getStringArrayList("arrayPixelX");
presasY = myBundle.getStringArrayList("arrayPixelY");
colores = myBundle.getStringArrayList("arrayColores");
Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
bitMap = bitMap.copy(bitMap.getConfig(), true);
Canvas canvas = new Canvas(bitMap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL_AND_STROKE);
//paint.setStrokeWidth(0.5f);
paint.setAntiAlias(true);
myImage.setImageBitmap(bitMap);
//changed set image resource to set image background resource
myImage.setBackgroundResource(R.drawable.pared);
for(int i = 0; i < presasX.size(); i++)
{
if(colores.get(i).equals("1"))
{
paint.setColor(Color.GREEN);
}
else if(colores.get(i).equals("2"))
{
paint.setColor(Color.RED);
}
else if(colores.get(i).equals("3"))
{
paint.setColor(Color.YELLOW);
}
canvas.drawCircle(Float.parseFloat(presasX.get(i)) * myImage.getWidth() / 100, Float.parseFloat(presasY.get(i)) * myImage.getHeight() / 100, 3, paint);
}
//invalidate to update bitmap in imageview
myImage.invalidate();
}
I have on some ArrayList the coords of the points I want to draw, also this coords are relative to an image, so I have to multiply that coords for the imagewidht and imageheight.
It only draws one green circle, but I need to draw (in that chase) five green points and four red points.
I put the arraylist info too:
presasX = [49.583333333333, 80.833333333333, 13.541666666667, 4.5833333333333, 77.708333333333, 49.583333333333, 4.5833333333333, 95.208333333333, 96.875]
presasY = [49.722222222222, 5, 22.5, 20.277777777778, 33.888888888889, 49.722222222222, 20.277777777778, 54.722222222222, 61.666666666667]
colores = [2, 2, 2, 2, 2, 1, 1, 1, 1]
Thanks a lot!
This is because layout has not inflated fully and therefore myImage.getWidth() and getHeight() are returning 0. So in effect the center of all your circles is the same (top left corner of the ImageView) and the next one is overwriting the previous one.
For actual width and height you can use ViewTreeObserver.addOnGlobalLayoutListener or programmatically create the ImageView with required dimensions or create a custom view extending the standard ImageView and implement its onDraw() etc.

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

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

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/

Categories

Resources