android - dynamically changing the position of an imageview - android

i'm currently using the matrix method to implement drag and pinch zoom for an image (image A)
Now i want a part of image A to be clickable so i put another imageview (image B) over image A and set the layout_margins for it in the xml file.
My question is...is there a way to dynamically change my layout_margins as i'm scaling my image for drag and pinch zoom?
I guess the simpler question is...how do i dynamically set the layout_margins for an imageview?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/map"
android:scaleType="matrix" />
<ImageView
android:id="#+id/dundas_station"
android:layout_width="75px"
android:layout_height="75px"
android:layout_marginTop="337px"
android:layout_marginLeft="373px"
android:clickable="true"
android:src="#drawable/google_maps_icon"
android:scaleType="matrix"
/>
</RelativeLayout>
thanks!

Use following code snippet. Don forget to import
"android.widget.FrameLayout.LayoutParams" or marginal layout params.
ImageView image;
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(100,0, 0,0);
// OR
params.topMargin= 100;
image.setLayoutParams(params);

You use setMargins

Related

Image position is not able to set center with scale type matrix

I have image view in xml and I want to try to set image in center:
My current code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageview_fullscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="matrix"
android:src="#drawable/ic_launcher"
/>
</RelativeLayout>
I have added functionality for zoom in/out on image view -
http://stackoverflow.com/questions/15609845/add-limit-to-zoom-in-and-zoom-out-in-android
When I write above code then the image is not able to set in center with scale type matrix. The image is set in left top side from above code.
How can I set this to center with scale type matrix. ?
You can use
android:layout_gravity="center_vertical"
And set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
For the ImageView, add android:layout_centerInParent="true".
Try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageview_fullscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher" />
</FrameLayout>
try with this. Edited the answer.
You mentioned that, you are using view zoom.
If you are using multi-touch to zoom the ImageView,
please resize and center the drawable int he imageview during onMeasure method
of the View.
Follow this example to implement both single touch and multi touch on the image view, the image will auto re-size and fit-center.
Also, you can change the parent to FrameLayout to achieve scale many views.
http://www.mobile-open.com/2015/66671.html
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
zoomImageView.setLayoutParams(params);
RectF drawableRect = new RectF(0, 0, zoomImageView.getDrawable().getIntrinsicWidth(), zoomImageView.getDrawable().getIntrinsicHeight());
RectF viewRect = new RectF(0, 0, width, (height) - 100);
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
//matrix.setScale(-1,-1);
view.setScaleType(ImageView.ScaleType.MATRIX);
zoomImageView.setImageMatrix(matrix);
<ImageView
android:id="#+id/zoomImage"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher" />
first in your layout place the image in center, when everr the user touchs the imaageview for first time make the imageview width and height matchparent, and set Scaletype matrix, and apply the matrix.
here width and height are device width and heigth,we canculate using following snippet.
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
You can use on RelativeLayout
android:layout_gravity="centerl"
And set on ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

How can I make my image bigger on Android with XML?

I have an image on my ImageView but I don't know how to make it bigger with XML. My image it's the following:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="#drawable/image"/>
I tried changing android:layout_width and android:layout_height attributes but if I put them with a number (for example, 200dp width and 100dp height) the ImageView it's on the left of the screen and not center.
I want to center the image and makes it a little bit bigger than the original. I couldn't find anything to makes it bigger at the same time it is displayed on the center of the screen.
Is it possible? How can I do that?
Thanks in advance!
Use scaletype and add android:adjustviewbounds="true" in your image view section.
Keep the size you want as the width and height of the ImageView and change the gravity of its parent to center
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/img"
android:layout_marginTop="40dp"
android:src="#drawable/image"/>
</LinearLayout>
EDIT:
If this is what you want your layout to look like
then all you would need to do is add
android:layout_gravity="center_horizontal"
to your ImageView, and specify the width and height in dp
There are 3 attributes you can add to your imageview:
android:scaleType=""
android:scaleX=""
android:scaleY=""
ScaleType types can be seen here. Basically, you'll want to use CENTER_CROP.
scaleX and scaleY uses float as parameters, defining the scale for the two axis. See the reference here.
Both codes above works (Andrew Brooke and IntelliJ Amiya, Thanks!) but now I found a "trick" to avoid both of these methods. It's to create the ImageView using android:minWidth and android:minHeight so the final ImageView will be like:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/img"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:minWidth="100dp"
android:minHeight="70dp"
android:src="#drawable/image"/>
With that, you can center your ImageView with the size that you want.

Android - Place image in relativeLayout with proper size

I am working on a android card view. Here is the code that I have now:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:orientation="vertical"
android:background="#drawable/drawable_card_bg">
<ImageView
android:id="#+id/photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/test_img" />
....
and the UI looks like:
However this is not what I am looking for. I want the image to be placed on top of the card, with the top, right, left margins to be 10dip from the background card boarder.
Meanwhile, I want to keep the ratio of the image, so don't want to hard-code the width/height of the image view. I want the image width to fit the width of the background card, and the height should be adjusted according to the width.
How should I change the layout parameters to achieve this?
Thank you
Add android:adjustViewBounds="true" to your ImageView in xml in order to let the ImageView adjust its bounds.
It's very easy......... please do this thing. Replace your imageview code using this one
<ImageView
android:id="#+id/photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/test_img" />
If my answer will help you then support this.
Thanks!!!

How to programmatically add ImageView in LinearLayout without extra spaces?

I am trying to add ImageView programatically inside a LinearLayout, which has vertical orientation. My layout file is describe as the xml below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:id="#+id/mainView"">
<TextView android:id="#+id/tvTituloInformacao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/white"
android:gravity="center_horizontal"
android:shadowColor="#color/black_translucent"
android:shadowDx="2.0"
android:shadowDy="2.0"
android:shadowRadius="3.0"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<LinearLayout android:id="#+id/resourceContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="10dip"
android:padding="3dip"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_below="#+id/tvTituloInformacao"
android:background="#color/black_translucent2" />
</RelativeLayout>
And my java source code that adds the ImageViews to the LinearLayout is as follow:
for(Resource r : mUser.getPictures()) {
ImageView img = new ImageView(this);
img.setVisibility(View.VISIBLE);
img.setTag(r.getThumb60());
imageLoader.DisplayImage(r.getThumb60(), this, img);
((LinearLayout) tempFotosView.findViewById(R.id.resourceContainer)).addView(img);
}
What happens is that before and after each ImageView I am getting some extra spaces that should not be there, so the height of the images container increases a lot. One test that I did and got the expected result was not to use the imageLoader and instead I set the ImageResource programatically to a static image file. This way I didn't get any extra space.
The weird result is illustrated in the image below:
Is there a way to not add this extra space?
I think images you are loading in image views may have some extra spacing on top and bottom, and other assumption you are loading images in imageview, with different resolution than your screen-size, and setting scaleType of ImageView to centerInside, if it is the case please try using following by fixing height of the image view as well, and set scaleType to fixXY.
This happens when the image is wider than the screen size after the ImageView has been built. Android shrinks the image's dimensions accordingly to aspect ratio, but doesn't remove the allocated space around it earlier.
For a cleaner fix, try this code after your imageLoader.
((LinearLayout)findViewById(R.id.resource)).add(img);

Adding views on a custom GLSurfaceView

(note that I'm a total beginner in android programming)
I have a class that derives from GLSurfaceView.
What I would like is to place some views (image,text) on top of it.
I managed to get the text view to position properly by using textView.setPadding(300,0,0,0);
The problem is that I cannot get the image view to position properly. I've tried imageView.layout(), imageView.setPadding().
Here is the code:
ImageView imageView=new .... // Create and set drawable
// Setting size works as expected
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200,200);
imageView.setLayoutParams(lp);
surfaceView = new MySurfaceViewDerivedFromOpenGLSurface(...);
setContentView(surfaceView);
addContentView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(textView2, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(imageView, lp); // Add the image view
Is it even possible to position it properly this way without specifing the stuff in the XML file?
I saw an example in the sdk dev website that shows how to create views on the openGL surface view, but the problem is that I have a derived class and I don't know if I can specify it in the XML file (I have 0% experience in the XML, Eclipse handled everything for me so far).
You'll save a ton of headaches later by learning how to use xml layout. Specifying a layout for a custom view tripped me up too. This is how it works:
<view class="complete.package.name.goes.here.ClassName"
android:id="#+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</view>
So a really simple vertical layout for your app would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<view class="package.name.to.MySurfaceViewDerivedFromOpenGLSurface"
android:id="#+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</view>
</LinearLayout>
You can get a reference to anything in your layout files as long as there's an id:
ImageView myImageView = (ImageView ) findViewById(R.id.imageView1);

Categories

Resources