ImageButton image resize - java

I have the following problem:
I try to use Glide to add to my ImageButton a background, but if I try it, the image sometime very-very big, little, not in the right place, and often the real ImageButton sticking out from behind.
Like this picture: https://www.kephost.com/image/El2G
So, how can I resize, or place, to the button from behind doesn't look, and the whole picture can see in the upper screen.
My Codes:
.xml
<ImageButton
android:id="#+id/ib1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
.java
Glide.with(MainMenu.this)
.load(R.drawable.updatelogkisfelbontas)
.fitCenter()
.placeholder(R.drawable.updatelogkisfelbontas)
.into(ib1);
}

try this:
Glide.with(MainMenu.this)
.load(R.drawable.updatelogkisfelbontas)
.override(200, 200);
.centerCrop();
.placeholder(R.drawable.updatelogkisfelbontas)
.into(ib1);
}

Related

How to set ImageView's tint under (Expandable)ListAdapter?

Preamble
I've been g00gling for half an hour now, and it seems like I'm really missing something important.
Note: I've already tried the solutions for these questions
How to change color of vector drawable path on button click
How to set tint for an image view programmatically in android?
Changing ImageView source
These solutions either recolor ALL ImageView srcs or they don't do anything at all (vector remains black).
My drawable is a vector asset from the material icons directory:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2z" />
</vector>
My ImageView:
<ImageView
android:id="#+id/gradeBullet"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="25.5dp"
android:layout_marginTop="25.5dp"
app:srcCompat="#drawable/importantgrade" />
I would like to do this with *Compat, because I'd also like to support earlier APIs (API>17).
My ImageView: ImageView gradeBullet = convertView.findViewById(R.id.gradeBullet);
Try I.
MyAmazingAdapter.java - getChildView(...)
gradeBullet.setColorFilter(ContextCompat.getColor(context, gradeObj.colorId), PorterDuff.Mode.MULTIPLY);
Result: Does nothing.
Try II.
MyAmazingAdapter.java - getChildView(...)
DrawableCompat.setTint(gradeBullet.getDrawable(), ContextCompat.getColor(context, R.color.myColor));
Result: Recolors everything.
Try III.
MyAmazingAdapter.java - getChildView(...)
VectorDrawableCompat drawable = (VectorDrawableCompat) gradeBullet.getDrawable();
drawable.setTint(ContextCompat.getColor(context, gradeObj.colorId));
gradeBullet.setImageDrawable(drawable);
Result: Gives a weird color. (Definitely not what I want, it's closer to the original color)
Try IV.
All the above with a final to the gradeBullet, got the same results.
Some other info
The gradeObj.colorId is different for every childView.
Using shapes and setColor instead of SVG is not what I want.
Currently only tested with API lvl 26
MyAmazingAdapter.java extends BaseExpandableListAdapter
If you'd like more info, just mention it in a comment.
You can try your first method with PorterDuff.Mode.SRC_IN or PorterDuff.Mode.SRC_ATOP instead of PorterDuff.Mode.MULTIPLY.
PorterDuff.mode
Hope this helps.

Correctly animate buttons which have elevation/drop shadow set

I'm currently coding a simple memory game for android and ran into this problem:
I wanted to animate my card (which is an image button) to flip around. This works. What however doesn't work is, the drop shadow assigned to the button through the elevation property. It doesn't display as expected and introduces graphical glitches and small performance issues.
You can have a look at the glitches here:
graphical glitches on the drop shadow
The buttons are defined in my activity_memory.xml file like this:
<!-- ... -->
<ImageButton
android:layout_width="70dp"
android:layout_height="70dp"
android:id="#+id/btn_1_1"
android:layout_column="0"
android:background="#drawable/zelda0"
android:layout_margin="2dp"
android:gravity="center"
android:onClick="button11clicked"
android:elevation="5dp" />
<!-- ... -->
the animation is specified in my drawable/animator flipcardtoback.xml file like this:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Rotate. -->
<objectAnimator
android:valueFrom="0"
android:valueTo="180"
android:propertyName="rotationY"
android:interpolator="#android:interpolator/accelerate_decelerate"
android:duration="1000" />
</set>
The code triggers an animation function which flips the card and halfway through the animation duration it switches the background image of the button from the frontside image to the backside image of the card.
I guess the way I flip the card isn't the problem. It's just the drop shadow which glitches. I don't really know how to fix this, as I've tried to make a new drawable resource forming a drop shadow and assign this to the background property of the button. This however results in the image obviously not being displayed.
Is there any way to fix this so it animates the drop shadow correctly or in a way which is more pleasing to the eye?
Scale down one button and a scale up the reverse. This way the button stays flat and Android has no trouble displaying a shadow.
public static final AccelerateInterpolator
ACCELERATE_INTERPOLATOR = new AccelerateInterpolator();
public static final DecelerateInterpolator
DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
...
private void animateFromTo(final Button buttonLeave, final Button buttonArrive) {
buttonArrive.setVisibility(View.INVISIBLE);
buttonLeave.animate()
.setDuration(250)
.scaleY(0)
.setInterpolator(ACCELERATE_INTERPOLATOR)
.setListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
buttonLeave.setVisibility(View.INVISIBLE);
buttonArrive.setScaleY(0);
buttonArrive.setVisibility(View.VISIBLE);
buttonArrive.animate()
.setDuration(250)
.scaleY(1)
.setInterpolator(DECELERATE_INTERPOLATOR)
.setListener(null)
.start();
}
}).start();
}

Do not show images of ImageView in real devices

I want show image in ImageView, but don't show it. I use XML code and Java code but don't show. show in simulator but in real devices don't show. tested on LG G2, HTC One X, Samsung Galaxy S3.
my xml code :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:background="#drawable/show_sms_header" />
my java code :
Dialog_Header_Img = (ImageView) findViewById(R.id.sms_dialog_header);
Dialog_Header_Img.setImageResource(R.drawable.show_sms_header);
I used to be separated from each.
Please tell me the solution
First of all you should set image in xml by code:
android:src="#drawable/show_sms_header"
not by android:background, well you can set background to color or other image but your main image you should set by android:src
If you changing something about your image, leave first line in your code that you show, and delete second, if setting image is only thing you set, then you can delete both, because you set source of image in xml.
The "src" attribute should be set to your drawable if you want it to be "centerCrop" correctly.
Check this out :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/show_sms_header" />
Make sure that your file, called "show_sms_header", is in your drawable ( /res/drawable) directory.

Displaying an Image Android

I am developing an Fitness App. My app will have different exercises like Push Ups and Sit Ups. Every exercise need to have an image to show to the user.
I have been thinking a while on what is a good way to solve this problem. But I don't think my solution below is good. Have you worked with images on Android for displaying images? Give me your solution on how you did it.
An exercise have a name but also images. The purpose is to display a specific Exercise with the images and exercise name.
My Exercise class looks like this right now:
I have thought of having the path to the image saved which I can have access when I need to show the image. I am uploading the images on the assets folder.
public class Exercise {
private String exerciseName;
private String exerciseSmallImagePath;
private String exerciseLargeImagePath;
public Exercise(String exerciseName, String exerciseSmallImagePath, String exerciseLargeImagePath){
this.exerciseName = exerciseName;
this.exerciseSmallImagePath = exerciseSmallImagePath;
this.exerciseLargeImagePath = exerciseLargeImagePath;
}
}
saving the path to the image-source is definitly a good approach. Have a look at ImageViews in order to display your image. There are two approaches to implement such an ImageView:
1: define it in your XML and set the image-source afterwards in your oncreate-method:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Some description" />
2: define your ImageView programmatically in your Activity:
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(fetch your ID here);
someLinearLayout.addView(imageView);
I suppose you are familiar with how to create simple application in Android? If not, then you should get started with samples and reading up on Android developers guide
This article can give you good start in how to work with Images.
As for how you should accomplish this, I can only suggest one approach, the rest is upto your imagination.
Start with creating a fragment that has an image and a text below (or above if you like) it. You can then put this fragment where you want with new image and text.
Here is a rough idea
<LinearLayout (vertical orientation>
<Image ... />
<Text ... />
</LinearLayout>
Once the layout is in place you can set the image source at runtime.
A good way to display this information is to have steps which user can navigate using swiping (view pager).
Every page can have the above mentioned fragment that will show one step. This will result in cleaner, slide screen style guide.

Setting margin on an ImageView

I'm trying to set the imageview's layout_marginTop to one value for different density/screen sizes. In my values-mdpi folder I have the following line in dimensions.xml
<dimen name="marginTop">10dp</dimen>
In the MainActivity
ImageView image = (ImageView) findViewById(R.id.s_image);
But there is no setmargin method for imageview. Is there a way to do this?
You don't need to do that in code, you can do it in your XML file where the image view is defined. See this page for more details.
<TextView
android:layout_height="#dimen/textview_height"
android:layout_width="#dimen/textview_width"
android:textSize="#dimen/font_size"/>
You're on the right track. It's probably easiest to refer to your dimension value within the xml (rather than set this up in java code).
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="#dimen/yourMarginTopValue" />
Try try to put android:adjustViewBounds="true" to
It's probably better to do it in XML since you seem to already have the ImageView defined in XML.
However, the layout_* XML attributes refer to the LayoutParams of the parent layout, not the view itself. To change them in code, access them with getLayoutParams(), do your modifications and call requestLayout() to schedule a re-layout pass. For example:
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)imageView.getLayoutParams();
lp.topMargin = 123;
imageView.requestLayout();

Categories