Set Width and Height from Bitmap or ImageView to CustomView - java

I'm trying to get Width and Height of Bitmap and set them as Layout Parameter of CustomView
JAVA :
private void setBg() {
drawView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
drawView.getLayoutParams().height = orginalBitmap.getHeight();
drawView.getLayoutParams().width = orginalBitmap.getWidth();
relativeLayout.addView(drawView);
}
XML :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom"
android:layout_below="#+id/clear">
<ImageView
android:id="#+id/fullimg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal|center_vertical"
android:adjustViewBounds="true" />
<RelativeLayout
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal|center_vertical"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" />
</RelativeLayout>
But the code above doesn't work, when I run the app I get this
the emojis, for example, should stop right in the corner of image view but instead of that it still appears in the two empty spaces.

Use this method instead , you have to set the layout params this way and then add them to your drawView
private void setBg(){
android.view.ViewGroup.LayoutParams params = drawView.getLayoutParams();
params.width = orginalBitmap.getWidth();
params.height = orginalBitmap.getHeight();
drawView.setLayoutParams(params);
relativeLayout.addView(drawView);
}

Here's what I've done, and it works well
mDensity = getResources().getDisplayMetrics().density;
actionBarHeight = (int) (110 * mDensity);
bottombarHeight = (int) (60 * mDensity);
viewWidth = getResources().getDisplayMetrics().widthPixels;
viewHeight = getResources().getDisplayMetrics().heightPixels - actionBarHeight - bottombarHeight;
viewRatio = (double) viewHeight / (double) viewWidth;
bmRatio = (double) orginalBitmap.getHeight() / (double) orginalBitmap.getWidth();
if (bmRatio < viewRatio) {
bmWidth = viewWidth;
bmHeight = (int) (((double) viewWidth) * ((double) (orginalBitmap.getHeight()) / (double) (orginalBitmap.getWidth())));
} else {
bmHeight = viewHeight;
bmWidth = (int) (((double) viewHeight) * ((double) (orginalBitmap.getWidth()) / (double) (orginalBitmap.getHeight())));
}
final RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lparams.addRule(RelativeLayout.CENTER_IN_PARENT);
lparams.height = bmHeight;
lparams.width = bmWidth;
drawView.setLayoutParams(lparams);

Related

Android ACTION_MOVE image set image scrolling bounds outside its parent

I implemented ACTION_MOVE for ImageView which is bigger then its parent (FrameLayout). Now I'm trying to add some code to stop movement (both drag and pinch zooming) when at least one of Image edges is inside its parent (so it won’t show background of a parent).
I used the code like in this question.
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) { //movement of first finger
matrix.set(savedMatrix);
float newX = event.getX() - start.x;
float newY = event.getY() - start.y;
if (view.getLeft() >= -392){
matrix.postTranslate(newX, newY);
}
}
else if (mode == ZOOM) { //pinch zooming
float[] f = new float[9];
float newDist = spacing(event);
if (newDist > 5f) {
matrix.set(savedMatrix);
scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
matrix.getValues(f);
float scaleX = f[Matrix.MSCALE_X];
float scaleY = f[Matrix.MSCALE_Y];
if(scaleX <= MIN_ZOOM) {
matrix.postScale((MIN_ZOOM)/scaleX,
(MIN_ZOOM)/scaleY, mid.x, mid.y);
}
else if(scaleX >= MAX_ZOOM) {
matrix.postScale((MAX_ZOOM)/scaleX,
(MAX_ZOOM)/scaleY, mid.x, mid.y);
}
}
break;
I tried many things, and I know similar questions had been asked already but non of them fit my need so far.
My best guess was to add code below but it looks like mImageView.getLeft(), mImageView.getRight() etc returns parents edges not the images.
In onCreate():
DisplayMetrics displayMetrics =
getApplicationContext().getResources().getDisplayMetrics();
float screenWidth = displayMetrics.widthPixels;
float screenHeight = displayMetrics.heightPixels;
and in case MotionEvent.ACTION_MOVE
if (mImageView.getLeft() > 0 ||
mImageView.getRight() < screenWidth) {
matrix.postTranslate(0, newY);
break;
}
else if (mImageView.getTop() > 0 ||
mImageView.getBottom() < screenHeight) {
matrix.postTranslate(newX, 0);
break;
}
else {
matrix.postTranslate(newX, newY);
}
And this is my layout:
<RelativeLayout
android:id="#+id/imageViewContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/mag_background">
<TextView/>
<ImageView/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageViewHeadingLabel">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
android:scaleType="matrix"
android:layout_gravity="center_vertical|center_horizontal"
android:contentDescription="image to zoom"/>
</FrameLayout>
</RelativeLayout>
Thanks in advance for your help!
I found solution hire.
I reverse the checks so instead of not letting image to move outside layout I not letting image bounds to be move inside layout like this:
if (mode == DRAG) { //movement of first finger
matrix.set(savedMatrix);
matrix.getValues(matrixValues);
matrixX = matrixValues[2];
matrixY = matrixValues[5];
width = matrixValues[0] * (view.getDrawable().getIntrinsicWidth());
height = matrixValues[4] * (view.getDrawable().getIntrinsicHeight());
dx = event.getX() - start.x;
dy = event.getY() - start.y;
//if image will go inside left bound
if (matrixX + dx > 0){
dx = -matrixX;
}
//if image will go inside right bound
if(matrixX + dx + width < view.getWidth()){
dx = view.getWidth() - matrixX - width;
}
//if image will go inside top bound
if (matrixY + dy > 0){
dy = -matrixY;
}
//if image will go inside bottom bound
if(matrixY + dy + height < view.getHeight()){
dy = view.getHeight() - matrixY - height;
}
matrix.postTranslate(dx, dy);
}
However if you move zoomed image to the corner and then zoom out, it will show parent background until next touch event so problem is not fully solved.

Android view dynamic size [duplicate]

This question already has answers here:
onMeasure custom view explanation
(3 answers)
Closed 7 years ago.
I have a layout with several views and one of these views have to be scaled based on screen size. As of now I have it set to 250dp as it looks good on a 5.1 inch device, but when used on smaller devices it isnt as good any more. I can't use wrap content as it will make the looks of it very bad and the size of buttons too small, so it has to be 1/3rd of the width of the screen. Is it possible to do that in the layout code, or does it have to be doen in java code?
Is it possible to do that in the layout code
Yes it is. It is pretty simple by using android:layout_weight and android:weightSum attributes of LinearLayout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="horizontal">
<View
android:id="#+id/thirdView"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
You need to override onMeasure(...) then do your view resizing login inside this function. Here is the perfect explanation and solution.
Some reference code from this link goes below...
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = 100;
int desiredHeight = 100;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
//Measure Width
if (widthMode == MeasureSpec.EXACTLY) {
//Must be this size
width = widthSize;
} else if (widthMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
width = Math.min(desiredWidth, widthSize);
} else {
//Be whatever you want
width = desiredWidth;
}
//Measure Height
if (heightMode == MeasureSpec.EXACTLY) {
//Must be this size
height = heightSize;
} else if (heightMode == MeasureSpec.AT_MOST) {
//Can't be bigger than...
height = Math.min(desiredHeight, heightSize);
} else {
//Be whatever you want
height = desiredHeight;
}
//MUST CALL THIS
setMeasuredDimension(width, height);
}
You could have a look at the PercentRelativeLayout from the Percent Support Library. This is a RelativeLayout in which you can specify it's Views sizes in percentages, e.g. set its width to 33%.
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
app:layout_widthPercent="33%"
app:layout_heightPercent="33%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%" />
</android.support.percent.PercentRelativeLayout>

EditText leftView image fit the size of the EditText Android

I want to put an Icon inside an EditText so I did like this:
android:drawableLeft="#drawable/search_icon"
The problem is that the image is bigger than the EditText so the EditText becomes bigger in order to fit the image size. What I want is the opposite: The image should resize in order to fit the EditText height, is it possible?
I (desperately) tried with:
android:adjustViewBounds="true"
But obviously it doesn't work.
Is there any way to do so?
Try
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dd80aa"
android:orientation="vertical">
<RelativeLayout
android:layout_width="363dp"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text here"
android:marginLeft="50dp"/>
<ImageView
android:layout_width="wrap_contenta"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="right"
android:src="#android:drawable/ic_menu_search"/>
</RelativeLayout>
</LinearLayout>
You can wrap in RelativeLayout , on the left set an ImageView with layout_height match_parrent
You can simply do this by fixing the height of the edit text.
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:ems="10"
android:drawableLeft="#drawable/beautiful"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
you can use below code for resizing
Drawable d = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
byte[] ba;
do {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
Log.e("BEFORE REDUCING",
bitmap.getHeight() + " " + bitmap.getWidth() + " "
+ bitmap.getRowBytes() * bitmap.getHeight());
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
Log.e("After REDUCING",
bitmap.getHeight() + " " + bitmap.getWidth() + " "
+ bitmap.getRowBytes() * bitmap.getHeight());
ba = bao.toByteArray();
if ((ba.length / 1024) >= 650) {
bitmap = Bitmap.createScaledBitmap(bitmap,
(int) (bitmap.getWidth() * 0.95),
(int) (bitmap.getHeight() * 0.95), true);
}
Log.e("BYTE LENGTH", "" + ba.length / 1024);
} while ((ba.length / 1024) >= 650);

Android Animation : Is there some reference website for Android animation effect, how can I create this logo animation?

Here is the animation:
https://www.youtube.com/watch?v=YqFI4r4VYgg&feature=youtu.be
Right now I am doing this
<ImageView
android:id="#+id/zweet_logo"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/description"
android:src="#drawable/zweet_logo" />
<RelativeLayout
android:id="#+id/appLoading"
style="#style/GenericProgressBackgroundAppLoading"
android:layout_width="33dp"
android:layout_height="33dp"
android:layout_below="#+id/zweet_logo"
android:layout_centerHorizontal="true" >
<ProgressBar style="#style/GenericProgressIndicatorAppLoading" />
</RelativeLayout>
logo_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator" >
<scale
android:duration="700"
android:fillBefore="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
<translate
android:duration="700"
android:fromXDelta="-200"
android:fromYDelta="-200" />
MainActivity is like
/** Define and start logo animation */
Animation logoMoveAnimation = AnimationUtils.loadAnimation(this,
R.anim.logo_animation);
ImageView zweetLogo = (ImageView) findViewById(R.id.zweet_logo);
zweetLogo.startAnimation(logoMoveAnimation);
That's so basic.
--
Right I have the iOS code:
- (void)animateZweetLogo
{
CGRect startFrame = self.zweetLogoImageView.frame;
MTTimingFunction timingFuction = kMTEaseOutBounce;
CGFloat duration = kAnimateZweetLogoAnimationDuration;
CGFloat endY = startFrame.origin.y;
CGFloat endX = startFrame.origin.x;
CGFloat endScale = 1;
CGFloat endRotation = 0;
CGFloat endAlpha = 1;
[UIView mt_animateViews:#[self.zweetLogoImageView]
duration:duration
timingFunction:timingFuction
options:MTViewAnimationOptionBeginFromCurrentState
animations:^{
//self.zweetLogoImageView.mt_animationPerspective = -1.0 / 500.0;
CGRect r = self.zweetLogoImageView.frame;
r.origin.x = endX;
r.origin.y = endY;
// scale
CGFloat h = startFrame.size.height;
CGFloat w = startFrame.size.width;
CGFloat hh = h * endScale;
CGFloat ww = w * endScale;
r.size.height = hh;
r.size.width = ww;
r.origin.y -= (hh - h) / 2.0;
r.origin.x -= (ww - w) / 2.0;
self.zweetLogoImageView.frame= r;
// alpha
self.zweetLogoImageView.alpha = endAlpha;
// rotation
CGFloat radians = mt_degreesToRadians(endRotation);
self.zweetLogoImageView.layer.transform = CATransform3DMakeRotation(radians, 0, 1, 0);
} completion:^{
[self performSelectorOnMainThread:#selector(resumeLoading) withObject:nil waitUntilDone:YES];
}];
}
Is there some library of animations that I can use to do something that look like the iOS code? Or there is no other than just code and test it in Java in a long and iterative process?
Check http://www.mybringback.com or http://thenewboston.org/list.php?cat=6.
or developer docs at developer.android.com are pretty useful.

stretch image using canvas android

Hello I have two images and i am merging that images using canvas
here is the below code
Intent intent = getIntent();
//bm is the image get from camera
bm = BitmapFactory.decodeFile(intent.getStringExtra("getdata"));
//this is simple white text image
bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.txt_made_hawk_nelson);
imgSeletedPhoto = (ImageView) findViewById(R.id.imgSelectedPhoto);
int width = bm.getWidth();
int height = bm.getHeight();
int maxWidth = (width > bm1.getWidth() ? width : bm1.getWidth());
int maxHeight = (height > bm1.getHeight() ? height : bm1.getHeight());
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) maxWidth) / width;
float scaleHeight = ((float) maxHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
bmOverlay = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
Canvas canvas = new Canvas(bmOverlay);
//canvas.drawBitmap(bm, 0, 0, null);
canvas.drawBitmap(bm1, 0, 50, null);
imgSeletedPhoto.setImageBitmap(bmOverlay);
} catch (Exception e) {
e.printStackTrace();
}
i want to scale image and image should look like below
but instead of by doing above code it is looking like
XML code of this file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="7" > // this is covering 70% of screen
<ImageView
android:id="#+id/imgSelectedPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#baca9d"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
can any body help me how to stretch image and make it full from bottom and top side also and put text in center
int targetWidth = 70% of screen width;
int targetHeight = 70% of screen height;
int saclex = (int)((float)targetWidth / (float)bmp1.getWidth());
int sacley = (int)((float)targetHeight / (float)bmp1.getHeight());
apply scale values to bmp1 and create scaled bitmap
saclex = (int)((float)targetWidth / (float)bmp.getWidth());
sacley = (int)((float)targetHeight / (float)bmp.getHeight());
apply scale values bmp and create scaled bitmap. Hope this will help. or else you can use this method to create it.
Bitmap bitmap = Bitmap.createScaledBitmap(mTargetImage, bWidth,
bHeight, false);

Categories

Resources