How to have drawings inside Android view scale to dimensions of view? - android

Im trying to make a custom view with some drawings in it. I want these drawings to take up the entire view but I also want the view to maintain a certain height/width ratio for these drawings.

In your onDraw(canvas) method, use canvas.getWidth() and canvas.getHeight() get to get the entire width and height of the View. Use this detail (like calculating the center of the View (width/2,height/2)) to draw your drawings. This way they will always fill the entire view and maintain the right aspect ratio.

Related

How to rotate a view in android and also adjust its height in respect to parent view

I am trying to rotate a view to any desired position from 0 to 360, It is a floating view and it is supposed to also go to the edges of the screen so I don't want to create a square box to rotate the view I want it just to take the place it needed in order to view the rotated view.
I am able to rotate the view but the issue is it is getting cropped by the parent and only the initial view is visible and the rest of it is being cropped. As shown in the image below:
Here is the code I am using:
public void rotateView(int angle) {
floatingView.setRotation(angle);
}
One Solution is I am thinking of using a diagonal value of the view as I rotate the view to set new height and width but not sure how to use it to just consider the width or height for the rotated part.
you can check this library it makes it easy for you: https://github.com/rongi/rotate-layout\
because it has the feature you looking for, The bounding box is rotated with the views inside.
Usage
implementation 'rongi.rotate-layout:rotate-layout:3.0.0'
<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>

what is the meaning of ImageView.ScaleType="MATRIX" : Android

I know about the matrix, its structure and working about image view's scale-type. but,
I can't find the exact meaning of ImageView.ScaleType="MATRIX". By declaring this, what exactly happens while drawing the image view.
When can I use ImageView.ScaleType="MATRIX" ?
How does it differ from FIT_END & FIT_START
I googled about it and also referred the official link but was not able to find the exact answer.
ImageView.ScaleType.MATRIX lets you use a Matrix to scale the image. You can set the Matrix using ImageView.setImageMatrix(matrix). So by declaring the scaleType to MATRIX you are saying that you want to use an explicit Matrix to do that.
You can use imageView.setScaleType(ImageView.ScaleType.MATRIX) whenever you want to customize the way the your image scales, rotates, etc. at your desire.
FIT_END and FIT_START are default types of scale. So, if you use FIT_END for instance, your image will maintain the original aspect ratio and it will align the result of the right and bottom edges of your image view. So basically, the difference is that FIT_END and FIT_START are "presets" while with MATRIX you declare that you want to use your own matrix to scale.
Read the docs for more info
As per my understanding, Use below details for each ImageView's ScaleType attributes
center
Displays the image centered in the view with no scaling.
centerCrop
Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.
centerInside
Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.
fitCenter
Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.
fitStart
Same as fitCenter but aligned to the top left of the view.
fitEnd
Same as fitCenter but aligned to the bottom right of the view.
fitXY
Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.
matrix
Scales the image using a supplied Matrix class. The matrix can be supplied using the setImageMatrix method. A Matrix class can be used to apply transformations such as rotations to an image.
The documentation you linked to provides the answer you are looking for.
Scale using the image matrix when drawing. The image matrix can be set using setImageMatrix(Matrix). From XML, use this syntax: android:scaleType="matrix"
Although you mentioned that you already know what a Matrix is in the context of graphics, I will explain briefly for the sake of other users who come across this question- A matrix can be used to manipulate the canvas when drawing graphics. In the case of an ImageView's matrix, you can use it to translate, flip, rotate, or otherwise move the image around on the screen.
when i can use ImageView.ScaleType="MATRIX"
You can use it whenever you want with an ImageView. You can call setScaleType() in your Java code to use a matrix scale type or you can add the android:scaleType="matrix" attribute in your layout XML.
how it differs from FIT_END & FIT_START
FIT_END and FIT_START both actually use a Matrix to scale your image. Both maintain the original aspect ratio and fit the image entirely within the view, but just align the result differently. End will align the scaled image to the end of the ImageView, whereas start will align the scaled image to the start of the ImageView
ScaleType="MATRIX"
A Matrix is constructed and scaled based on the user’s pinch gesture scale factor and then the ImageView set based on this scaled Matrix.
Please visit here Android ImageView ScaleType
Check the Effect.And https://guides.codepath.com/android/Working-with-the-ImageView

Android Custom View sizing

I am attempting to experiment in creating my own Views by subclassing "View." I want to simulate a new Button type class that will be drawn with the onDraw where I simply draw a rectangle from the canvas being passed in. I then add that custom View into the layout XML file, but I cannot control its size. For example, I set the width to "fill_parent" and the height to "wrap_content" but I don't know what the content of my View is. (BTW, the LinearLayout that my view is in is oriented to "vertical")
Can anyone give me some pointers in how to control the size of my View? For example, how does my custom view know what parameters were set in the XML file?
Android cannot detect the contents of your Canvas, so it assumes the contents are 0x0.
To do what you're looking for, you have to override onLayout or onMeasure (onLayout is specified by Android developers as a way to lay out children instead). You will also need a way to calculate the size of your contents.
First, start with a member variable called Point mSize = new Point(0, 0);. In a method other than onDraw, but called just as often, you will want to replace this with the size of your contents. If you draw a circle that has a radius of 25, do: mSize.x = 50; mSize.y = 50;.
Next, I'll introduce you to this answer (for onMeasure). It shows how to set the size of an item based on its parent's dimensions (divided by 2, in this case). Then, you can pull the size from mSize.x and mSize.y and apply those dimensions.
Finally, calling invalidate() after you measure your new mSize should force the View to lay itself out again, and adjust to your new parameters. This is the one part I'm unsure of in this regard.
NB: Also, you could call setLayoutParams using a fixed width and height once you calculate the size of your Canvas contents. Not sure of the performance implications for this, though.

How it is possible to stretch the image manually to our required pixel position

How it is possible to stretch the image manually to our required pixel position.in my android application i want to stretch my image to the specified position by dragging the end points of it.can anybody suggest me how to do this............
You can use an imageview with the attribute
android:scaleType="fitXY"
This will make the image to fit the bounds of the imageview.
To modify the image on click or touch, you will need add a ontouchlistener to the view. And on the based on the view getTop and and getLeft, you will need to set the layout params of the view to increase or decrease the size.
Or you will need to extend a view and override the onDraw method, in that method you can use the canvas.drawBitmap method to specify the size of the bitmap.
Either way you will need to use the onTouchListener.

Resize layout after an image transformation android

I have a layout in which I resize an image by doing a transformation with prescale. After the scale the image has the right size, but the layout container around it cuts its size of. I want the layout container arround it to show to complete image.
I just tried invalidate on the image, the container as well as requestLayout(). Any ideas?
Thanks
How are you transforming the image? If you are setting a matrix on an ImageView, you need to tell the ImageView to adjust its bounds to the size of the content. If you are scaling the image by setting a transformation on the Canvas, you must change the dimensions of the View yourself.

Categories

Resources