Last active
December 5, 2018 10:31
-
-
Save evandroferreiras/0dade9d8acc2d5e0d145fde7373096bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.pkg.myimageviewlibrary; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.ColorMatrix; | |
import android.graphics.ColorMatrixColorFilter; | |
import android.graphics.drawable.Drawable; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
import android.widget.RelativeLayout; | |
import com.bumptech.glide.Glide; | |
public class MyImageViewBasic extends RelativeLayout { | |
//RelativeLayout properties | |
private Context context; | |
private AttributeSet attrs; | |
private int defStyleAttr; | |
//Core | |
private ImageView imageView; | |
//Attributes | |
private Drawable imageFile; | |
private Drawable imagePlaceHolder; | |
private Drawable imageError; | |
private boolean isBlackAndWhite; | |
public MyImageViewBasic(Context context) { | |
super(context); | |
this.context = context; | |
InitView(); | |
} | |
public MyImageViewBasic(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
this.context = context; | |
this.attrs = attrs; | |
InitView(); | |
} | |
public MyImageViewBasic(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
this.context = context; | |
this.attrs = attrs; | |
this.defStyleAttr = defStyleAttr; | |
InitView(); | |
} | |
private void InitView(){ | |
inflate(context, R.layout.my_image_view_layout, this); | |
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MyImageViewBasic, defStyleAttr, 0); | |
isBlackAndWhite = arr.getBoolean(R.styleable.MyImageViewBasic_isBlackAndWhite, false); | |
imageFile = arr.getDrawable(R.styleable.MyImageViewBasic_imageSrc); | |
imagePlaceHolder = arr.getDrawable(R.styleable.MyImageViewBasic_imagePlaceholder); | |
imageError = arr.getDrawable(R.styleable.MyImageViewBasic_imageError); | |
imageView = findViewById(R.id.my_image_view); | |
if (imageFile != null) { | |
setDrawableImage(imageFile); | |
} | |
if (isBlackAndWhite){ | |
setBlackAndWhite(); | |
} | |
arr.recycle(); | |
} | |
private void setBlackAndWhite(){ | |
ColorMatrix matrix = new ColorMatrix(); | |
matrix.setSaturation(0); | |
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); | |
this.imageView.setColorFilter(filter); | |
} | |
public void setDrawableImage(Drawable imageFile) { | |
imageView.setImageDrawable(imageFile); | |
} | |
public void setUrlImage(String url, int imageError, int imagePlaceHolder, | |
ImageView.ScaleType scaleType) { | |
imageView.setScaleType(scaleType); | |
Glide | |
.with(context) | |
.load(url) | |
.placeholder(imagePlaceHolder) | |
.crossFade() | |
.dontAnimate() | |
.error(imageError) | |
.into(imageView); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment