How to center a Imageview on the screen? (with java code) - android

atm i'm using this code, but this code doesn't works for me, because the code is stretching the Image on the screen, i dont want that, i need that the image uses his real size (200x210)
FrameLayout fl = new FrameLayout(this.getApplicationContext());
splash = new ImageView(getApplicationContext());
splash.setImageResource(R.drawable.logo2);
fl.addView(splash);
fl.setForegroundGravity(Gravity.CENTER);
fl.setBackgroundColor(Color.BLACK);
setContentView(fl);
How to do it without making a giant image that it is using all the screen?

You have to specify scalType if you don't want imageview to stretch your image.
splash.setScaleType(ScaleType.CENTER);

use this properties of ImageView as shown here:
splash.setAdjustViewBounds(true);
and tell me if it is working.

Related

IMAGE VIEW ANDROID STUDIO

I have a doubt. While working on an App , I came across this query.
When an Imageview1(smaller size) is dragged and dropped on to Imageview2(bigger size), how to make Imageview1 (fit the area of imageview2), that is, enlarge the Imageview1 automatically to the size of Imageview2.
Instead of doing something so twisted, you can try a simpler solution like:
ImageView2.setImageDrawable(ImageView1.getDrawable());
ImageView1.setVisibility(INVISIBLE); //GONE could be used too, depending on the situation
Overwriting ImageView2 would involve many other operations like moving ImageView1 inside the layout, setting new LayoutParams, etc.
EDIT
Use this:
ImageView1.buildDrawingCache();
Bitmap src = ImageView1.getDrawingCache();
BitmapDrawable destDrawable = new BitmapDrawable(Bitmap.createScaledBitmap(src, ImageView2.getWidth(), ImageView2.getHeight(), false));
ImageView2.setImageDrawable(destDrawable);
In general, it is not a good practice to have views cover other views.
When you do the drag action try this
ImageView.ScaleType(FIT_XY);

PhotoView fit screen height maintain aspect ratio

I cant scaletype for my PhotoView that lets me pan side to side on an image while being full-screen, without cropping the sides using Photo.ScaleType.CENTER_CROP. How would I go about doing this?
This is what I want (where the user can pan side to side to view the whole image):
This is what I get (when using ScaleType.CENTER_CROP, which is closest to what I'm going for but because of the CROP, doesnt allow panning side to side.):
you can use a this ImageViewZoom.
if you don't want to use lib see the codes and try to scale canvas
I fixed it by using a PhotoViewAttacher like this:
ImageView imageView = (ImageView) findViewById(R.id.preview);
PhotoViewAttacher photoViewAttacher = new PhotoViewAttacher(imageView);
photoViewAttacher.setScaleType(ImageView.ScaleType.CENTER_CROP)
and then using an ImageView (id.preview) with the ScaleType in the XML set as Matrix. (otherwise it wont work)

Android ImageView: Replace Image (Drawable) While keeping View Size

In Short
I have an ImageView with size set to wrap_content.
The image has a drawable already set, and everything looks great.
Now, I want to change the drawable, while forcing the ImageView to keep it's size.
Explanation
Well, the easy answer is of course to set the width & height to a fixed size.
But in my case, I am copying an ImageView + layout from a Google Map to add my custom map button. It works well, because I have an icon with same size.
Now I want to change the icon for a moment (loading icon btw), but because the Drawable source is larger, it changes the ImageView size accordingly.
ScaleType doesn't work as the width and height are not fixed.
What I've Tried
1. Setting the new Drawable bounds using getBounds on the old one.
2. setRight() on the ImageView with the right bound of the existing map button (the original one which doesn't change).
(left is already fixed, it's inside a RelativeLayout).
Some (working) Code of how I created the new button
ImageView newBtn = new ImageView(context);
newBtn.setAdjustViewBounds(false);
newBtn.setBackgroundResource(R.drawable.map_btn_background);
newBtn.setScaleType(ScaleType.CENTER_INSIDE);
newBtn.setImageResource(imageResId);
newBtn.setClickable(true);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationBtn.getLayoutParams();
rlp = copyRelativeLayoutParams(rlp);
//add to Google's map button layout
btnContainer.addView(newBtn, rlp);
Some (not working) Code of how I change the drawable.
This is the point were I want the ImageView to keep it's size.
perfectlyMadeNewMapButtonImageView.setImageDrawable(mLoadingDrawable);
//mLoadingDrawable.setBounds(
// perfectlyMadeNewMapButtonImageView.getDrawable().getBounds());
// not working

How to Make ImageView to Wrap its Contents?

I'm a starter Android learner and I think that this is a very easy question but I can't find it anywhere. I have added an ImageView like this:
//....
iv = new ImageView(context);
iv.setBackground(getResources().getDrawable(drawable));
iv.setAdjustViewBounds(true);
RelativeLayout.LayoutParams imajpara=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
iv.setMaxWidth(100);
iv.setMaxHeight(100);
iv.setLayoutParams(imajpara);
iv.setScaleType(ScaleType.CENTER_INSIDE);
//....
But the ImageView (a ball picture in this case) fits the entire screen, even though the image (.png) is 100x100 pixels. In the code you see my attempts to fix this but none of them worked. The imageview is in a RelativeLayout.
So how can I make the ImageView resize itself according to the image's dimensions?
Use setImageDrawable() instead of setBackground().
Instead of indicating "WRAP_CONTENT" in the constructor of your Layoutparams, you can set directly the right dimensions.

How to stop OpenGL ES drawing background color on Android?

Now I want to insert a ImageView behind the GLSurfaceView.But the GLSurfaceView always draws background color so I can't see the ImageView.I tried glClearColor(0,0,0,0) but it doesn't work.What should I do?Please help me!
Thank you very much.
If I remember correctly you should be able to adapt this
Overlapping Views in Android
to your needs
(relative layout and android:background="#0000")
since GLSurfaceView is a View it should work.
I fixed it.
using
gLSurfaceView.setEGLConfigChooser(8,8,8,8,16,0);
gLSurfaceView.setRenderer(this);
RelativeLayout rl = new RelativeLayout(this);
BgLayout bgl = new BgLayout(this);
gLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
gLSurfaceView.setZOrderOnTop(true);
rl.addView(bgl);
rl.addView(gLSurfaceView);

Categories

Resources