Load Images In ImageView ByDefault With Glide - android

My Question is about loading image into image View With Glide. I Know How to Load Image With Glide. But I want to Create a custom ImageView so that It can Load Images By Default With Glide (To make it faster and remove memory issues by default). Is It Possible. If Yes Then How? Thank you in advance.
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
///How to Over Ride Back Ground Attribute Sent by XML / Sent By Java Initialization
Glide.with(context).load(R.drawable.chains).into(this);
}
Update:
To be very simple I want To Use My Custom ImageView in my whole project Instead of Ordinary one. so that it will over come memory issues by default. In CustomImageView I want to Load Images with Glide . I'm Looking something Like
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
///How to Over Ride Back Ground Attribute Sent by XML / Sent By Java Initialization
#Override
public void setBackground(Drawable background) {
super.setBackground(background);
Glide.with(this.getContext()).load(background).into(this);
}
}
how to manage CustomImageView to work exactly like ImageView but Instead load Images With Glide.

Try something like this:
public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, #Nullable AttributeSet set) {
super(context, set);
int[] attrs = {android.R.attr.background};
TypedArray typedArray = context.obtainStyledAttributes(set, attrs);
Drawable drawable = typedArray.getDrawable(0); // 0 is an index of attrs[]
if (drawable != null) {
setBackground(null);
Glide.with(context).load(typedArray.getResourceId(0, placeholder_resource_id)).into(this); // 0 is an index of attrs[]
}
typedArray.recycle();
}
}

Related

How can i know that a imageview has been set a drawable that is or is not a specific one?

the question is about imageview in android:
There is an imageview, after I call method like
imageview.setImageDrawable(drawable)
or
imageview.setImageBitmap(bitmap)
then a drawable will be drawned on the imageview, I want to know that before I call another setXXXDrawable/Bitmap method to the same imageview , the drawable that had been drawned on this imageview has or has not been changed by other operation? or the image showned on the imageview has been changed since my last call?
Any help will be appreciated~ thks!
You can create your own ImageView to detect image changes , Here is the class from Sandy's answer , try with it , setImageChangeListner() to detect image changes .
public class MyImageView extends ImageView {
private OnImageChangeListiner onImageChangeListiner;
public MyImageView(Context context) {
super(context);
}
public MyImageView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public void setImageChangeListiner(
OnImageChangeListiner onImageChangeListiner) {
this.onImageChangeListiner = onImageChangeListiner;
}
#Override
public void setBackgroundResource(int resid) {
super.setBackgroundResource(resid);
if (onImageChangeListiner != null)
onImageChangeListiner.imageChangedinView(this);
}
#Override
public void setBackgroundDrawable(Drawable background) {
super.setBackgroundDrawable(background);
if (onImageChangeListiner != null)
onImageChangeListiner.imageChangedinView(this);
}
public static interface OnImageChangeListiner {
public void imageChangedinView(ImageView mImageView);
}
}
Reference Link : ImageView onImageChangedListener Android
First get the drawable from imageView.getDrawable() method and get bimap from that drawable that you just got. Now compare these both bitmaps to identify that you have both same image or image has been changed. Both will have same bytes if they will be same.
You can check whether an image has been assigned a drawable or not as follows :
if(imageView.getDrawable() != null) {
// you can get the drawable that has been assigned to it
} else {
// assign the desired drawable
imageview.setImageDrawable(drawable)
}

Rotated View's onTouch wrong coordinates

I must get upside down view w/o moving the System menu. So I do:
getWindow().getDecorView().getRootView().setRotation(180);
But all child buttons get onTouch event not when they being touched, but when I touch the areas where they would be without rotation.
How can I fix the problem?
Got solution without translation matrix and such... Just create a new Layout class like this:
public class RotatableView extends FrameLayout {
public RotatableView(Context context) {
this(context, null);
}
public RotatableView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
setUpsideDown();
}
private void setUpsideDown() {
this.setRotation(180);
}
}
and use it for your layouts

How to get URI from Drawable

I am working on an Android application where I deliver some built-in images and give the user the option to download some more from the web to use in the application. At some point in my app, I look at an ImageView in my layout and want to determine if the Drawable inside is an in-built resource or an image I have downloaded from the web to the SD Card.
Is there any way to extract the URI of the Drawable used in the ImageView? This way I would be able to see if it is a resource or a downloaded file.
Here is my code so far:
ImageView view = (ImageView) layout.findViewById(R.id.content_img);
Drawable image = view.getDrawable();
UPDATE:
Using Barry Fruitman's suggestion from below, I stored the URI of the image directly inside my custom ImageView for later use. Here is what my implementation looks like:
public class MemoryImageView extends ImageView {
private String storedUri = null;
public MemoryImageView(Context context, String Uri) {
super(context);
}
public MemoryImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MemoryImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public String getStoredUri() {
return storedUri;
}
public void setStoredUri(String storedUri) {
this.storedUri = storedUri;
}
}
And usage looks like this:
MemoryImageView view = (MemoryImageView) layout.findViewById(R.id.content_img);
String img = view.getStoredUri();
if(img.startsWith("android.resource")) {
//in-built resource
} else {
//downloaded image
}
No. Once you create the Drawable that information is lost. What I suggest you do is subclass the ImageView and add extra member(s) to keep track of whatever you want.
Replace:
<ImageView />
with
<com.mypackage.MyImageView />
And create:
class MyImageView extends ImageView {
protected final int LOCAL_IMAGE = 1;
protected final int REMOTE_IMAGE = 2;
protected int imageType;
}
MyImageView will behave exactly like an ImageView, but with that extra member that you can read and write anywhere you want. You will probably also have to override the ImageView contructor(s) with constructors that just call super().

Too much time on onmeasure method after setImageBitmap of ImageView

long time reader, first time asker so please be gently ;)
I have an Android code that displays around 10 pictures in second on ImageView.
(calling setImageBitmap on ImageView to set new bitmap)
This causes display lags so I'm trying to speed it as much as I can.
I found that (with traceview) that app spend a lot of time on methods:
ViewRoot.performTraversals
View.measure
FrameLayout.onmeasure
ViewGrou.measureChildWithMargins
I suspecting on ImageView measuring so i have create CustomImageView and overide onMeasure just to call super method and write sysout.
It seems that after every setImageBitmap onmeasure is called 3 times.
Bitmaps are allways of the same size so there is no change. Actually it can be change from time to time when image source is changed but that should be remeasured only then.
What can I change, overide, do to eliminite this measurement impact on application speed?
If you need further information tell me and I will provide it.
Sample code
final Bitmap bitmapScaled = Bitmap.createScaledBitmap(bitmap2, width, height, false);
imageView2.setScaleType(ScaleType.CENTER_INSIDE);
if (setSize) {
setSize(imageView2, width, height);
}
imageView2.setImageBitmap(bitmapScaled);
Method that I change to make it work faster (possibly not safe?!)
boolean layout=true;
int noL=1;
#Override
public void requestLayout() {
if (layout){
super.requestLayout();
if (noL>10){
layout=false;
}else{
noL++;
}
}
}
When I had a similar problem I did the following:
public class ImageViewEx extends ImageView{
private boolean makeRequest;
public ImageViewEx(Context context){
super(context);makeRequest=true;
}
public ImageViewEx(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ImageViewEx(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
makeRequest=true;
}
public void setImageBitmapNoRequestLayout(Bitmap bitmap) {
makeRequest = false;
setImageBitmap(bitmap);
makeRequest = true;
}
#Override public void requestLayout(){
if(makeRequest)super.requestLayout();
}
}
Now changing the bitmap with setImageBitmapNoRequestLayout(...) does not call requestLayout().

How can I add a decorator image on top of my image view

I have an ImageView in my android layout. And I would like to add a 'decorator' image at the lower right corner of my image view.
Can you tell me how can I do it?
I am thinking of doing with a FramLayout with 2 image views as its child, but how can i make one image the lower right corner of another?
<FrameLayout>
<ImageView .../>
<ImageView .../>
</FrameLayout>
You probably want to be using a RelativeLayout (Documentation) instead - it supports stacking views, and all you'd have to do is align the bottom and left of the overlay ImageView with the bottom and left.
You could create your own class that extends ImageView. It will always draw decorator over the ImageView content.
public class MyImage extends ImageView {
static Bitmap decorator;
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(decorator==null)
decorator = BitmapFactory.decodeResource(getResources(), R.drawable.decorator);
canvas.drawBitmap(decorator, 0, 0, null);
}
public MyImage(Context context) {
super(context);
}
public MyImage(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public MyImage(Context context, AttributeSet attrs, int params) {
super(context, attrs, params);
}
}

Categories

Resources