I need to change a image width
original image like the first line below:
but when I change the width it keeps the ratio resize the original image
I need to do something like the third image
can change the width and don't modify original image
How can I do this through Java or XML in android?
xml code:
<ImageView
android:id="#+id/fill"
android:layout_width="150dp"
android:layout_height="32dp"
android:layout_alignLeft="#+id/BGfill"
android:layout_centerVertical="true"/>
Related
I've searched this up many times, but couldn't find an answer which worked for me. So basically, my issue is that I get an image from a backend provider and should display it in the ImageView with an opacity gradient of 100% at the bottom and 0% at the top so the image behind it slowly shows up. Applying a transparent gradient overlay doesn't work since the pixels to be transparent are the actual image I get from the backend service. Any help or ideas would be appreciated! The UI is in XML not Compose
This is the xml for the view I want to add an opacity gradient to. The image comes as a bitmap variable I assign to it's background currently:
<ImageView
android:id="#+id/bottom_container_background"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_container_height"
android:adjustViewBounds="true"
android:alpha="0.7"
android:src="#drawable/cardview_gradient_shape"
app:layout_constraintBottom_toBottomOf="#+id/iv_epg"
app:layout_constraintEnd_toEndOf="#+id/iv_epg"
app:layout_constraintStart_toStartOf="#+id/iv_epg"
tools:src="#drawable/cardview_gradient_shape" />
val croppedBlurredThumbnail = BitmapDrawable(thumbnailBottomBackgroundBlurred)
cardLayout.bottom_container_background.background = croppedBlurredThumbnail
I'm creating an app that uses an ImageView in the main activity that covers the whole width of the screen, the height should fit the image's resolution.
Here's my xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:adjustViewBounds="true"
android:id="#+id/bgImage"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bg_home_blue"/>
</RelativeLayout>
Here's what my layout looks like:
Everything seems to work fine but I don't know where these paddings are coming from, I've tried to change the scaleType but nothing seems to work.
Edit:
If I put instead of src just a blue background, the paddings go away and it fills the whole width (than I need to set a specific height as well)
I guess your image is png and have that space around it, even when your scaleType is FitXY it doesn't mean that it will remove the space around the png file, check that out and let us know what happened, or at least upload your image file
I would like to fill the screen with a rotated ImageView. The question of how to fill the screen with an ImageView and how to rotate an ImageView has been answered seperately mutlipe times on the site. My XML code looks as follows.
<ImageView
android:id="#+id/video_frame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:rotation="270"
/>
The problem is that the rotation happens after the image has been fitted with no rotation applied and I end up with an image that does not fill the screen.
I could of course just rotate the images (and remove the ImageView rotation) as follows:
Matrix mMatrix = new Matrix();
mMatrix.postRotate(-90);
ImageView video = (ImageView)findViewById(R.id.video_frame);
Bitmap frame = BitmapFactory.decodeFile("path/to/file.jpg");
Bitmap frame2 = Bitmap.createBitmap(frame, 0, 0, frame.getWidth(), frame.getHeight(), mMatrix, true);
video.setImageBitmap(frame2);
Here the problem is that the application is realtime and I am serving multipe frames per second (20+). The rotation per frame seems to be quite heavy and even if in this case the images fill the screen, the result is lagging and not as responsive as without the matrix rotation.
My question would therefore be if I could solve this issue with XML only or in any other way without rotating every image I want to display?
You can use RotateLayout and put your ImageView in RotateLayout and pass your rotation in setAngle() method of RotateLayout, So It rotates only view not Image or we can say bitmap it's much faster than any other rotation of Images.
you can add its dependency in Gradle file.
Usage
In your layout file add
<com.github.rongi.rotate_layout.layout.RotateLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:angle="90"> <!-- Specify rotate angle here -->
<YourLayoutHere
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</YourLayoutHere>
</com.github.rongi.rotate_layout.layout.RotateLayout>
Voila! Your layout will be rotated 90 degrees.
Download
compile 'rongi.rotate-layout:rotate-layout:3.0.0'
Features
Handles all touch events in a correct way. You press the same button you touch!
Layout measures itself in a correct way. This means that if the original view is 50x100, then 90 degrees rotated it will measure itself as 100x50 and can fit in another layout with this dimensions.
RotateLayout Link
I have a background on my android app with is a png image. It is 800 x 480 px in size.
It is being stretched width wise and hence does not look as I intend it to. As the image is actually the background of the app and not just an imageView I was unable to use the android:scaleType="centerCrop" to get the image to scale equally.
I need the image to fill the background equally.
I have also tried using NinePatch drawable but the results were not good at all on this - The image became blurred or was OK when being stretched vertically but not width wise.
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
Is there any other command I can use to get the background to display correctly?
to resolve this use draw9patch
android-sdk/tools/draw9patch
https://developer.android.com/tools/help/draw9patch.html
use imageview
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_image"
android:scaleType="fitCenter"
/>
check this also Link
ImageView.ScaleType CENTER_CROP
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
I am trying to make a small image appear on my screen. I want it to be a small square. With the below code, it shows up as a long flat rectangle. I have attached a picture of what it looks like below.
java code
ImageView q1Image = (ImageView)findViewById(R.id.q1Image);
q1Image.setScaleType(ScaleType.FIT_XY);
xml code
<TableRow
android:id="#+id/row4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/q1Image"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight=".1"
android:textSize="8sp"
android:gravity="center_horizontal" />
EDIT
If I make the width and height equal to 50dp each, the image gets bigger but is still a flat looking rectangle.
Your scaling the Image wrong. Here are all scaletypes:
Top row (l-r) center, centerCrop, centerInside.
Bottom row (l-r): fitCenter, fitStart, fitEnd, fitXY.
You probably need fitCenter:
q1Image.setScaleType(ScaleType.FIT_CENTER);
FIT_XY will stretch your image to fit all yout ImageView.
Use scale type fitCenter to preserve aspect ratio by placing the image in center if your ImageView
q1Image.setScaleType(ScaleType.FIT_CENTER);
Refer to http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
For other options.
Without the full layout XML this is just speculation, but seeing as you don't appear to have fixed the problem by changing the scale type, here are some other suggestions:
Wouldn't it be better if the layout_weight of the ImageView was zero?
Are you specifying android:stretchColumns on the TableLayout?
The image looks like it is being stretched to match the width of the text below. How many columns have you got in your table? It looks like the text in the other rows is in column 0 (as is the image in your first row) and the text in the first row is in column 1. If you want to leave column zero blank for the other rows, you need to specify android:layout_column on the text views.